rivescript-go: github.com/aichaos/rivescript-go/lang/javascript Index | Files

package javascript

import "github.com/aichaos/rivescript-go/lang/javascript"

Package javascript implements JavaScript object macros for RiveScript.

This is powered by the Otto JavaScript engine[1], which is a JavaScript engine written in pure Go. It is not the V8 engine used by Node, so expect possible compatibility issues to arise.

Usage is simple. In your Golang code:

import (
	rivescript "github.com/aichaos/rivescript-go"

func main() {
	bot := rivescript.New(nil)
	jsHandler := javascript.New(bot)
	bot.SetHandler("javascript", jsHandler)

	// and go on as normal

And in your RiveScript code, you can load and run JavaScript objects:

> object add javascript
	var a = args[0];
	var b = args[1];
	return parseInt(a) + parseInt(b);
< object

> object setname javascript
	// Set the user's name via JavaScript
	var uid = rs.CurrentUser();
	rs.SetUservar(uid, args[0], args[1])
< object

+ add # and #
- <star1> + <star2> = <call>add <star1> <star2></call>

+ my name is *
- I will remember that.<call>setname <id> <formal></call>

+ what is my name
- You are <get name>.

[1]: https://github.com/robertkrimen/otto


Package Files


type JavaScriptHandler Uses

type JavaScriptHandler struct {
    // contains filtered or unexported fields

func New Uses

func New(rs *rivescript.RiveScript) *JavaScriptHandler

New creates an object handler for JavaScript with its own Otto VM.

func (JavaScriptHandler) Call Uses

func (js JavaScriptHandler) Call(name string, fields []string) string

Call executes a JavaScript macro and returns its results.

func (JavaScriptHandler) Load Uses

func (js JavaScriptHandler) Load(name string, code []string)

Load loads a new JavaScript object macro into the VM.

Package javascript imports 4 packages (graph) and is imported by 1 packages. Updated 2016-12-27. Refresh now. Tools for package owners.