Your company has settled on Plivo to handle its voice and messaging communications, and now it’s your job to start integrating Plivo into your company’s applications. Don’t worry — Plivo has an SDK to help you out. Let’s see how to send and receive MMS through Plivo in a Python application.
Install the Plivo Python SDK
We’ll presume you already have Python installed. Installing the Plivo SDK is as simple as running
$ pip install plivo
If you prefer to install from source code, visit our Quickstart Guide for instructions.
Find your Auth ID and Auth Token
You have to have proper credentials before you can use the Plivo API. We provide an Auth ID and Auth Token in the Account section at the top of the overview page of the Plivo console.
Choose a phone number
You need an MMS-enabled Plivo phone number to send MMS to the US and Canada, the two countries where Plivo supports sending MMS. Check the Phone Numbers screen of your Plivo console to see what numbers you have available and which of them support MMS capabilities. You can also buy numbers from this screen.
Send an MMS message
Now you’re ready to start. Create a file called send_mms.py and paste in this code:
from flask import Flask, Response from plivo import plivo app = Flask(__name__) @app.route('/send_mms/', methods=['GET', 'POST']) def outbound_mms(): client = plivo.RestClient('<auth_id>','<auth_token>') response = client.messages.create( src='+14156667777', dst='+14156667778', media_ids=['801c2056-33ab-499c-80ef-58b574a462a2'], text='Hello, MMS from Python!', media_urls=['https://media.giphy.com/media/26gscSULUcfKU7dHq/source.gif'], type_='mms') return Response(response.to_string()) if __name__ == '__main__': app.run(host='0.0.0.0', debug=True)
Replace the placeholders auth_id and auth_token with actual values from your Plivo Console. Save the file and run it with the command
$ python send_mms.py
Note: If you’re using a Plivo trial account, you can send messages only to phone numbers that have been verified with Plivo. You can verify a phone number using the Sandbox Numbers page of the console. Also, if you want to upload your media files to Plivo and use them, you can upload the file on the Messaging > MMS Media Upload page of the console.
Receive an MMS message
Of course, sending messages is only half of the equation. Plivo supports receiving SMS text messages in many countries (see our SMS API coverage page, and click on the countries you’re interested in). When someone sends an SMS message to a Plivo phone number, you can receive it on your server by using a Flask web app. Plivo will send the message along with other parameters, including the Media_URL(s), to your Message URL.
First, optionally, set up a virtual environment to keep these packages isolated from others on your system. Then create a file called receive_mms.py (or whatever name you like) with this code in it:
from flask import Flask, request app = Flask(__name__) @app.route('/receive_mms/', methods=['GET', 'POST']) def inbound_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('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)
Run it with the command
$ python receive_sms.py
You should then be able to see your basic server app in action on http://localhost:5000/receive_mms/.
That’s fine for testing, but it’s not much good if you can’t connect to the internet to receive incoming messages and handle callbacks. For that, we recommend using ngrok, which exposes local servers behind NATs and firewalls to the public internet over secure tunnels. Install it and run ngrok on the command line, specifying the port that hosts the application on which you want to receive messages:
./ngrok http [portnum]
Ngrok will display a forwarding link that you can use to access your local server using the public network.
Now you can create an application to receive MMS messages (follow our Quickstart guide for details).
Haven’t tried Plivo yet? Getting started is easy and only takes five minutes. Sign up today.