This is fully documented and easy to use go-bindings for working with the Bot Viber API.
Please read the Official Viber API Documentation before starting.
Download the latest version of the Bot API.
go get -u github.com/go-microbot/viber
Create your own bot token. Follow the Official guide.
Each callback will contain a signature on the JSON passed to the callback. The signature is HMAC with SHA256 that will use the authentication token as the key and the JSON as the value. The result will be passed as HTTP Header X-Viber-Content-Signature
so the receiver can determine the origin of the message.
For more information see Setting a Webhook article.
package main
import (
"context"
"fmt"
"github.com/go-microbot/viber/api"
apiModels "github.com/go-microbot/viber/api/models"
"github.com/go-microbot/viber/bot"
"github.com/go-microbot/viber/models"
)
const token = "<PASTE_YOUR_TOKEN_HERE>"
func main() {
// init Bot API with token.
botAPI := api.NewViberAPI(token)
// create Bot instance.
myBot := bot.NewViberBot(&botAPI)
// start listening.
go myBot.WaitForUpdates(bot.NewWebhookStrategy(bot.WebhookConfig{
ServeURL: "localhost:8443", // server to catch Viber requests.
// if you want to validate each callback signature add these parameters as well.
// More info: https://developers.viber.com/docs/api/rest-bot-api/#callbacks.
VerifySignature: true,
SignatureKey: token,
}))
// setup Webhook.
go func() {
whResp, err := botAPI.SetWebhook(context.Background(), apiModels.SetWebhookRequest{
URL: "https://03322284e668.ngrok.io", // use your website URL (SSL required).
})
if err != nil {
panic(err)
}
if whResp.Status != models.ResponseStatusCodeOK {
panic(fmt.Sprintf("request to set webhook returned unexpected status: %d - %s", whResp.Status, whResp.StatusMessage))
}
}()
// listen Bot's events.
events, errs := myBot.Callbacks()
for {
select {
case event, ok := <-events:
if !ok {
fmt.Println("events channel closed")
return
}
switch event.Event {
case models.EventTypeWebhook:
fmt.Println("webhook successfully installed")
case models.EventTypeMessage:
// send "hello" message.
_, err := myBot.API().SendTextMessage(context.Background(), apiModels.SendTextMessageRequest{
GeneralMessageRequest: apiModels.GeneralMessageRequest{
Receiver: event.Sender.ID,
Type: models.MessageTypeText,
Sender: apiModels.MessageSender{
Name: "Greeting bot",
},
},
Text: fmt.Sprintf("Hello, %s!", event.Sender.Name),
})
if err != nil {
fmt.Printf("could not send message to user: %v", err)
}
}
case err, ok := <-errs:
if !ok {
fmt.Println("errors channel closed")
return
}
fmt.Println(err)
}
}
}
Viber doesn’t support self signed certificates.
See the examples folder to get all available examples.
To run tests locally please specify the TEST_BOT_TOKEN
env variable. It should contains your bot token.
Use the following command:
go test -p 1
Or use Makefile's test
command:
make test
Use the following commands:
golangci-lint cache clean
golangci-lint run --config .golangci.yml --timeout=5m
Or use Makefile's lint
command:
make lint