Difference between revisions of "Text messaging (Twilio API) + Raspberry Pi"

From ESE205 Wiki
Jump to: navigation, search
m
Line 21: Line 21:
 
If you get a “pip: command not found” error, run this instead:  
 
If you get a “pip: command not found” error, run this instead:  
  
sudo easy_install twilio
+
  sudo easy_install twilio
  
If you would prefer to install it manually, download the source code for twilio-python with this link:
+
If you would prefer to install it manually, download the source code for twilio-python with this [https://github.com/twilio/twilio-python/zipball/master| link:]
 
 
https://github.com/twilio/twilio-python/zipball/master
 
  
 
Then install it by running:
 
Then install it by running:
  
sudo python setup.py install
+
  sudo python setup.py install
  
 
==Step 2: Send an outbound text message==
 
==Step 2: Send an outbound text message==
Line 37: Line 35:
 
   sudo nano file_name.py
 
   sudo nano file_name.py
  
2) Copy or paste the code found [https://github.com/TwilioDevEd/api-snippets/blob/master/quickstart/python/sms/example-1/send_notifications.6.x.py| here: ] 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.  
+
2) Copy or paste the code found [https://github.com/TwilioDevEd/api-snippets/blob/master/quickstart/python/sms/example-1/send_notifications.6.x.py| here: ] into your file. Before you run it, swap the placeholder values for account_sid and auth_token with your personal Twilio credentials. Go [https://www.twilio.com/console| here:] and log in. On this page, you will find your credentials, which you will need any time you send messages through the Twilio client like this. You can reveal 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].  
 
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:  
+
3) Save your changes and exit the file. To test if your program works, run this in your command line:  
  
sudo python file_name.py
+
  sudo python file_name.py
  
 
You should receive a message on your phone number.  
 
You should receive a message on your phone number.  
Line 54: Line 48:
 
To receive and reply to messages, you need to create a web application that can accept incoming requests, which is why we need Flask.  
 
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
+
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.  
 
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.  
Line 60: Line 54:
 
For python 2.4, run the following in your command line:
 
For python 2.4, run the following in your command line:
  
sudo easy_install virtualenv
+
  sudo easy_install virtualenv
  
 
For python 2.5 – 2.7, run this:  
 
For python 2.5 – 2.7, run this:  
  
sudo easy_install -2.7 virtualenv
+
  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)
 
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)
Line 70: Line 64:
 
For python 3.4+, run this:  
 
For python 3.4+, run this:  
  
sudo pip install virtualenv
+
  sudo pip install virtualenv
  
  
2) create and activate your virtual environment
+
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:  
 
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
+
  cd twilio_guide_folder
sudo virtualenv --no-site-packages
+
  sudo virtualenv --no-site-packages
  
 
Activate the virtual environment. Run this:  
 
Activate the virtual environment. Run this:  
  
sudo source bin/activate
+
  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
+
If you are interested in learning more about virtualenv, visit [http://docs.python-guide.org/en/latest/dev/virtualenvs/#lower-level-virtualenv| here:]
  
3) Install dependencies  
+
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:  
 
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:  
Line 95: Line 89:
 
To install both of these packages with pip in your command line, run this:
 
To install both of these packages with pip in your command line, run this:
  
sudo bin/pip install –r dependencies.txt
+
  sudo bin/pip install –r dependencies.txt
  
 
4) Test everything
 
4) Test everything
 
 
 
Before testing, make sure your virtualenv is activated. Run this in your command line:
 
Before testing, make sure your virtualenv is activated. Run this in your command line:
cd twilio_guide_folder
+
  cd twilio_guide_folder
sudo source bin/activate
+
  sudo source bin/activate
 
 
 
Then, create and open a file called run.py:  
 
Then, create and open a file called run.py:  
 
+
  sudo nano run.py
sudo nano run.py
 
 
 
 
Add these lines:
 
Add these lines:
  
Line 114: Line 103:
 
@app.route("/")
 
@app.route("/")
 
def hello():
 
def hello():
    return "Hello World!"
+
(indentation) return "Hello World!"
 
if __name__ == "__main__":
 
if __name__ == "__main__":
    app.run(debug=True)
+
(indentation) app.run(debug=True)
 +
 
 
Save and exit the file. Now you can try running your file. In your command line, type:  
 
Save and exit the file. Now you can try running your file. In your command line, type:  
 
+
  sudo python run.py
sudo python run.py
 
 
 
 
You should see:
 
You should see:
 
+
  *Running on http://127.0.0.1:5000/
* 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.  
 
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
+
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.  
 
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 [https://ngrok.com/download| link:]
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.  
 
Right click on the “Linux ARM” icon and copy the Link Address.  
 
Next, run this in your command line:  
 
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
sudo wget [paste the Link Address here] https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-arm.zip
 
 
 
 
and then unzip it using:  
 
and then unzip it using:  
 
+
  unzip ngrok-stable-linux-arm.zip
unzip ngrok-stable-linux-arm.zip
 
 
 
 
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.
 
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.  
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:  
 
To connect yout account, run this in your command line:  
 
   
 
   
sudo ./ngrok authtoken <insert_your_authtoken_here>
+
  sudo ./ngrok authtoken <insert_your_authtoken_here>
  
 
(The authtoken can be found in your account’s dashboard under “Auth”)
 
(The authtoken can be found in your account’s dashboard under “Auth”)
Line 153: Line 133:
 
Once downloaded and installed, start the Hello World application we made previously and run ngrok:  
 
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
+
  sudo python run.py & ./ngrok –subdomain http example.ngrok.io 5000
  
 
You should see an output similar to this:  
 
You should see an output similar to this:  
 
 
 
 
 
Your public URL should be example.ngrok.io.  
 
Your public URL should be example.ngrok.io.  
  
6) Receive and reply to inbound SMS messages with Flask
+
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
+
Open up run.py again and update the code to look like the sample found [ https://github.com/TwilioDevEd/api-snippets/blob/master/quickstart/python/sms/example-8/reply_to_message.6.x.py| here: ]
 +
Save and exit your file.
  
Save and exit your file.
 
 
7) Configure your URL into Twilio
 
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.
 
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.
 
1. Log into Twilio.com and go to the Console's Numbers page.
Line 173: Line 150:
 
3. Find the Messaging section. The default “CONFIGURE WITH” is what you’ll need: "Webhooks/TwiML".
 
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 )
 
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!
 
Save changes- You’re ready!
  
Line 178: Line 156:
 
To test your application, run this:  
 
To test your application, run this:  
  
sudo python run.py & ./ngrok –subdomain http example.ngrok.io 5000
+
  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.
 
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.

Revision as of 14:59, 30 April 2018

Introduction

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

1) Update Python 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

2) 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: 1) 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

2) Copy or paste the code found here: into your file. Before you run it, swap the placeholder values for account_sid and auth_token with your personal Twilio credentials. Go here: and log in. On this page, you will find your credentials, which you will need any time you send messages through the Twilio client like this. You can reveal 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].

3) 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:

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:

Flask>=0.12 twilio~=6.0.0

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(): (indentation) return "Hello World!" if __name__ == "__main__": (indentation) app.run(debug=True)

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: 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:

  unzip ngrok-stable-linux-arm.zip

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 [ https://github.com/TwilioDevEd/api-snippets/blob/master/quickstart/python/sms/example-8/reply_to_message.6.x.py%7C here: ] 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.