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 PHP 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 Laravel controller to receive messages.

Change to the project directory and run this command to create a Laravel controller for inbound messages.

$ php artisan make:controller WhatsappController

This command generates a controller named WhatsappController in the app/http/controllers/ directory. Edit the app/http/controllers/WhatsappController.php file and paste this code.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class WhatsappController extends Controller
{
    public function receiveWhatsapp(Request $request)
    {
        $webhookData = $request->getContent();

        $webhook = json_decode($webhookData, true);

        $from_number = $webhook['From'];
        $to_number = $webhook['To'];
        $content_type = $webhook['ContentType'];
        $context = $webhook['Context'];
        $button = $webhook['Button'];
        $media0 = isset($webhook['Media0']) ? $webhook['Media0'] : '';
        $body = $webhook['Body'];

        switch ($content_type) {
            case 'text':
                echo "Text Message received - From: $from_number, To: $to_number, Text: $body";
                break;
            case 'media':
                echo "Media Message received - From: $from_number, To: $to_number, Media Attachment: $media0, Caption: $body";
                break;
            case 'button':
                $button_text = $button['Text'];
                $button_payload = $button['Payload'];
                echo "Button Message received - From: $from_number, To: $to_number, Button Text: $button_text, Button Payload: $button_payload";
                break;
        }

        if (!empty($context) && isset($context['MessageUUID'])) {
            $context_message_uuid = $context['MessageUUID'];
            echo "Context Message UUID: $context_message_uuid";
        }

        http_response_code(200);
        echo '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.