How to Receive and Respond to Incoming MMS Messages in Python with Flask and Plivo

How to Receive and Respond to Incoming MMS Messages in Python with Flask and Plivo

You can 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 API 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 Python development environment and a web server and safely expose that server to the internet.

Create a Flask server to receive MMS messages

Create a file called receive_mms.py 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
36
37
38
39
40
41
42
from flask import Flask, request
import plivo

app = Flask(__name__)


@app.route("/receive-mms/", methods=["GET", "POST"])
def receive_mms():
   # Receive MMS
   from_number = request.values.get("From")
   to_number = request.values.get("To")
   text = request.values.get("Text")
   media_url = request.values.get("Media0")
   print("MMS message received - From: %s, To: %s, Text: %s, Media: %s"% (from_number, to_number, text, media_url))

   if text.lower() == "hi":
       text = "Hello!!"
       media = ["https://media.giphy.com/media/888R35MJTmDxQfRzfS/giphy.gif"]

   elif text.lower() == "bye":
       text = "Bye and have a nice day!"
       media = ["https://media.giphy.com/media/QM5lHSyFjz1XW/giphy.gif"]
   else:
       text = "I am glad that we connected"
       media = ["https://media.giphy.com/media/888R35MJTmDxQfRzfS/giphy.gif"]

   # Reply recipient using Send MMS API
   client = plivo.RestClient("<auth_id>", "<auth_token>")
   response = client.messages.create(
       src=to_number,
       dst=from_number,
       text=text,
       media_urls=media,
       type_="mms"
   )
   print(response)

   return "MMS responded", 200


if __name__ == "__main__":
   app.run(host="0.0.0.0")

Replace the auth placeholders with your authentication credentials from the Plivo console

Note: 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.

Test

Save the file and run it.

$ python receive_mms.py

You should see your basic server application in action at http://localhost:5000/receive-mms/.

Note: 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 Flask 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.

Create 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).

Assign Application to Plivo Phone Number

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.

Note: If you’re using a Plivo Trial account, you can make calls only to phone numbers that have been verified with Plivo. You can verify (sandbox) a number by going to the console’s Phone Numbers Sandbox Numbers page.

Haven’t tried Plivo yet? Getting started is easy and only takes minutes. Sign up today.

Get Volume Pricing

Thousands of businesses in more than 220 countries trust Plivo’s cloud communications platform

The best communications platform forthe world’s leading entertainment service

Frequently asked questions

No items found.
footer bg

Subscribe to Our Newsletter

Get monthly product and feature updates, the latest industry news, and more!

Thank you icon
Thank you!
Thank you for subscribing
Oops! Something went wrong while submitting the form.