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

runtime error: invalid memory address or nil pointer dereference #30

Open distravantari opened 7 years ago

distravantari commented 7 years ago

I try ro copy and paste your example for rivescript session using redis

screen shot 2017-08-17 at 8 13 02 pm

but I keep on getting this error.

screen shot 2017-08-17 at 8 14 13 pm
kirsle commented 7 years ago

Notes to self or whoever wants to tackle this one:

The fix would be: handle the error here. Actually it would probably be better to move the GetHistory() call a little higher up in the code and skip dealing with any of the %Previous checks if the user has no history yet.

    // See if there were any %Previous's in this topic, or any topic related to
    // it. This should only be done the first time -- not during a recursive
    // redirection. This is because in a redirection, "lastReply" is still gonna
    // be the same as it was the first time, resulting in an infinite loop!
+   history, err := rs.sessions.GetHistory(username)
-   if step == 0 {
+   if err != nil && step == 0 {
        allTopics := []string{topic}
        if len(rs.includes[topic]) > 0 || len(rs.inherits[topic]) > 0 {
            // Get ALL the topics!
            allTopics = rs.getTopicTree(topic, 0)
        }
julianfrank commented 7 years ago

Is this corrected in the current release? Or there a work around?

kirsle commented 7 years ago

Not yet.

As a workaround, if you set a user variable before getting a reply, then it will initialize the user's session and it won't raise a nil pointer exception.

This might be a good idea to do anyway, for example to set an "is_admin" variable that you could query from within RiveScript -- if your bot has any need of such things, anyway.

Example:

// Setting a user variable to initialize their session
// will avoid the nil pointer exception when getting
// their history back from the session manager.
bot.SetUservar(username, "is_admin", isAdmin(username))
reply, err := bot.Reply(username, message)
julianfrank commented 7 years ago

I get this panic even after setting uservar :(

http: panic serving 127.0.0.1:52478: assignment to entry in nil map
goroutine 13 [running]:
net/http.(*conn).serve.func1(0xc4200f34a0)
        /usr/local/go/src/net/http/server.go:1721 +0xd0
panic(0x8bbaa0, 0xc4202fecc0)
        /usr/local/go/src/runtime/panic.go:489 +0x2cf
github.com/aichaos/rivescript-go/sessions/memory.(*MemoryStore).GetAll(0xc42017aa60, 0x0)
        /home/jf/go/src/github.com/aichaos/rivescript-go/sessions/memory/memory.go:106 +0x124
github.com/aichaos/rivescript-go.(*RiveScript).GetAllUservars(0xc4201ca0e0, 0xc4202dc99f)
        /home/jf/go/src/github.com/aichaos/rivescript-go/config.go:300 +0x34
main.RawIntentHandler(0xbb1940, 0xc4201ca380, 0xc42000b400)
        <Panic happens here> .go:57 +0x422
net/http.HandlerFunc.ServeHTTP(0x94fbe0, 0xbb1940, 0xc4201ca380, 0xc42000b400)
        /usr/local/go/src/net/http/server.go:1942 +0x44
main.Logger.func1(0xbb1940, 0xc4201ca380, 0xc42000b400)
<srcdir>/src/routehandlers.go:19 +0xcb
net/http.HandlerFunc.ServeHTTP(0xc4202de040, 0xbb1940, 0xc4201ca380, 0xc42000b400)
        /usr/local/go/src/net/http/server.go:1942 +0x44
github.com/gorilla/mux.(*Router).ServeHTTP(0xc4201b1770, 0xbb1940, 0xc4201ca380, 0xc42000b400)
        /home/jf/go/src/github.com/gorilla/mux/mux.go:114 +0x10c
net/http.serverHandler.ServeHTTP(0xc42018c160, 0xbb1940, 0xc4201ca380, 0xc42000b200)
        /usr/local/go/src/net/http/server.go:2568 +0x92
net/http.(*conn).serve(0xc4200f34a0, 0xbb2180, 0xc4202de280)
        /usr/local/go/src/net/http/server.go:1825 +0x612
created by net/http.(*Server).Serve
        /usr/local/go/src/net/http/server.go:2668 +0x2ce

if I try to

swceBot.SetUservar(Username, "username", Username)
userVars, _ := swceBot.GetUservars(Username)
line 57 => x := swceBot.GetAllUservars()