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

Outbound SMS — How Does It Work?

Image for post
Image for post

Creating a Twilio Account & Purchasing 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

pip install twilio
# 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.
python send_sms.py
Image for post
Image for post
Ignore my goofy test messages :P

Receiving and Replying to Incoming SMS Messages

pip install virtualenv
cd ~/path_to_your_project
virtualenv — no-site-packages .
###ACTIVATE THE VIRTUALENV SANDBOX
source bin/activate
###YOUR VERSIONS MAY VARY WHEN DEPENDING WHEN YOU GO THROUGH THIS TUTORIALFlask>=0.12
twilio~=6.16.0
bin/pip install -r requirements.txt
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.
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

./ngrok http 5000
Image for post
Image for post
Ngrok gives you public access to locally hosted applications.

Replying to Incoming SMS Messages Sent from Twilio

# /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)
Image for post
Image for post
Paste your custom ngrok URL in the Twilio Console Number’s page.
###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.
Image for post
Image for post
Again, ignore my goofy Matrix references :P

HackerNoon.com

#BlackLivesMatter

By HackerNoon.com

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

Create a free Medium account to get DONT SIGN UP FOR THIS NEWSLETTER in your inbox.

Jovan Hernandez

Written by

Active investor, systems thinker, cloud engineer 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

Active investor, systems thinker, cloud engineer 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

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

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