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

Topic Timeout #42

Closed rmasci closed 4 years ago

rmasci commented 5 years ago

I have a chatbot running, and I have programming that allows a user to start a troubleshooting session, and each step is a topic. The problem is they may go away, and then come back hours later and try to ask the chatbot another question -- but they're stuck inside the topic they left on which may be a few days ago. Is there a way to time out a topic? So that ten minutes later it goes back to topic=random?

kirsle commented 5 years ago

Hi,

There isn't a built-in feature for this but here's a couple ideas:

Use a goroutine in the background to handle the timeout

Something like,

go func() {
    for {
        time.Sleep(5 * time.Minute)
        users := bot.GetAllUservars()
        for _, username := range users {
            if timestamp, err := bot.GetUservar(username, "lastSeen"); err == nil {
                ts, _ := time.Parse(time.RFC3339, timestamp) // like "2006-01-02T15:04:05Z"
                if time.Now().Sub(ts) > (30 * time.Minute) {
                    // user has expired
                    bot.ClearUservars(username)
                }
            }
        }
    }
}()

// ...

func GetReply(username, message string) (string, error) {
    reply, err := bot.Reply(username, message)
    bot.SetUservar(username, "lastSeen", time.Now().Format(time.RFC3339)
    return reply, err
}

Using Redis: add this feature to rivescript-go/sessions/redis and send a PR

Auto-expiring data is something Redis is really good at, and there's already a Redis-backed session driver for user variable storage, but it doesn't have a feature to do this yet. If you're up for it, you could add this feature to the Redis session driver and send me a pull request (assuming you'd want to use Redis at all for your bot).

That change might look like:

rmasci commented 4 years ago

I put somethign like this in place where I store the date they went in to a topic, then every message I get from that user i check the time, If 5 min has gone past without a message it reverts back to no topic. Thanks!!