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 Java 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.
Use Spring Initializer to create a boilerplate project called Plivo WhatsApp. Open the file PlivoWhatsappApplication and paste this code.
package com.example.Plivo.Whatsapp;
import com.plivo.api.exceptions.PlivoXmlException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class PlivoWhatsappApplication {
public static void main(String[] args) {
SpringApplication.run(PlivoWhatsappApplication.class, args);
}
@PostMapping(value = "/receive_whatsapp/")
public String inboundWhatsapp(@RequestBody String payload) throws PlivoXmlException {
JSONObject data = new JSONObject(payload);
String fromNumber = data.getString("From");
String toNumber = data.getString("To");
String contentType = data.getString("ContentType");
if ("text".equals(contentType)) {
String text = data.getString("Body");
System.out.printf("Text Message received - From: %s, To: %s, Text: %s\n", fromNumber, toNumber, text);
} else if ("media".equals(contentType)) {
String mediaAttachment = data.optString("Media0", "");
String caption = data.optString("Body", "");
System.out.printf("Media Message received - From: %s, To: %s, Media Attachment: %s, Caption: %s\n", fromNumber, toNumber, mediaAttachment, caption);
} else if ("button".equals(contentType)) {
JSONObject button = data.getJSONObject("Button");
String buttonText = button.getString("Text");
String buttonPayload = button.getString("Payload");
System.out.printf("Button Message received - From: %s, To: %s, Button Text: %s, Button Payload: %s\n", fromNumber, toNumber, buttonText, buttonPayload);
}
JSONObject context = data.optJSONObject("Context");
if (context != null) {
String contextMessageUUID = context.optString("MessageUUID", "");
System.out.printf("Context Message UUID: %s\n", contextMessageUUID);
}
return "Message Received";
}
}
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.