Using Twilio to Send SMS Texts via Python, Flask, and Ngrok

Jovan Hernandez
Aug 6, 2018 · 7 min read
Image for post
Image for post

Python is great for many things. One thing, in particular, is hacking together code to develop and deploy simple web apps for particular functions or utilities you may need in your business. In this tutorial, I’ll be going over several how to integrate Twilio’s Programmable SMS systems using their Communications REST API, Python’s Flask micro-framework, and ngrok, a cloud service that exposes local servers behind NATs and firewalls to the public internet over secure tunnels.

Outbound SMS — How Does It Work?

Image for post
Image for post

Using Twilio’s REST API, you can send outgoing SMS messages from your Twilio phone number to mobile phones around the globe.

Your application makes a post request to Twilio. Once received, Twilio sends a text message to a number declared in the Python script, or whatever variable is declared. It’s that simple.

Creating a Twilio Account & Purchasing a Number

Before you begin, you’ll need to open an account with Twilio and also purchase a number that your messages will be sent from. It’s a fairly simple process and you won’t need to spend anything for this tutorial as they give you free credits to purchase and test out their services. If you plan on integrating Twilio into your business, you will incur some costs in the future so it’d be a good idea to skim over their pricing model.

Head over to Twilio.comand sign up for a free account. Once registered, you can purchase a number using the credits they give new members. For the sake of brevity, I’ll link you to their support page on How to Purchase a Number.

Image for post
Image for post
Make sure you purchase a number that is SMS capable.

Setting Up Your Environment and Installing Packages

In my last post, I went over how to configure Ubuntu for Python development, Flask apps, and VirtualEnv. You might want to check that out first but if you already have, let’s begin by installing twiliopackage. (NOTE: You should be on Python3 for this tutorial.)

pip install twilio

With Twilio installed, we can now send outbound messages from the number we purchased to target numbers with a simple Python script. Create and open a new file called send_sms.pyand type or paste in this code sample. Make sure to replace the placeholder values with your own information (those being your account_sid, auth_token, body, from, and tofields. You can find your Account SID and Auth Token in your Twilio Console.)

# Download the helper library from https://www.twilio.com/docs/python/installfrom twilio.rest import Client# Your Account Sid and Auth Token from twilio.com/console
account_sid = ‘AC00000000000000000000000000000’
auth_token = ‘your_auth_token’
client = Client(account_sid, auth_token)
message = client.messages.create(
body=’This is a test message!’,
from_=’[+][1][5555555555]',
to=’[+][1][6666666666]'
)
print(message.sid)
Image for post
Image for post
Replace the placeholder values with your own information.

Once done, save and run the script with the command below:

python send_sms.py

That’s it! You should have a message from your Twilio phone number hit your cell phone shortly. You’ll see a prefix to your message if you are on a Trial account.

Image for post
Image for post
Ignore my goofy test messages :P

Receiving and Replying to Incoming SMS Messages

In order to receive and reply to incoming SMS messages, we will need to install Flask, setup a virtualenv sandbox, and also configure ngork so Twilio can talk to our app. Let’s begin.

If you are familiar at all with Python development or have seen my last post on setting up a development environment, this should seem familiar. Lucky for us, if you are on Python 3.4+, pip should automatically be installed. If not, Google is your friend.

Not to install virtualenv, just run the following command:

pip install virtualenv

Once installed, navigate to your directory where you are keeping this project and create a virtual environment.

cd ~/path_to_your_project
virtualenv — no-site-packages .
###ACTIVATE THE VIRTUALENV SANDBOX
source bin/activate

Once activated, you should see the name of your project sandbox prefixed in your terminal. If you are confused or ran into an issue, see one of my previous posts here.

Virtualenv is great for testing apps and installing packages and dependencies unique to each project. For this Twilio app, we are going to need two packages. Start by creating a new file called requirements.txtand adding the following lines:

###YOUR VERSIONS MAY VARY WHEN DEPENDING WHEN YOU GO THROUGH THIS TUTORIALFlask>=0.12
twilio~=6.16.0

Then install these packages by running the following:

bin/pip install -r requirements.txt

If all is well, we can test everything by creating a test file and running it locally. Make sure your virtualenv is activated, navigate to your project folder and create a new file called run.py. Paste or write the following code:

from flask import Flask
app = Flask(__name__)
@app.route(“/sms”)
def hello():
return “This is your Twilio App Working!”
if __name__ == “__main__”:
app.run(debug=True)
Image for post
Image for post
Test code for the Twilio app in Flask.

Now let’s run the script by running python run.py.
You should see: * Running on http://127.0.0.1:5000/
Naviage to localhost:5000/sms. You should see the following text in your browser:

Image for post
Image for post
What you should see if you did it correctly.

Allow Twilio to talk to your Flask App Using ngrok

When you’re working on your Flask application in your development environment, your app is only reachable by other programs on your computer. This is why your app is only available on localhostor 127.0.0.1. In order to resolve this, we can deploy our app on something like Heroku, AWS, or other cloud services, but for this specific tutorial, we are going to use ngrok.

Per their own website, ngrok “allows you to expose a web server running on your local machine to the internet. Just tell ngrok what port your web server is listening on.” Once started, Ngrok provides a unique URL on the ngrok.io domain which forwards incoming requests to your local development environment. If you’ve never used ngrok, head over to their download pageand grab the binary for your OS.

Once the package has been downloaded and unzipped, we can run our test app we made previously: python run.py. Then in a new terminal app, we’ll initialize a session with ngrok with the following command:

./ngrok http 5000

You should see output similar to the terminal in the top left of the screenshot below:

Image for post
Image for post
Ngrok gives you public access to locally hosted applications.

Copy your unique public address, put it in a browser, and trail it with a “/sms/”. For instance, the address above should be “http://a0f8f155.ngrok.io/sms”.

Replying to Incoming SMS Messages Sent from Twilio

When someone sends an SMS to your Twilio phone number, Twilio makes an HTTP request to your server asking for instructions on what to do next. Once you receive the request, you can tell Twilio to reply with an SMS, kick off a phone call, store details about the message in your database, or trigger an entirely different action.

For this specific tutorial, we’ll have Twilio reply to the user’s response with an acknowledgment receipt. Use a text editor to open run.pyagain and edit the code to reflect the following:

# /usr/bin/env python
# Download the twilio-python library from twilio.com/docs/libraries/python
from flask import Flask, request
from twilio.twiml.messaging_response import MessagingResponse
app = Flask(__name__)@app.route("/sms", methods=['GET', 'POST'])
def sms_ahoy_reply():
"""Respond to incoming messages with a receipt SMS."""
# Start our response
resp = MessagingResponse()
# Add a message
resp.message("Thank you for your response! We are confirming your message.")
return str(resp)if __name__ == "__main__":
app.run(debug=True)

Feel free to edit the code above, especially the line under # Add a message.I was playing around with my code while watching the Matrix so my messages will seem goofy in the following examples.

Save the file and run the python run.pycommand again. Make sure ngrok is still running and copy the custom public URL they give you. For Twilio to know what URL to grab, we need to configure our Twilio phone number to call your webhook URL whenever a new message comes in, which is what ngrok does for us.

Log into Twilio.com and go to the Console’s Numbers page, click on your SMS-enabled phone number and find the Messaging section. The default “CONFIGURE WITH” is what you’ll need: “Webhooks/TwiML”.

In the “A MESSAGE COMES IN” section, select “Webhook” and paste in the URL you want to use.

Image for post
Image for post
Paste your custom ngrok URL in the Twilio Console Number’s page.

Now let’s test your entire SMS messaging app by executing your two python scripts.

###RUN THIS TO SEND THE INITIAL TEXT
python send_sms.py
###RUN THIS TO MAKE SURE TWILIO IS LISTENING FOR THE RESPONSE
python run.py
Image for post
Image for post
Everything running together. You can confirm your response script is publicly available by going to the URL.

You should receive your initial text, and if you reply to it, you should get the response text that you coded in your second python script.

Image for post
Image for post
Again, ignore my goofy Matrix references :P

That’s pretty much it! With the knowledge from this tutorial, you can build out a fully-fledged web app that you have more than likely interacted with already. Many professions from doctors and dentists use services like this to confirm appointments, or even ride-sharing apps like Uber to confirm a user is ready to be picked up.

There are many other Twilio tutorials out there that guide you through similar processes. Piece some of them together to build your own custom app!

HackerNoon.com

#BlackLivesMatter

By HackerNoon.com

how hackers start their afternoons. the real shit is on hackernoon.com. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Jovan Hernandez

Written by

Systems thinker, cloud engineer, active investor jovanshernandez.com

HackerNoon.com

Elijah McClain, George Floyd, Eric Garner, Breonna Taylor, Ahmaud Arbery, Michael Brown, Oscar Grant, Atatiana Jefferson, Tamir Rice, Bettie Jones, Botham Jean

Jovan Hernandez

Written by

Systems thinker, cloud engineer, active investor jovanshernandez.com

HackerNoon.com

Elijah McClain, George Floyd, Eric Garner, Breonna Taylor, Ahmaud Arbery, Michael Brown, Oscar Grant, Atatiana Jefferson, Tamir Rice, Bettie Jones, Botham Jean

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store