diff --git a/Changes.md b/Changes.md index e4b279e..117b24c 100644 --- a/Changes.md +++ b/Changes.md @@ -2,7 +2,7 @@ This documents the history of significant changes to `rivescript-go`. -## v0.1.1 - TBD +## v0.2.0 - Feb 7, 2016 This update focuses on bug fixes and code reorganization. @@ -36,6 +36,13 @@ This update focuses on bug fixes and code reorganization. ### Changes +* Add ANSI colors to the RiveScript shell (`cmd/rivescript`); they can be + disabled with the `-nocolor` command line option. +* Add new commands to the RiveScript shell: + * `/debug [true|false]` to toggle the debug mode (`/debug` will print + the current setting of debug mode). + * `/dump ` to print the internal data structures for the + topics and sorted trigger sets, respectively. * Separate the unit tests into multiple files and put them in the `rivescript` package instead of `rivescript_test`; this enables test code coverage reporting (we're at 72.1% coverage!) @@ -60,7 +67,9 @@ This update focuses on bug fixes and code reorganization. * Fix `LoadDirectory()` to return an error when doesn't find any RiveScript source files to load, which helps protect against the common error that you gave it the wrong directory. -* New unit tests: object macros +* New unit tests: object macros. +* An internal optimization that allowed for cleaning up a redundant storage + location for triggers that have `%Previous` commands (PR #20) ## v0.1.0 - Dec 11, 2016 diff --git a/LICENSE b/LICENSE index 40f5afc..cabbc44 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2015 Noah Petherbridge +Copyright (c) 2017 Noah Petherbridge Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 830f81f..c94d5c7 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # RiveScript-Go +[![GoDoc](https://godoc.org/github.com/aichaos/rivescript-go?status.svg)](https://godoc.org/github.com/aichaos/rivescript-go) [![Gitter](https://badges.gitter.im/aichaos/rivescript-go.svg)](https://gitter.im/aichaos/rivescript-go?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) [![Build Status](https://travis-ci.org/aichaos/rivescript-go.svg?branch=master)](https://travis-ci.org/aichaos/rivescript-go) @@ -41,20 +42,23 @@ To test drive RiveScript in your web browser, try the ## Documentation -* RiveScript Library: -* RiveScript Stand-alone Interpreter: -* JavaScript Object Macros: +* RiveScript Library: +* RiveScript Stand-alone Interpreter: +* JavaScript Object Macros: +* RiveScript Parser: Also check out the [**RiveScript Community Wiki**](https://github.com/aichaos/rivescript/wiki) for common design patterns and tips & tricks for RiveScript. ## Installation +For the development library: + `go get github.com/aichaos/rivescript-go` -For the stand-alone binary for testing a RiveScript bot: +For the stand-alone `rivescript` binary for testing a bot: -`go install github.com/aichaos/rivescript-go/cmd/rivescript` +`go get github.com/aichaos/rivescript-go/cmd/rivescript` ## Usage @@ -70,7 +74,7 @@ $ rivescript eg/brain > rivescript.exe eg/brain ``` -See `rivescript --help` for options it accepts, including debug mode and UTF-8 +See `rivescript -help` for options it accepts, including debug mode and UTF-8 mode. When used as a library for writing your own chatbot, the synopsis is as follows: @@ -81,11 +85,14 @@ package main import ( "fmt" "github.com/aichaos/rivescript-go" - "github.com/aichaos/rivescript-go/config" ) func main() { - bot := rivescript.New(config.Basic()) + // Create a new bot with the default settings. + bot := rivescript.New(nil) + + // To enable UTF-8 mode, you'd have initialized the bot like: + bot = rivescript.New(rivescript.WithUTF8()) // Load a directory full of RiveScript documents (.rive files) err := bot.LoadDirectory("eg/brain") @@ -94,7 +101,7 @@ func main() { } // Load an individual file. - err = bot.LoadFile("brain/testsuite.rive") + err = bot.LoadFile("./testsuite.rive") if err != nil { fmt.Printf("Error loading from file: %s", err) } @@ -103,8 +110,12 @@ func main() { bot.SortReplies() // Get a reply. - reply := bot.Reply("local-user", "Hello, bot!") - fmt.Printf("The bot says: %s", reply) + reply, err := bot.Reply("local-user", "Hello, bot!") + if err != nil { + fmt.Printf("Error: %s\n", err) + } else { + fmt.Printf("The bot says: %s", reply) + } } ``` @@ -115,7 +126,7 @@ all the supported options. You only need to provide keys that are different to the defaults. ```go -bot := rs.New(&config.Config{ +bot := rivescript.New(&rivescript.Config{ Debug: false, // Debug mode, off by default Strict: false, // No strict syntax checking UTF8: false, // No UTF-8 support enabled by default @@ -124,17 +135,14 @@ bot := rs.New(&config.Config{ }) ``` -For convenience, the `config` package provides two config templates: +For convenience, you can use a shortcut: ```go -// Basic has all the defaults, plus Strict=true -bot := rs.New(config.Basic()) - -// UTF8 has all of Basic's settings, plus UTF8=true -bot := rs.New(config.UTF8()) +// A nil config uses all the defaults. +bot = rivescript.New(nil) -// You can also provide a nil configuration, which defaults to Basic() -bot := rs.New(nil) +// WithUTF8 enables UTF-8 mode (other settings left as default). +bot = rivescript.New(rivescript.WithUTF8()) ``` ## Object Macros @@ -152,7 +160,7 @@ JavaScript object macros using the Otto library. ## UTF-8 Support UTF-8 support in RiveScript is considered an experimental feature. It is -disabled by default. Enable it by setting `RiveScript.SetUTF8(true)`. +disabled by default. 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 @@ -170,10 +178,10 @@ string literal to the `RiveScript.SetUnicodePunctuation` function. Example: ```go // Make a new bot with UTF-8 mode enabled. -bot := rivescript.New(config.UTF8()) +bot := rivescript.New(rivescript.WithUTF8()) -// Override the punctuation characters that get stripped from the -// user's message. +// Override the punctuation characters that get stripped +// from the user's message. bot.SetUnicodePunctuation(`[.,!?;:]`); ``` @@ -263,7 +271,7 @@ The distributable directory contains only the following types of files: ``` The MIT License (MIT) -Copyright (c) 2016 Noah Petherbridge +Copyright (c) 2017 Noah Petherbridge Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/cmd/rivescript/main.go b/cmd/rivescript/main.go index 245aae6..552b438 100644 --- a/cmd/rivescript/main.go +++ b/cmd/rivescript/main.go @@ -41,7 +41,7 @@ func init() { flag.BoolVar(&version, "version", false, "Show the version number and exit.") flag.BoolVar(&debug, "debug", false, "Enable debug mode.") flag.BoolVar(&utf8, "utf8", false, "Enable UTF-8 mode.") - flag.UintVar(&depth, "depth", 50, "Recursion depth limit (default 50)") + flag.UintVar(&depth, "depth", 50, "Recursion depth limit") flag.BoolVar(&nostrict, "nostrict", false, "Disable strict syntax checking") flag.BoolVar(&nocolor, "nocolor", false, "Disable ANSI colors") } diff --git a/eg/json-server/main.go b/eg/json-server/main.go index 0f460de..16d17dd 100644 --- a/eg/json-server/main.go +++ b/eg/json-server/main.go @@ -9,7 +9,6 @@ import ( "strings" "github.com/aichaos/rivescript-go" - "github.com/aichaos/rivescript-go/config" "github.com/aichaos/rivescript-go/lang/javascript" ) @@ -32,7 +31,7 @@ func main() { flag.Parse() // Set up the RiveScript bot. - Bot = rivescript.New(&config.Config{ + Bot = rivescript.New(&rivescript.Config{ Debug: *debug, UTF8: *utf8, }) @@ -48,14 +47,14 @@ func main() { log.Fatal(http.ListenAndServe(addr, nil)) } -// Type Request describes the JSON arguments to the API. +// Request describes the JSON arguments to the API. type Request struct { Username string `json:"username"` Message string `json:"message"` Vars map[string]string `json:"vars"` } -// Type Response describes the JSON output from the API. +// Response describes the JSON output from the API. type Response struct { Status string `json:"status"` // 'ok' or 'error' Error string `json:"error,omitempty"` @@ -98,7 +97,11 @@ func ReplyHandler(w http.ResponseWriter, r *http.Request) { } // Get a reply from the bot. - reply := Bot.Reply(params.Username, params.Message) + reply, err := Bot.Reply(params.Username, params.Message) + if err != nil { + writeError(w, err.Error(), http.StatusInternalServerError) + return + } // Retrieve all user variables from the bot. var vars map[string]string diff --git a/eg/json-server/main_test.go b/eg/json-server/main_test.go index 3d765d7..1925342 100644 --- a/eg/json-server/main_test.go +++ b/eg/json-server/main_test.go @@ -9,12 +9,11 @@ import ( "reflect" "testing" - rivescript "github.com/aichaos/rivescript-go" - "github.com/aichaos/rivescript-go/config" + "github.com/aichaos/rivescript-go" ) func init() { - Bot = rivescript.New(config.UTF8()) + Bot = rivescript.New(rivescript.WithUTF8()) Bot.Stream(` + hello bot - Hello human. diff --git a/rivescript.go b/rivescript.go index 5154c9f..2e1f183 100644 --- a/rivescript.go +++ b/rivescript.go @@ -20,7 +20,7 @@ import ( ) // VERSION describes the module version. -const VERSION string = "0.1.1" +const VERSION string = "0.2.0" // RiveScript represents an individual chatbot instance. type RiveScript struct {