This guide covers how to receive a callback response on a phone number registered to your WhatsApp Business Account (WABA) when a customer clicks on a quick reply button in an interactive message template.
To get started, you need a Plivo account — sign up with your work email address if you don’t have one already. If this is your first time using Plivo APIs, follow our instructions to set up a Go development environment.
You must have an onboarded WhatsApp account to receive inbound messages. If a number is listed as connected, it can receive inbound messages.
Create a file called receive_whatsapp.go
and paste into it this code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"encoding/json"
"fmt"
"net/http"
)
type Webhook struct {
From string `json:"From"`
To string `json:"To"`
ContentType string `json:"ContentType"`
Context struct {
MessageUUID string `json:"MessageUUID"`
} `json:"Context"`
Button struct {
Text string `json:"Text"`
Payload string `json:"Payload"`
} `json:"Button"`
Media0 string `json:"Media0"`
Body string `json:"Body"`
}
func whatsappHandler(w http.ResponseWriter, r *http.Request) {
var webhook Webhook
err := json.NewDecoder(r.Body).Decode(&webhook)
if err != nil {
http.Error(w, "Invalid JSON", http.StatusBadRequest)
return
}
fromNumber := webhook.From
toNumber := webhook.To
context := webhook.Context
switch webhook.ContentType {
case "text":
text := webhook.Body
fmt.Printf("Text Message received - From: %s, To: %s, Text: %s\n", fromNumber, toNumber, text)
case "media":
caption := webhook.Body
fmt.Printf("Media Message received - From: %s, To: %s, Media Attachment: %s, Caption: %s\n", fromNumber, toNumber, webhook.Media0, caption)
case "button":
buttonText := webhook.Button.Text
buttonPayload := webhook.Button.Payload
fmt.Printf("Button Message received - From: %s, To: %s, Button Text: %s, Button Payload: %s\n", fromNumber, toNumber, buttonText, buttonPayload)
}
if context.MessageUUID != "" {
fmt.Printf("Context Message UUID: %s\n", context.MessageUUID)
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("Message Received"))
}
func main() {
http.HandleFunc("/receive_whatsapp/", whatsappHandler)
http.ListenAndServe(":8080", nil)
}
Add or update a webhook URL from this link to a WhatsApp Business Account. Once you’ve done this, you should be able to receive inbound messages.
Send a WhatsApp message to the Plivo number you specified using WhatsApp application.