Ejemplo n.º 1
0
// InitBrain loads the RiveScript brain.
func (self *Scarecrow) InitBrain() {
	self.Brain = rivescript.New()
	jsHandler := rivescript_js.New(self.Brain)
	self.Brain.SetHandler("javascript", jsHandler)
	self.Brain.LoadDirectory(self.BotsConfig.Personality.Brain.Replies)
	self.Brain.SortReplies()
}
Ejemplo n.º 2
0
func ExampleRiveScript_javascript() {
	// Example for configuring the JavaScript object macro handler via Otto.

	bot := rivescript.New()

	// Create the JS handler.
	jsHandler := rivescript_js.New(bot)
	bot.SetHandler("javascript", jsHandler)

	// 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)
}
Ejemplo n.º 3
0
func main() {
	// Collect command line arguments.
	debug := flag.Bool("debug", false, "Enable debug mode.")
	utf8 := flag.Bool("utf8", false, "Enable UTF-8 mode.")
	depth := flag.Int("depth", 50, "Recursion depth limit (default 50)")
	flag.Parse()
	args := flag.Args()

	if len(args) == 0 {
		fmt.Fprintln(os.Stderr, "Usage: rivescript [options] </path/to/documents>")
		os.Exit(1)
	}

	root := args[0]

	// Initialize the bot.
	bot := rivescript.New()
	bot.Debug = *debug
	bot.UTF8 = *utf8
	bot.Depth = *depth

	// JavaScript object macro handler.
	jsHandler := rivescript_js.New(bot)
	bot.SetHandler("javascript", jsHandler)

	// Load the target directory.
	err := bot.LoadDirectory(root)
	if err != nil {
		fmt.Printf("Error loading directory: %s", err)
		os.Exit(1)
	}

	bot.SortReplies()

	fmt.Printf(`RiveScript Interpreter (Golang) -- Interactive Mode
---------------------------------------------------
RiveScript version: %s
        Reply root: %s

You are now chatting with the RiveScript bot. Type a message
and press Return to send it. When finished, type '/quit' to
exit the program. Type '/help' for other options.
`, bot.Version(), root)

	// Drop into the interactive command shell.
	reader := bufio.NewReader(os.Stdin)
	for {
		fmt.Print("You> ")
		text, _ := reader.ReadString('\n')
		text = strings.TrimSpace(text)
		if len(text) == 0 {
			continue
		}

		if strings.Index(text, "/help") == 0 {
			help()
		} else if strings.Index(text, "/quit") == 0 {
			os.Exit(0)
		} else {
			reply := bot.Reply("localuser", text)
			fmt.Printf("Bot> %s\n", reply)
		}
	}
}