Multimedia Message Service (MMS) picture and video messages are a common extension to the Short Message Service (SMS) system for sending text messages. Using a web application programming interface (API) with Python makes it easy to send MMS messages from a web application or script. In this short tutorial we'll learn how to add MMS sending capability to a new or existing Python application.
Tools We Need
Either Python 2 or 3 works for the code in this tutorial. Just make sure you have one of those two versions installed on your system by going to the terminal and typing python --version
. The other dependencies for this tutorial include:
- Python version 2 or 3
- pip and virtualenv to handle one application dependency
- A free Twilio account to use their MMS web API
- Twilio Python helper library
If you are unsure of how to get pip and virtualenv installed, take a look at the first few steps of the how to set up Python 3, Flask and Green Unicorn on Ubuntu 16.04 LTS guide.
Twilio Web API
Our simple Python example application will use the Twilio web API to send picture messages. Go to the Twilio website sign up for a free trial account. If you already have a Twilio account (and you should because it makes it easy to add almost any type of communications to applications!) then sign into your existing account.
In trial mode Twilio can send MMS to a validated phone number associated with the account. When you're ready to send MMS messages to any phone in any country then you will have to upgrade your account.
After signing up for a Twilio account, you will receive your own phone number that'll be used to send messages. That phone number can send outbound MMS messages without any configuration. It can also receive messages but that requires modifying the Request URL webhook in the phone number details screen.
Installing Our Dependency
We'll use the twilio helper library as a dependency for our Python code. The helper library can be installed via the pip
command, which pulls the code from PyPI into our local virtualenv. In this tutorial we'll call our virtualenv pymms
but you can name it whatever you want for your application.
We have to create the virtualenv before using it. In your terminal enter:
- virtualenv pymms
If you need to install virtualenv take a look at the how to set up Python 3, Django and Green Unicorn on Ubuntu 16.04 LTS guide.
Activate the virtualenv with the source
command.
- source pymms/bin/activate
The command prompt will change to look like this after it is activated:
Now install the Twilio Python helper library.
- pip install twilio
Once the helper library installs we can use it in our Python code.
Sending MMS From Python
Launch the the Python interpreter by executing the python
command in your terminal. You can also create a new file named send_mms.py
if you want to re-use the code after we give it a try.
We need to grab our account credentials from the Twilio Console to connect our Python code to our Twilio account. Go to the Twilio Console and copy the Account SID and Authentication Token into your Python code.
Enter the following code into the interpreter or into the new Python file.
- # we import the Twilio client from the dependency we just installed
- from twilio.rest import TwilioRestClient
- # the following line needs your Twilio Account SID and Auth Token
- client = TwilioRestClient("ACxxxxxxxxxxxxxx", "zzzzzzzzzzzzz")
- # this is the URL to an image file we're going to send in the MMS
- media = "http://www.mattmakai.com/source/static/img/work/fsp-logo.png"
- # change the "from_" number to your Twilio number and the "to" number
- # to the phone number you signed up for Twilio with, or upgrade your
- # account to send MMS to any phone number that MMS is available
- client.messages.create(to="+19732644152", from_="+12023358536",
- body="MMS via Python? Nice!", media_url=media)
All the lines above that start with #
are comments to give you some context for what each line is doing. After entering that code into the interpreter or running the Python script with python send_mms.py
Twilio will send your MMS.
In a few seconds you should see a message appear on your phone - note that MMS can take a little longer because your phone has to download the image. I use an iPhone so here is what the message looked like when I received it:
That is everything need to send MMS to a phone. Pretty awesome result for a few lines of Python code, right? This code can be added to any Python program to send outbound MMS.
One final note: keep your Twilio Auth Token secret otherwise anyone who gets it will be able to send and receive messages through your account.
Questions? Contact me via Twitter @fullstackpython or @mattmakai. I'm also on GitHub with the username mattmakai.
Something wrong with this post? Fork this page's source on GitHub.
Learn more about these concepts
APIs API Integration Twilio …or view all topics.
This site is based on Matt Makai's project Full Stack Python, thanks for his excellent work!
此网站由 @haiiiiiyun 和 开源爱好者们 共同维护。 若发现错误或想贡献,请访问: Github fullstackpython.cn 项目