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:
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.
In Visual Studio, create a new project. Use the template for Web Application (Model-View-Controller).
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.Linq;
namespace ReceiceWhatsapp.Controllers
{
[ApiController]
[Route("[controller]")]
public class ReceiveWhatsappController : ControllerBase
{
[HttpPost]
public IActionResult ReceiveMessage([FromBody] JObject data)
{
string fromNumber = data["From"].ToString();
string toNumber = data["To"].ToString();
string contentType = data["ContentType"].ToString();
switch (contentType)
{
case "text":
Console.WriteLine($"Text Message - From: {fromNumber}, To: {toNumber}, Text: {data["Body"]}");
break;
case "media":
Console.WriteLine($"Media Message - From: {fromNumber}, To: {toNumber}, URL: {data["Media0"]}, Caption: {data["Body"]}");
break;
case "button":
Console.WriteLine($"Button Message - From: {fromNumber}, To: {toNumber}, Text: {data["Button"]["Text"]}, Payload: {data["Button"]["Payload"]}");
break;
case "interactive":
JObject interactive = (JObject)data["Interactive"];
if (interactive["Type"].ToString() == "button_reply") {
JObject buttonReply = (JObject)interactive["ButtonReply"];
Console.WriteLine($"Interactive Button - From: {fromNumber}, To: {toNumber}, ID: {buttonReply["Id"]}, Title: {buttonReply["Title"]}");
} else if (interactive["Type"].ToString() == "list_reply") {
JObject listReply = (JObject)interactive["ListReply"];
Console.WriteLine($"Interactive List - From: {fromNumber}, To: {toNumber}, ID: {listReply["Id"]}, Title: {listReply["Title"]}, Description: {listReply["Description"]}");
}
break;
case "location":
JObject location = (JObject)data["Location"];
Console.WriteLine($"Location - From: {fromNumber}, To: {toNumber}, Latitude: {location["Latitude"]}, Longitude: {location["Longitude"]}, Name: {location["Name"]}, Address: {location["Address"]}");
break;
}
return Ok("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.