-
Notifications
You must be signed in to change notification settings - Fork 15
/
doc_test.go
127 lines (101 loc) · 3.29 KB
/
doc_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package rivescript_test
import (
"fmt"
"github.com/aichaos/rivescript-go"
"github.com/aichaos/rivescript-go/lang/javascript"
)
func Example() {
// Create a new RiveScript instance, which represents an individual bot
// with its own brain and memory of users.
//
// You can provide a rivescript.Config struct to configure the bot and
// provide values that differ from the defaults:
bot := rivescript.New(&rivescript.Config{
UTF8: true, // enable UTF-8 mode
Debug: true, // enable debug mode
})
// Or if you want the default configuration, provide a nil config.
// See the documentation for the rivescript.Config type for information
// on what the defaults are.
bot = rivescript.New(nil)
// Load a directory full of RiveScript documents (.rive files)
bot.LoadDirectory("eg/brain")
// Load an individual file.
bot.LoadFile("testsuite.rive")
// Stream in more RiveScript code dynamically from a string.
bot.Stream(`
+ hello bot
- Hello, human!
`)
// 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)
}
func ExampleRiveScript_utf8() {
// Examples of using UTF-8 mode in RiveScript.
bot := rivescript.New(rivescript.WithUTF8())
bot.Stream(`
// Without UTF-8 mode enabled, this trigger would be a syntax error
// for containing non-ASCII characters; but in UTF-8 mode you can use it.
+ comment ça va
- ça va bien.
`)
// Always call SortReplies when you're done loading replies.
bot.SortReplies()
// Without UTF-8 mode enabled, the user's message "comment ça va" would
// have the ç symbol removed; but in UTF-8 mode it's preserved and can
// match the trigger we defined.
reply, _ := bot.Reply("local-user", "Comment ça va?")
fmt.Println(reply) // "ça va bien."
}
func ExampleRiveScript_javascript() {
// Example for configuring the JavaScript object macro handler via Otto.
bot := rivescript.New(nil)
// 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)
}
func ExampleRiveScript_subroutine() {
// Example for defining a Go function as an object macro.
bot := rivescript.New(nil)
// 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)
}