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 Ruby 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 Rails controller to receive messages.

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

$ rails generate controller Plivo whatsapp

This command generates a controller named plivo_controller in the app/controllers/ directory and a respective view in app/views/plivo. We can delete the view as we don’t need it.

$ rm app/views/plivo/whatsapp.html.erb

Edit the app/controllers/plivo_controller.rb file and paste this code into the PlivoController class.

include Plivo
include Plivo::XML

class PlivoController < ApplicationController
def inbound
    from_number = params[:From]
      to_number = params[:To]
      text = params[:Body]
      media = params[:Media0]
      puts ""Message received - From: #{from_number}, To: #{to_number}, Media Attachment: #{media}""

      if text
        puts "Text: #{text}"
  end
end

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.