Overview
This guide shows how to receive and automatically respond to incoming MMS messages on a Plivo number, as you might want to do for someone who’s out of the office or who leaves the company.
Here’s how to use Plivo’s SMS APIs to build this use case.
Prerequisites
To get started, you need a Plivo account — sign up with your work email address if you don’t have one already. To receive incoming messages you must have a Plivo phone number that supports MMS; you can rent numbers from the Numbers page of the Plivo console or by using the Numbers API. If this is your first time using Plivo APIs, follow our instructions to set up a Ruby development environment and a web server and safely expose that server to the internet.
Create a Rails server to receive MMS messages
Change to the project directory and run this command to create a Rails controller for inbound messages.
$ rails generate controller Plivo sms
This command generates a controller named plivo_controller in the app/controllers/ directory and a respective view in the app/views/plivo directory. We can delete the view as we don’t need it.
$ rm app/views/plivo/sms.html.erb
Create the autoresponder application using Rails server
Edit app/controllers/plivo_controller.rb and paste into it this code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
include Plivo
include Plivo::Exceptions
include Plivo::XML
class PlivoController < ApplicationController
skip_before_action :verify_authenticity_token
def receive-mms
from_number = params[:From]
to_number = params[:To]
text = params[:Text]
media_url = params[:Media0]
puts "Message received - From: #{from_number}, To: #{to_number}, Text: #{text}, Media: #{media_url}"
if text.downcase == "hi"
text = "Hello!"
media = ["https://media.giphy.com/media/888R35MJTmDxQfRzfS/giphy.gif"]
elsif text.downcase == "bye"
text = "Bye and have a nice day!"
media = ["https://media.giphy.com/media/QM5lHSyFjz1XW/giphy.gif"]
else
text = "I'm glad that we connected"
media = ["https://media.giphy.com/media/888R35MJTmDxQfRzfS/giphy.gif"]
end
api = RestClient.new("<auth_id>","<auth_token>")
response = api.messages.create(
src:to_number,
dst:from_number,
text:body,
media_urls: media,
type: "mms"
)
render json: response.to_s
end
end
Add a route
Edit config/routes.rb and change the line
Rails.application.routes.draw do
get 'plivo/sms'
end
to
Rails.application.routes.draw do
post 'plivo/receive-mms/' => 'plivo#receivemms'
end
Test
Start the Rails server
$ rails server
You should see your basic server application in action at http://localhost:3000/plivo/receive-mms/.
Replace the auth placeholders with your authentication credentials from the Plivo console
- We recommend that you store your credentials in the auth_id and auth_token environment variables, to avoid the possibility of accidentally committing them to source control. If you do this, you can initialize the client with no arguments and Plivo will automatically fetch the values from the environment variables. You can use os module(os.environ) to store environment variables and fetch them when initializing the client.
- Sending and receiving MMS is only available in the United States and Canada.
Expose your local server to the internet.
Create a Plivo application
Associate the Rails server you created with Plivo by creating a Plivo application. Visiting Messaging > Applications and click Add New Application. You can also use Plivo’s Application API.
Give your application a name — we called our Receive-MMS. Enter the server URL you want to use (for example https://<yourdomain>.com/receive-mms/) in the Message URL field and set the method to POST. Click Create Application to save your application.
Assign a Plivo number to your application
Navigate to the Numbers page and select the phone number you want to use for this application. From the Application Type drop-down, select XML Application. From the Plivo Application drop-down, select Respond-MMS (the name we gave the application).
Click Update Number to save.
Test
Send a text message to the Plivo number you specified using any phone. The message should be replied to the destination number you specified.
Haven’t tried Plivo yet? Getting started is easy and only takes minutes. Sign up today.