mileusna / viber

Viber messaging/chatbot for Go/Golang
MIT License
31 stars 34 forks source link

Fix runtime error on v.client.Do() #2

Closed foxcool closed 6 years ago

foxcool commented 6 years ago

Error catched on sending message after webhook handling.

panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x28 pc=0x96f422]

When we handle webhooks from viber, we create instance by this way:

v := &viber.Viber{
        AppKey: token,
        Sender: viber.Sender{
            Name:   "MyPage",
            Avatar: "https://mysite.com/img/avatar.jpg",
        },
        //Message:   msgReceivedFunc,
        //Delivered: deliveredFunc,
}

May be this is bad way, and we need to use .New() method everywhere, beause v.client is nil.

My fix: just check and init &http.Client{} if client is nil.

mileusna commented 6 years ago

Yes, it looks like that I have experimenting something with http client and only partially pushed something to GitHub. I do have exactly the same lines in my code, but uncommitted. O:)

I will check my code again and do it right or include your changes, or revers it back while I was using http.DefaultClient. I had some issues and I thought it was because of the viber client, but it turned out that problem has nothing to do with viber package.

mileusna commented 6 years ago

Now I was curious how my app on top of this package works since I use GitHub code while building on target server, but it turns out that I'm using viber.New() which do initialize http.Client. :)

mileusna commented 6 years ago

I have accepted your code, also pushed some of my commits so please update. SetRequestTimeout() also sufferered from the same bug if client was nil.

Thanks 👍