whatsapp-api-webhook-server-golang is a library for integration with WhatsApp messenger using the API service green-api.com. You should get a registration token and an account ID in your personal cabinet to use the library. There is a free developer account tariff.
The documentation for the REST API can be found at the link. The library is a wrapper for the REST API, so the documentation at the link above also applies.
To send a message or perform other Green API methods, the WhatsApp account in the phone app must be authorized. To authorize the account, go to your cabinet and scan the QR code using the WhatsApp app.
Go must be installed on the server. Go installation instructions.
Update the system:
sudo apt update
sudo apt upgrade -y
Set up the firewall:
Allow the SSH connection:
sudo ufw allow ssh
Base rules:
sudo ufw default deny incoming
sudo ufw default allow outgoing
Allow HTTP and HTTPS connections:
sudo ufw allow http
sudo ufw allow https
Enable the firewall:
sudo ufw enable
Do not forget to create a module:
go mod init example
Installation:
go get github.com/green-api/whatsapp-api-webhook-server-golang
import (
"github.com/green-api/whatsapp-api-webhook-server-golang/pkg"
)
The WebhookToken attribute is optional.
webhook := pkg.Webhook{
Address: ":80",
Pattern: "/",
}
The StartServer function takes a handler function. The handler function must have 1
parameter (body map[string]interface{}
). When a new notification is received, your handler function will be executed.
Link to example: main.go.
_ := webhook.StartServer(func(body map[string]interface{}) {
fmt.Println(body)
})
go run main.go
Licensed under Attribution-NoDerivatives 4.0 International (CC BY-ND 4.0) terms. Please see file LICENSE.