Documentation ¶
Overview ¶
Package rivescript implements the RiveScript chatbot scripting language.
UTF-8 Support ¶
UTF-8 support in RiveScript is considered an experimental feature. It is disabled by default. Enable it by setting `RiveScript.UTF8 = true`.
By default (without UTF-8 mode on), triggers may only contain basic ASCII characters (no foreign characters), and the user's message is stripped of all characters except letters, numbers and spaces. This means that, for example, you can't capture a user's e-mail address in a RiveScript reply, because of the @ and . characters.
When UTF-8 mode is enabled, these restrictions are lifted. Triggers are only limited to not contain certain metacharacters like the backslash, and the user's message is only stripped of backslashes and HTML angled brackets (to protect from obvious XSS if you use RiveScript in a web application). Additionally, common punctuation characters are stripped out, with the default set being `/[.,!?;:]/g`. This can be overridden by providing a new `Regexp` object as the `RiveScript.UnicodePunctuation` attribute. Example:
// Make a new bot with UTF-8 mode enabled. bot := rivescript.New() bot.UTF8 = true // Override the punctuation characters that get stripped from the // user's message. bot.UnicodePunctuation = regexp.MustCompile(`[.,!?;:]`);
The `<star>` tags in RiveScript will capture the user's "raw" input, so you can write replies to get the user's e-mail address or store foreign characters in their name.
See Also ¶
The official homepage of RiveScript, http://www.rivescript.com/
Index ¶
- Constants
- type MacroInterface
- type RiveScript
- func (rs *RiveScript) ClearAllUservars()
- func (rs *RiveScript) ClearUservars(username string)
- func (rs *RiveScript) CurrentUser() string
- func (rs *RiveScript) DeleteSubroutine(name string)
- func (rs *RiveScript) DumpSorted()
- func (rs *RiveScript) DumpTopics()
- func (rs *RiveScript) FreezeUservars(username string) error
- func (rs *RiveScript) GetAllUservars() map[string]map[string]string
- func (rs *RiveScript) GetUservar(username string, name string) (string, error)
- func (rs *RiveScript) GetUservars(username string) (map[string]string, error)
- func (rs *RiveScript) GetVariable(name string) (string, error)
- func (rs *RiveScript) LastMatch(username string) (string, error)
- func (rs *RiveScript) LoadDirectory(path string, extensions ...string) error
- func (rs *RiveScript) LoadFile(path string) error
- func (rs *RiveScript) RemoveHandler(lang string)
- func (rs *RiveScript) Reply(username string, message string) string
- func (rs *RiveScript) SetDebug(value bool)
- func (rs *RiveScript) SetDepth(value int)
- func (rs *RiveScript) SetGlobal(name string, value string)
- func (rs *RiveScript) SetHandler(lang string, handler MacroInterface)
- func (rs *RiveScript) SetPerson(name string, value string)
- func (rs *RiveScript) SetStrict(value bool)
- func (rs *RiveScript) SetSubroutine(name string, fn Subroutine)
- func (rs *RiveScript) SetSubstitution(name string, value string)
- func (rs *RiveScript) SetUTF8(value bool)
- func (rs *RiveScript) SetUnicodePunctuation(value string)
- func (rs *RiveScript) SetUservar(username string, name string, value string)
- func (rs *RiveScript) SetUservars(username string, data map[string]string)
- func (rs *RiveScript) SetVariable(name string, value string)
- func (rs *RiveScript) SortReplies()
- func (rs *RiveScript) Stream(code string) error
- func (rs *RiveScript) ThawUservars(username string, action string) error
- func (rs *RiveScript) Version() string
- type Subroutine
Examples ¶
Constants ¶
const ( VERSION = "0.0.2" RS_VERSION float64 = 2.0 )
Constants
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type MacroInterface ¶
type RiveScript ¶
type RiveScript struct { // Parameters Debug bool // Debug mode Strict bool // Strictly enforce RiveScript syntax Depth int // Max depth for recursion UTF8 bool // Support UTF-8 RiveScript code UnicodePunctuation *regexp.Regexp // contains filtered or unexported fields }
Example ¶
package main import ( "fmt" rivescript "github.com/aichaos/rivescript-go" ) func main() { 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) }
Output:
Example (Javascript) ¶
package main import ( "fmt" rivescript "github.com/aichaos/rivescript-go" "github.com/aichaos/rivescript-go/lang/rivescript_js" ) func main() { // 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) }
Output:
Example (Subroutine) ¶
package main import ( "fmt" rivescript "github.com/aichaos/rivescript-go" ) func main() { // 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) }
Output:
func New ¶
func New() *RiveScript
func (*RiveScript) ClearAllUservars ¶
func (rs *RiveScript) ClearAllUservars()
ClearAllUservars clears all variables for all users.
func (*RiveScript) ClearUservars ¶
func (rs *RiveScript) ClearUservars(username string)
ClearUservars clears all a user's variables.
func (*RiveScript) CurrentUser ¶
func (rs *RiveScript) CurrentUser() string
CurrentUser returns the current user's ID.
func (*RiveScript) DeleteSubroutine ¶
func (rs *RiveScript) DeleteSubroutine(name string)
DeleteSubroutine removes a Go object macro.
func (*RiveScript) DumpSorted ¶
func (rs *RiveScript) DumpSorted()
DumpSorted is a debug method which dumps the sort tree from the bot's memory.
func (*RiveScript) DumpTopics ¶
func (rs *RiveScript) DumpTopics()
DumpTopics is a debug method which dumps the topic structure from the bot's memory.
func (*RiveScript) FreezeUservars ¶
func (rs *RiveScript) FreezeUservars(username string) error
FreezeUservars freezes the variable state of a user.
This will clone and preserve the user's entire variable state, so that it can be restored later with `ThawUservars()`.
func (*RiveScript) GetAllUservars ¶
func (rs *RiveScript) GetAllUservars() map[string]map[string]string
GetAllUservars gets all the variables for all the users.
This returns a map of username (strings) to `map[string]string` of their variables.
func (*RiveScript) GetUservar ¶
func (rs *RiveScript) GetUservar(username string, name string) (string, error)
GetUservar gets a user variable.
This is equivalent to `<get name>` in RiveScript. Returns `undefined` if the variable isn't defined.
func (*RiveScript) GetUservars ¶
func (rs *RiveScript) GetUservars(username string) (map[string]string, error)
GetUservars gets all the variables for a user.
This returns a `map[string]string` containing all the user's variables.
func (*RiveScript) GetVariable ¶
func (rs *RiveScript) GetVariable(name string) (string, error)
GetVariable gets a bot variable.
This is equivalent to `<bot name>` in RiveScript. Returns `undefined` if the variable isn't defined.
func (*RiveScript) LastMatch ¶
func (rs *RiveScript) LastMatch(username string) (string, error)
LastMatch returns the user's last matched trigger.
func (*RiveScript) LoadDirectory ¶
func (rs *RiveScript) LoadDirectory(path string, extensions ...string) error
LoadDirectory loads multiple RiveScript documents from a folder on disk.
Params:
path: Path to the directory on disk extensions...: List of file extensions to filter on, default is '.rive' and '.rs'
func (*RiveScript) LoadFile ¶
func (rs *RiveScript) LoadFile(path string) error
LoadFile loads a single RiveScript source file from disk.
Params:
path: File path to
func (*RiveScript) RemoveHandler ¶
func (rs *RiveScript) RemoveHandler(lang string)
DeleteHandler removes an object macro language handler.
func (*RiveScript) Reply ¶
func (rs *RiveScript) Reply(username string, message string) string
Reply fetches a reply from the bot for a user's message.
Params:
username: The name of the user requesting a reply. message: The user's message.
func (*RiveScript) SetDebug ¶
func (rs *RiveScript) SetDebug(value bool)
SetDebug sets the value of rs.Debug (maybe useful for non-Go bindings)
func (*RiveScript) SetDepth ¶
func (rs *RiveScript) SetDepth(value int)
SetDepth sets the value of rs.Depth (maybe useful for non-Go bindings)
func (*RiveScript) SetGlobal ¶
func (rs *RiveScript) SetGlobal(name string, value string)
SetGlobal sets a global variable.
This is equivalent to `! global` in RiveScript. Set the value to `undefined` to delete a global.
func (*RiveScript) SetHandler ¶
func (rs *RiveScript) SetHandler(lang string, handler MacroInterface)
SetHandler sets a custom language handler for RiveScript object macros.
func (*RiveScript) SetPerson ¶
func (rs *RiveScript) SetPerson(name string, value string)
SetPerson sets a person substitution pattern.
This is equivalent to `! person` in RiveScript. Set the value to `undefined` to delete a person substitution.
func (*RiveScript) SetStrict ¶
func (rs *RiveScript) SetStrict(value bool)
SetStrict sets the value of rs.Strict (maybe useful for non-Go bindings)
func (*RiveScript) SetSubroutine ¶
func (rs *RiveScript) SetSubroutine(name string, fn Subroutine)
SetSubroutine defines a Go object macro from your program.
Params:
name: The name of your subroutine for the `<call>` tag in RiveScript. fn: A function with a prototype `func(*RiveScript, []string) string`
func (*RiveScript) SetSubstitution ¶
func (rs *RiveScript) SetSubstitution(name string, value string)
SetSubstitution sets a substitution pattern.
This is equivalent to `! sub` in RiveScript. Set the value to `undefined` to delete a substitution.
func (*RiveScript) SetUTF8 ¶
func (rs *RiveScript) SetUTF8(value bool)
SetUTF8 sets the value of rs.UTF8 (maybe useful for non-Go bindings)
func (*RiveScript) SetUnicodePunctuation ¶
func (rs *RiveScript) SetUnicodePunctuation(value string)
SetUnicodePunctuation sets the value of rs.UnicodePunctuation (maybe useful for non-Go bindings)
func (*RiveScript) SetUservar ¶
func (rs *RiveScript) SetUservar(username string, name string, value string)
SetUservar sets a variable for a user.
This is equivalent to `<set>` in RiveScript. Set the value to `undefined` to delete a substitution.
func (*RiveScript) SetUservars ¶
func (rs *RiveScript) SetUservars(username string, data map[string]string)
SetUservars sets a map of variables for a user.
Set multiple user variables by providing a map[string]string of key/value pairs. Equivalent to calling `SetUservar()` for each pair in the map.
func (*RiveScript) SetVariable ¶
func (rs *RiveScript) SetVariable(name string, value string)
SetVariable sets a bot variable.
This is equivalent to `! var` in RiveScript. Set the value to `undefined` to delete a bot variable.
func (*RiveScript) SortReplies ¶
func (rs *RiveScript) SortReplies()
SortReplies sorts the reply structures in memory for optimal matching.
After you have finished loading your RiveScript code, call this method to populate the various sort buffers. This is absolutely necessary for reply matching to work efficiently!
func (*RiveScript) Stream ¶
func (rs *RiveScript) Stream(code string) error
Stream loads RiveScript code from a text buffer.
Params:
code: Raw source code of a RiveScript document, with line breaks after each line.
func (*RiveScript) ThawUservars ¶
func (rs *RiveScript) ThawUservars(username string, action string) error
ThawUservars unfreezes a user's variables.
The `action` can be one of the following: * thaw: Restore the variables and delete the frozen copy. * discard: Don't restore the variables, just delete the frozen copy. * keep: Keep the frozen copy after restoring.
func (*RiveScript) Version ¶
func (rs *RiveScript) Version() string
type Subroutine ¶
type Subroutine func(*RiveScript, []string) string
Subroutine is a Golang function type for defining an object macro in Go.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
cmd
|
|
rivescript
Stand-alone RiveScript Interpreter.
|
Stand-alone RiveScript Interpreter. |
lang
|
|
rivescript_js
Package rivescript_js implements JavaScript object macros for RiveScript.
|
Package rivescript_js implements JavaScript object macros for RiveScript. |