Receive Media Messages

Overview

This guide covers how to receive WhatsApp media messages when a user sends it to your registered phone number.

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;
require '../../vendor/autoload.php';
use Plivo\RestClient;
use Illuminate\Http\Request;

class WhatsappController extends Controller
{
    public function receiveWhatsapp()
    {
        $from_number = $_REQUEST[""From""];
        $to_number = $_REQUEST[""To""];
        $media0 = $_REQUEST[""media0""] ?? '';
        $text = $_REQUEST[""Body""] ?? '';

        echo ""Message received - From: $from_number, To: $to_number, Media Attachment: $media0"";

        if ($text != '') {
            echo ""Text: $text"";
        }
    }
}

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.