aichaos / rivescript-go

A RiveScript interpreter for Go. RiveScript is a scripting language for chatterbots.
https://www.rivescript.com/
MIT License
60 stars 16 forks source link

Add a Redis session driver for RiveScript #24

Closed kirsle closed 7 years ago

kirsle commented 7 years ago

This adds a Redis session manager driver for RiveScript.

package main

import (
    "fmt"

    rivescript "github.com/aichaos/rivescript-go"
    "github.com/aichaos/rivescript-go/sessions/redis"
    goRedis "gopkg.in/redis.v5"
)

func main() {
    // Verbose example with ALL options spelled out. All the settings are
    // optional, and their default values are shown here.
    bot := rivescript.New(&rivescript.Config{
        // Initialize the Redis session manager here.
        SessionManager: redis.New(&redis.Config{
            // The prefix is added before all the usernames in the Redis cache.
            // For a username of 'alice' it would go into 'rivescript/alice'
            Prefix: "rivescript/",

            // The prefix used to store 'frozen' copies of user variables. The
            // default takes the form "frozen:<prefix>" using your Prefix,
            // so this field is doubly optional unless you wanna customize it.
            FrozenPrefix: "frozen:rivescript/",

            // If you need to configure the underlying Redis instance, you can
            // pass its options along here.
            Redis: &goRedis.Options{
                Addr: "localhost:6379",
                DB:   0,
            },
        }),
    })

    // A minimal version of the above that uses all the default options.
    bot = rivescript.New(&rivescript.Config{
        SessionManager: redis.New(nil),
    })

    bot.LoadDirectory("eg/brain")
    bot.SortReplies()

    // And go on as normal.
    reply, err := bot.Reply("soandso", "hello bot")
    if err != nil {
        fmt.Printf("Error: %s\n", err)
    } else {
        fmt.Printf("Reply: %s\n", reply)
    }
}

It also fixes some bugs within RiveScript itself: