Short Message Service (SMS) text messages are ubiquitous for communication all over the world. It is easy to send SMS text messages from a Python application using a web application programming interface (API). Let's take a look at the tools we need to quickly add SMS capability to our Python apps.
This guide works with both Python 2 and 3, so make sure you have one of those two versions installed.
If you need assistance getting pip and virtualenv installed, check out the first few steps of the how to set up Python 3, Flask and Green Unicorn on Ubuntu 16.04 LTS guide that'll show how to install system packages for those tools.
We're going to use a web API to make sending SMS easier and more reliable. Head to the Twilio website and sign up for a free trial account. If you already have a Twilio account (and you should - it's awesome for more than just sending text messages!) then sign into your existing account.
The Twilio trial account allows you to send text messages to your own validated phone number. When you want to send SMS to any phone number in your country or other countries then you can upgrade your account to send messages for fractions of a cent.
After signing up, you will get a free phone number in your country. We can use that phone number without any configuration to send outbound text messsages. You can also receive text messages but that requires changing the Request URL webhook in the phone number configuration screen - we'll cover that in a future blog post.
Our code will use a helper library to make it easier to send text messages from Python. We are going to install the helper library from PyPI into a virtualenv. First we need to create the virtualenv. In your terminal use the following command to create a new virtualenv. If you need to install virtualenv take a look at the how to set up Python 3, Flask and Green Unicorn on Ubuntu 16.04 LTS guide.
virtualenv sendsms
Activate the virtualenv.
source sendsms/bin/activate
The command prompt will change after we properly activate the virtualenv to something like this:
Now install the Twilio Python helper library.
pip install twilio
The helper library is now installed and we can use it with the Python code we create and execute.
Fire up the Python interpreter in the terminal using the python
command,
or create a new file named send_sms.py
.
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")
# 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 SMS to any phone number
client.messages.create(to="+19732644152", from_="+12023358536",
body="Hello from Python!")
All the lines above that start with #
are comments. Once you enter that
code into the interpreter or run the Python script using
python send_sms.py
the SMS will be sent.
In a few seconds you should see a message appear on your phone. I'm on iOS so here's how the text message I received looked.
That's it! You can add this code to any Python code to send text messages. Just keep your Auth Token secret as it'll allow anyone that has it to use your account to send and receive messages.
Questions? Contact me via Twitter @fullstackpython or @mattmakai. I'm also on GitHub with the username mattmakai.
See something wrong in this post? Fork this page's source on GitHub and submit a pull request.