示例#1
0
func ExampleRiveScript_subroutine() {
	// Example for defining a Go function as an object macro.

	bot := rivescript.New()

	// Define an object macro named `setname`
	bot.SetSubroutine("setname", func(rs *rivescript.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)
}
示例#2
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()
}
示例#3
0
func NewTest(t *testing.T) *RiveScriptTest {
	tester := new(RiveScriptTest)
	tester.bot = rivescript.New()
	tester.t = t
	tester.username = "******"
	return tester
}
示例#4
0
func ExampleRiveScript() {
	bot := rivescript.New()

	// 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)
}
示例#5
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)
}
示例#6
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)
		}
	}
}
示例#7
0
// InitBrain loads the RiveScript brain.
func (self *Scarecrow) InitBrain() {
	self.Brain = rivescript.New()
	self.Brain.LoadDirectory(self.BotsConfig.Personality.Brain.Replies)
	self.Brain.SortReplies()
}
示例#8
0
文件: main.go 项目: aichaos/rsgb
func RiveScript() *js.Object {
	return js.MakeWrapper(rivescript.New())
}