Receive Media Messages

Overview

This guide covers how to receive WhatsApp media messages on a phone number registered to your WhatsApp Business Account.

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 com.plivo.api.exceptions.PlivoXmlException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class PlivoWhatsappApplication {

    public static void main(String[] args) {
        SpringApplication.run(PlivoWhatsappApplication.class, args);
    }

    @GetMapping(value = "/reply_whatsapp/")
    public String getBody(
            @RequestParam String From,
            @RequestParam String To,
            @RequestParam String Media0,
            @RequestParam(name = "Body", required = false) String Body,
    ) throws PlivoXmlException {
        System.out.printf("Media Message Received - From: %s, To: %s, Media Attachment: %s\n", From, To, Media0);

        if (Body != null) {
            System.out.printf("Text: %s\n", Body);
        }

        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.