package viber

import (
	"bytes"
	"encoding/json"
	"io/ioutil"
	"net/http"
)

// PostData to viber API
func (v *Viber) PostData(url string, i interface{}) ([]byte, error) {
	b, err := json.Marshal(i)
	if err != nil {
		return nil, err
	}

	Log.Println("Post data:", string(b))

	req, err := http.NewRequest("POST", url, bytes.NewBuffer(b))
	req.Header.Add("X-Viber-Auth-Token", v.AppKey)
	req.Close = true

	resp, err := v.client.Do(req)
	if err != nil {
		return nil, err
	}

	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}

	return body, nil
}

package viber

import (
	"bytes"
	"encoding/json"
	"io/ioutil"
	"net/http"
)

// PostData to viber API
func (v *Viber) PostData(url string, i interface{}) ([]byte, error) {
	b, err := json.Marshal(i)
	if err != nil {
		return nil, err
	}

	Log.Println("Post data:", string(b))

	req, err := http.NewRequest("POST", url, bytes.NewBuffer(b))
	req.Header.Add("X-Viber-Auth-Token", v.AppKey)
	req.Close = true
	
	if v.client == nil {
		v.client = &http.Client{}
	}

	resp, err := v.client.Do(req)
	if err != nil {
		return nil, err
	}

	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}

	return body, nil
}

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

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.