Python 007: SMS Integration
This will help you:
Learn how to send and receive texts in Python programs
In this activity, you'll learn how to use Twilio, a text messaging API, to send and recieve texts in a Python program. You could use this to send yourself reminders, have conversations with a chatbot on your phone, or send alerts to the users of a program.
Time: 1-3 hours / Level: B2
You should already:
Have a basic understanding of Python
Download the Twilio API and create a free account (see directions in Step 1)
Get the code and resources for this activity by clicking below. It will allow you to download the files from a Google Drive folder. Unzip the folder and save it in a sensible location.
Glossary
API - Application Programming Interface, a way of interacting with an application (usually one hosted online) through a written program.
Auth token - Authorization token, a secret code programmers get in order to use an API.
Virtual Environment - An isolated version of your programming environment, so that installed packages and changes to environment variables don't mess with your computer's operating environment.
Server - a computer where a program connected to the internet is running. If the server stops running the program, it will not be usable.
Localhost - 127.0.0.1, the local IP address of your computer for when it's just talking to itself.
Port - essentially, a channel for communicating over the internet with a certain IP address. Each address has many ports.
IP Address - Internet Protocol address. It's like the phone number for computers (personal/client computers or websites/servers.) However, sometimes the IP address for a computer can change.
Step 1: Warm-up - Register with & download Twilio
Click on this link and register for a free Twilio account. This will allow you to get an active phone number to use in programs, and send a limited number of texts. The free trial account includes a limited amount of credit you can use to send messages. It requires payment to continue using after your credit has run out, but you do not need to add any payment information to use your free trial.
Read the instructions here for setting up your account.
Install Twilio by typing pip install Twilio
in the terminal.
Here is a quickstart guide for using Twilio with Python. It's very helpful, but you do about the same things in the following steps.
Step 2: Warm-up - Sending a text
Open the file send_text.py
and read the comments to understand what is happening. Add your credentials from your Twilio account, and your own phone number as the recipient. You may notice that you must use a Twilio-provided number for the sender, and a number you have validated for the receiver. Then, run it by typing python send_text.py
in the terminal.
Your account SID and auth token are like a secret username and password that you, and only you, use in your own programs to show Twilio's services who you are and prove you can use their services. In general, web APIs (Application Programming Interfaces) use an auth token for each user, so that they can control who is making requests to the application, and how many requests they are making. It takes resources to make the API available and handle requests, and someone making many, many requests in a short period of time could make the service unavailable for other users.
It's not a good idea to post your account SID or auth token anywhere else. If you're using an API in a project you are sharing, here are tips for storing your credentials so that you don't have to frequently add and remove them in your code.
Step 3: Activity - Preparing to receive messages
To receive and handle messages with Twilio, you will need to run a very minimal web app. We're going to use Flask for this, and you can learn more about Flask in the Flask web app activity.
To start, go to this tutorial and follow the steps to set up Flask. It starts by helping you set up a virtual environment, which keeps your environment for a project from changing how your computer's normal operating environment is set up. You can read more about virtual environments here if you are curious.
To activate the virtual environment, source
is not a command in Windows. Instead, after running virtualenv --no-site-packages .
(yes, include the '.') you should run scripts/activate
. If you ever want to deactivate, run:
cd scripts
deactivate
cd ..