Text messaging (Twilio API) + Raspberry Pi
The goal of the following guide is to help you be able to send and receive text messages using a raspberry pi.
Step 1: Sign up for a Twilio account and get a phone number
If you want to send a text message from Python, Twilio is a great tool. After you sign up, you will be provided with a Twilio phone number. Note that the free version of Twilio will allow to send text messages only to the phone number that you signed up with. If you want to be able to send messages to other numbers, you will have to add funds to your Twilio account.
Step 2: Install the Twilio helper library
If you are using a raspberry pi, it is very likely that you have Python installed already. However, it is better to have an updated Python library before proceeding with installing the Twilio helper library.
You can update your Python library by typing this into your command line:
sudo apt-get update python3
Next, you need to install the Twilio helper library.
The easiest way to install the library is by simply running this in your command line:
sudo pip install twilio
If you get a “pip: command not found” error, run this instead:
sudo easy_install twilio
If you would prefer to install it manually, download the source code for twilio-python with this link:
Then install it by running:
sudo python setup.py install
Step 2: Send an outbound text message
Now you are able to send a text message from the number purchased from Twilio. Here’s how to do it: Create a new file and give it a name. Let’s call our file file_name.py for the purpose of this guide:
sudo nano file_name.py
Next, copy or paste the code found here: https://github.com/TwilioDevEd/api-snippets/blob/master/quickstart/python/sms/example-1/send_notifications.6.x.py
Into your file. Before you run it, swap the placeholder values for account_sid and auth_token with your personal Twilio credentials. Go to https://www.twilio.com/console and log in. On this pafe, you will findyour credentials, which you will need any time you send messages through the Twilio client like this. You can revel your auth Token by clicking on the eyeball icon.
Also, remember to replace the “from_” and the “to” phone numbers making sure to use this formatting: [+][country code][phone number including area code].
Save your changes and exit the file. To test if your program works, run this in your command line:
sudo python file_name.py
You should receive a message on your phone number.
Step 3: Install Flask and set up your development environment
To receive and reply to messages, you need to create a web application that can accept incoming requests, which is why we need Flask.
1) install pip and Virtualenv
To install Flask and set up your development environment, you will need two things: pip to install Flask and virtualenv to create a unique sandbox for this project.
For python 2.4, run the following in your command line:
sudo easy_install virtualenv
For python 2.5 – 2.7, run this:
sudo easy_install -2.7 virtualenv
Replace the 2.7 with 2.5 or 2.6 if you have that version installed. (To figure out which python you have installed run: sudo python version)
For python 3.4+, run this:
sudo pip install virtualenv
2) create and activate your virtual environment
After installing virtualenv, use your command line to go to the directory you are using for this guide and create a virtual environment. Run this:
cd twilio_guide_folder sudo virtualenv --no-site-packages
Activate the virtual environment. Run this:
sudo source bin/activate
If you are interested in learning more about virtualenv, visit here: http://docs.python-guide.org/en/latest/dev/virtualenvs/#lower-level-virtualenv
3) Install dependencies
Install Flask. Create a new file and give it a name. For the purpose of this guide, we will call our file dependencies.txt. Add the following lines to it:
To install both of these packages with pip in your command line, run this:
sudo bin/pip install –r dependencies.txt
4) Test everything
Before testing, make sure your virtualenv is activated. Run this in your command line: cd twilio_guide_folder sudo source bin/activate
Then, create and open a file called run.py:
sudo nano run.py
Add these lines:
from flask import Flask app = Flask(__name__) @app.route("/") def hello():
return "Hello World!"
if __name__ == "__main__":
Save and exit the file. Now you can try running your file. In your command line, type:
sudo python run.py
You should see:
- Running on http://127.0.0.1:5000/
If you navigate to http://127.0.0.1:5000/ in your browser , you should see a “Hello World!” message.
5) Allow Twilio to talk to your Flask application
When Twilio receives an SMS, it reaches out to a URL in the application that you have created for instructions on how to handle the message. When you’re working on your Flask app in your development environment, your app is only reachable by other programs on the pi, therefore Twilio will not be able to talk to it. You need to make your application accessible over the internet. There are a lot of ways to do this, but we recommend using a tool called Ngrok.
To install ngrok on your pi, head to this link: https://ngrok.com/download Right click on the “Linux ARM” icon and copy the Link Address. Next, run this in your command line:
sudo wget [paste the Link Address here] https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-arm.zip
and then unzip it using:
Now you have ngrok installed. Ngrok randomly generates a URL every time you run it. If you want to reserve a domain, sign up for an account on ngrok.com.
After your account is completely setup, log in to your account and click on the “Reserved” icon to reserve a domain and save changes. Make sure it is in the form: example.ngrok.io.
To connect yout account, run this in your command line:
sudo ./ngrok authtoken <insert_your_authtoken_here>
(The authtoken can be found in your account’s dashboard under “Auth”)
Once downloaded and installed, start the Hello World application we made previously and run ngrok:
sudo python run.py & ./ngrok –subdomain http example.ngrok.io 5000
You should see an output similar to this:
Your public URL should be example.ngrok.io.
6) Receive and reply to inbound SMS messages with Flask
Open up run.py again and update the code to look like the sample found here: https://github.com/TwilioDevEd/api-snippets/blob/master/quickstart/python/sms/example-8/reply_to_message.6.x.py
Save and exit your file. 7) Configure your URL into Twilio For Twilio to know where to look, you need to configure your Twilio phone number to call your webhook URL whenever a new message comes in. 1. Log into Twilio.com and go to the Console's Numbers page. 2. Click on your SMS-enabled phone number. 3. Find the Messaging section. The default “CONFIGURE WITH” is what you’ll need: "Webhooks/TwiML". 4. In the “A MESSAGE COMES IN” section, select "Webhook" and paste in the URL you want to use (https://example.ngrok.io ) Save changes- You’re ready!
8) Test your application To test your application, run this:
sudo python run.py & ./ngrok –subdomain http example.ngrok.io 5000
If you send a text message from your mobile phone to your Twilio phone number, you should see a HTTP request in your Ngrok console. Your Flask app will process the text message, and you’ll get your response back as an SMS.