Receive Response from a Quick Reply Button in an Interactive Templated Message

Overview

This guide covers how to receive webhook response when a user clicks on a quick reply button in an interactive templated message.

Prerequisites

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 Go server to receive messages.

Create a file called receive_whatsapp.go and paste into it this code.

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)
}

Configure a webhook URL in your WhatsApp Business Account

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.

Test

Send a WhatsApp message to the Plivo number you specified using WhatsApp application.