Receive Inbound Messages and Interactions

Overview

This guide explains how to handle webhook responses when you receive inbound messages or interactions from your users on your WhatsApp number. The following use cases are covered in the code snippets below:

  • Your WhatsApp number is receiving a text message.
  • Your WhatsApp number is receiving a media message.
  • Your WhatsApp number is receiving a location message.
  • Your WhatsApp number is receiving a response when a user clicks a quick reply button sent in an interactive templated message.
  • Your WhatsApp number is receiving a response when a user clicks an interactive button message you sent.
  • Your WhatsApp number is receiving a response when a user clicks an interactive list message you sent.

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 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.

Create a Spring application to receive 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 org.json.JSONObject;
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) {
        JSONObject data = new JSONObject(payload);

        String fromNumber = data.getString("From");
        String toNumber = data.getString("To");
        String contentType = data.getString("ContentType");

        switch (contentType) {
            case "text":
                System.out.printf("Text Message - From: %s, To: %s, Text: %s\n", fromNumber, toNumber, data.getString("Body"));
                break;
            case "media":
                System.out.printf("Media Message - From: %s, To: %s, URL: %s, Caption: %s\n", fromNumber, toNumber, data.optString("Media0", ""), data.optString("Body", ""));
                break;
            case "button":
                JSONObject button = data.getJSONObject("Button");
                System.out.printf("Button Message - From: %s, To: %s, Text: %s, Payload: %s\n", fromNumber, toNumber, button.getString("Text"), button.getString("Payload"));
                break;
            case "interactive":
                JSONObject interactive = data.getJSONObject("Interactive");
                if ("button_reply".equals(interactive.getString("Type"))) {
                    JSONObject buttonReply = interactive.getJSONObject("ButtonReply");
                    System.out.printf("Interactive Button - From: %s, To: %s, ID: %s, Title: %s\n", fromNumber, toNumber, buttonReply.getString("Id"), buttonReply.getString("Title"));
                } else if ("list_reply".equals(interactive.getString("Type"))) {
                    JSONObject listReply = interactive.getJSONObject("ListReply");
                    System.out.printf("Interactive List - From: %s, To: %s, ID: %s, Title: %s, Description: %s\n", fromNumber, toNumber, listReply.getString("Id"), listReply.getString("Title"), listReply.getString("Description"));
                }
                break;
            case "location":
                JSONObject location = data.getJSONObject("Location");
                System.out.printf("Location - From: %s, To: %s, Latitude: %s, Longitude: %s, Name: %s, Address: %s\n", fromNumber, toNumber, location.getString("Latitude"), location.getString("Longitude"), location.getString("Name"), location.getString("Address"));
                break;
        }

        return "Message Received";
    }
}

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.