Receive Media Messages

Overview

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.

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 .NET 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 an MVC controller to receive messages.

In Visual Studio, create a new project. Use the template for Web Application (Model-View-Controller).

Create an MVC app

Give the project a name — such as ReceiveWhatsapp. Navigate to the controllers directory in the ReceiveWhatsapp project. Create a controller named ReceiveWhatsappController.cs and paste this code.

using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace ReceiceWhatsapp.Controllers
{
    public class ReceiveWhatsappController : Controller
    {
        public IActionResult Index()
        {
            using (var reader = new StreamReader(Request.Body))
            {
                var requestBody = reader.ReadToEnd();
                Webhook webhook = JsonConvert.DeserializeObject<Webhook>(requestBody);

                if (webhook == null)
                {
                    return BadRequest("Invalid JSON");
                }

                string fromNumber = webhook.From;
                string toNumber = webhook.To;
                string contentType = webhook.ContentType;
                string media0 = webhook.Media0;
                string body = webhook.Body;
                string contextMessageUUID = webhook.Context?.MessageUUID;
                string buttonText = webhook.Button?.Text;
                string buttonPayload = webhook.Button?.Payload;

                switch (contentType)
                {
                    case "text":
                        System.Console.WriteLine($"Text Message received - From: {fromNumber}, To: {toNumber}, Text: {body}");
                        break;
                    case "media":
                        System.Console.WriteLine($"Media Message received - From: {fromNumber}, To: {toNumber}, Media Attachment: {media0}, Caption: {body}");
                        break;
                    case "button":
                        System.Console.WriteLine($"Button Message received - From: {fromNumber}, To: {toNumber}, Button Text: {buttonText}, Button Payload: {buttonPayload}");
                        break;
                }

                if (!string.IsNullOrEmpty(contextMessageUUID))
                {
                    System.Console.WriteLine($"Context Message UUID: {contextMessageUUID}");
                }
            }

            return Ok("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.