Beispiel #1
0
func ExampleRiveScript_subroutine() {
	// Example for defining a Go function as an object macro.
	// import rss "github.com/aichaos/rivescript-go/src"

	bot := rivescript.New(config.Basic())

	// Define an object macro named `setname`
	bot.SetSubroutine("setname", func(rs *rss.RiveScript, args []string) string {
		uid := rs.CurrentUser()
		rs.SetUservar(uid, args[0], args[1])
		return ""
	})

	// Stream in some RiveScript code.
	bot.Stream(`
		+ my name is *
		- I will remember that.<call>setname <id> <formal></call>

		+ what is my name
		- You are <get name>.
	`)
	bot.SortReplies()

	_ = bot.Reply("local-user", "my name is bob")
	reply := bot.Reply("local-user", "What is my name?")
	fmt.Printf("Bot: %s\n", reply)
}
Beispiel #2
0
func NewTest(t *testing.T) *RiveScriptTest {
	return &RiveScriptTest{
		bot:      rivescript.New(config.Basic()),
		t:        t,
		username: "******",
	}
}
Beispiel #3
0
func New(cfg *config.Config) *RiveScript {
	rs := new(RiveScript)
	if cfg == nil {
		cfg = config.Basic()
	}

	if cfg.SessionManager == nil {
		rs.say("No SessionManager config: using default MemoryStore")
		cfg.SessionManager = memory.New()
	}

	if cfg.Depth <= 0 {
		rs.say("No depth config: using default 50")
		cfg.Depth = 50
	}

	rs.Debug = cfg.Debug
	rs.Strict = cfg.Strict
	rs.UTF8 = cfg.UTF8
	rs.Depth = cfg.Depth
	rs.sessions = cfg.SessionManager

	rs.UnicodePunctuation = regexp.MustCompile(`[.,!?;:]`)

	// Initialize helpers.
	rs.parser = parser.New(parser.ParserConfig{
		Strict:  rs.Strict,
		UTF8:    rs.UTF8,
		OnDebug: rs.say,
		OnWarn:  rs.warnSyntax,
	})

	// Initialize all the data structures.
	rs.global = map[string]string{}
	rs.var_ = map[string]string{}
	rs.sub = map[string]string{}
	rs.person = map[string]string{}
	rs.array = map[string][]string{}
	rs.includes = map[string]map[string]bool{}
	rs.inherits = map[string]map[string]bool{}
	rs.objlangs = map[string]string{}
	rs.handlers = map[string]macro.MacroInterface{}
	rs.subroutines = map[string]Subroutine{}
	rs.topics = map[string]*astTopic{}
	rs.thats = map[string]*thatTopic{}
	rs.sorted = new(sortBuffer)

	return rs
}
Beispiel #4
0
func ExampleRiveScript() {
	bot := rivescript.New(config.Basic())

	// Load a directory full of RiveScript documents (.rive files)
	bot.LoadDirectory("eg/brain")

	// Load an individual file.
	bot.LoadFile("testsuite.rive")

	// Sort the replies after loading them!
	bot.SortReplies()

	// Get a reply.
	reply := bot.Reply("local-user", "Hello, bot!")
	fmt.Printf("The bot says: %s", reply)
}
Beispiel #5
0
func ExampleRiveScript_javascript() {
	// Example for configuring the JavaScript object macro handler via Otto.

	bot := rivescript.New(config.Basic())

	// Create the JS handler.
	bot.SetHandler("javascript", javascript.New(bot))

	// Now we can use object macros written in JS!
	bot.Stream(`
		> 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>.
	`)
	bot.SortReplies()

	reply := bot.Reply("local-user", "Add 5 and 7")
	fmt.Printf("Bot: %s\n", reply)
}