Receive MMS using Python
Sending an outbound MMS is only one way to deliver your message to a recipient; in order to increase the interaction, two-way communication is required. In this guide, we’ll show you how to receive MMS on a Plivo phone number.
Start your implementation with Plivo to receive MMS using PHLO or the traditional API way. PHLO allows you to create and deploy your flows using its intuitive canvas in few clicks. Refer to the instructions from the respective tabs below to start your integration with PHLO or API as you wish.
To receive incoming MMS, you can create and deploy a PHLO with a few clicks on the PHLO canvas. PHLO also lets you visually construct your entire use-case. With PHLO, you only pay for MMS you send/receive, and building with PHLO is free. You can refer to the below instructions in the Using PHLO tab to begin your implementation.
In this section, we will guide you to create a PHLO to Receive MMS.
- Create a Plivo Account(if you don’t have one already): You can sign up with your work email address and complete the phone verification step using your mobile number.
- Plivo Phone Number(Optional): To receive MMS, you must have a Plivo phone number that supports MMS. Numbers can be purchased from the Numbers section of your Plivo Console and use the same as the source number/from number for the outbound MMS. This number will also help you receive incoming MMS as you must have a MMS-enabled Plivo phone number to do the same. Please note that you can also purchase numbers using the Numbers API.
Create the PHLO
You can create a PHLO by referring to the below instructions to Receive MMS:
- On the side navigation bar, click PHLO. The PHLO page will appear and display your existing PHLOs, if any exist. If this is your first PHLO, then the PHLO page will be empty.
- Click Create New PHLO to build a new PHLO.
- In the Choose your use-case window, click Build my own. The PHLO canvas will appear with the Start node.Note: The Start node is the starting point of any PHLO. You can choose between the four available trigger states of the start node; Incoming SMS, Incoming Call, and API Request. For this PHLO, we will use the Incoming Message trigger state.
- From the list of components on the left-hand side, drag and drop the HTTP Request component onto the canvas. This will add a HTTP Request node onto the canvas.
- Connect the Start node with the HTTP Request node, using the Incoming Message trigger state.
- Configure the HTTP Request node to send the details to your Web Server.
- Once you have configured the node, click Validate to save the configurations.
- After you complete the configurations, provide a recognizable name for your PHLO and click Save. Your PHLO is now ready.
Assign the PHLO to a Plivo Number for Incoming MMS
Once you have created and configured your PHLO, assign your PHLO to a Plivo number.
To assign a PHLO to a number
- Log in to your Plivo Console
- On the Product Navigation bar, click Phone Numbers.
- On the Numbers page, under Your Numbers, click the phone number you wish to use for the PHLO.
- In the Number Configuration window, select PHLO from the Application Type list.
- From the PHLO Name list, select the PHLO you wish to use with the phone number, and then click Update Number.
If you have not purchased a phone number yet, you can buy a phone number(s) by referring to the instructions available here.
Test and Validate
You can now receive a message to your Plivo phone number to receive an incoming MMS and see how the inbound MMS is handled using PHLO.
To receive your first incoming MMS, you can refer to the instructions in the following section to begin your implementation.
In this section, we will guide you in setting up an app to receive a MMS on your Plivo number.
- Create a Plivo Account (if you don’t have one already): You can Sign up with your work email address and complete the phone verification step using your mobile number.
- Buy a Plivo Number: You must have a mms-enabled Plivo phone number if you are willing to receive incoming MMS. You can purchase numbers from the Numbers section of your Plivo Console. It is also possible to purchase numbers using the Numbers API.
Set Up a Flask server for Incoming MMS
In this section, we’ll walk you through how to set up a Flask server in under five minutes and start handling incoming MMS.
Install Plivo Python Package
Create a project directory, run the following command:
$ mkdir mypythonapp
Change the directory to our project directory in the command line:
$ cd mypythonapp
Install the SDK using pip
$ pip install plivo
Alternatively, you can download the source code from this repo and run
$ python setup.py install
Alternatively, you can download the source code from this repo(beta branch) and run
$ python setup.py install
We recommend that you use virtualenv to manage and segregate your Python environments, instead of using
sudo with your commands and overwriting dependencies.
Create a Flask App for Incoming MMS
Use the following code snippet to start a local server.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 from flask import Flask, request app = Flask(__name__) @app.route('/receive_sms/', methods=['GET', 'POST']) def inbound_sms(): from_number = request.values.get('From') to_number = request.values.get('To') text = request.values.get('Text') media_url = request.values.get('Media0') print('Message received - From: %s, To: %s, Text: %s, Media: %s' %(from_number, to_number, text, media_url)) return 'Message Recevived' if __name__ == '__main__': app.run(host='0.0.0.0', debug=True)
Save this code in any file (name the file something like
receive_mms.py). To run this file on the server, go to the folder where this file resides and use the following command:
$ python receive_mms.py
And you should see your basic server app in action on http://localhost:5000/receive_mms/
Exposing your local server to the internet
To receive Incoming MMS and to handle callbacks, your local server should be able to connect with Plivo API service, Ngrok is a tunneling software used to expose a web server running on your local machine to the internet. Using Ngrok you can set webhooks which can talk to Plivo server.
You can download and install ngrok from here. Follow the detailed configuration instructions to get started.
Run ngrok on the port which currently hosts your application. For example, if your port number is 8000, run the following command:
./ngrok http 8000
This will give you a UI with links that look like ngrok.io/* which you can use to access your local server using the public network.
You can check the app in action on https://6ea358b0f703.ngrok.io/receivesms/
Create an Application
- Create an Application by visiting the Application Page and click on
New Application. You can also use Plivo’s Application API.
- Give your application a name. Let’s call it
Receive MMS. Enter your server URL (e.g.
http://example.com/receivesms/) in the
Message URLfield and set the method as
POST. See our Application API docs to learn how to modify your application through our APIs.
- Click on
Create Applicationto save your application.
Assign a Plivo number to your app
- Navigate to the Numbers page and select the phone number you want to use for this app.
Receive MMS(name of the app) from the Plivo App dropdown list.
- Click on
Test and validate
Receive MMS to your Plivo number using a regular mobile phone. Plivo will send a request to your
Message URL with the parameters listed in the Messages Documentation.
Common Use Cases for Receive MMS
The majority of the use cases include an Receive MMS. For example,MMS Surveys, MMS chat and so on.