An easy-to-use repo to kickstart your Twilio app using Flask and deploy onto Heroku. Easy to clone, easy to tweak, easy to deploy.
[]
(http://travis-ci.org/RobSpectre/Twilio-Hackpack-for-Heroku-and-Flask)
Look at all these crazy features!
- Twilio Client - This hackpack ships with a base Jinja2 template for Twilio Client already configured and ready to call.
- Automagic Configuration - Just run
python configure.py --account_sid ACxxxx --auth_token yyyyy
and the hackpack configures Twilio and Heroku for you. - Production Ready - The production branch features a few more settings and dependencies to make the hackpack ready to put into live service.
- Plug-and-Play - Procfile, requirements.txt and Makefile make installation and usage a breeze.
- Boilerplate - All the Flask app boilerplate with example Voice and SMS Request URLs ready for use on Twilio.
- Testing - Easy base class for unit testing with example tests, nose ready.
- PEP8 - It's good for you!
This hackpack ships with two ready-to-go endpoints for your Twilio Voice and SMS apps. The two routes /voice and /sms contain two examples you can modify easily.
For example, here is a quick Twilio Voice app that plays some Ramones.
@app.route('/voice', methods=['POST'])
def voice():
response = twiml.Response()
response.play("http://example.com/music/ramones.mp3")
return str(response)
SMS apps are similarly easy.
@app.route('/sms', methods=['POST'])
def sms():
response = twiml.Response()
response.sms("The Ramones are great!")
return str(response)
These apps can get interactive pretty quickly. For example, let's make an SMS app that responds with "Best band ever" when you text RAMONES.
@app.route('/sms', methods=['POST'])
def sms():
response = twiml.Response()
body = request.form['Body']
if "RAMONES" in body:
response.sms("Best band ever.")
else:
response.sms("Not the best band ever.")
return str(response)
You can apply this same concept to Gathering user input on Twilio Voice. Here we will Gather the user input with one route and then handle the user input with another.
@app.route('/voice', methods=['POST'])
def voice():
response = twiml.Response()
with response.gather(numDigits=1, action="/gather") as gather:
gather.say("Press 1 to indicate The Ramones are the best band ever.")
return str(response)
@app.route('/gather', methods=['POST'])
def gather():
response = twiml.Response()
digits = request.form['Digits']
if digits == "1":
response.say("You are correct. The Ramones are the best.")
else:
response.say("You are wrong. Never call me again.")
return str(response)
Step-by-step on how to deploy, configure and develop on this hackpack.
- Grab latest source
git clone git://github.com/RobSpectre/Twilio-Hackpack-for-Heroku-and-Flask.git
- Navigate to folder and create new Heroku Cedar app
heroku create --stack cedar
- Deploy to Heroku
git push heroku master
- Scale your dynos
heroku scale web=1
- Visit the home page of your new Heroku app to see your newly configured app!
heroku open
Want to use the built-in Twilio Client template? Configure your hackpack with three easy options.
This hackpack ships with an auto-configure script that will create a new TwiML app, purchase a new phone number, and set your Heroku app's environment variables to use your new settings. Here's a quick step-by-step:
- Make sure you have all dependencies installed
make init
- Run configure script and follow instructions.
python configure.py --account_sid ACxxxxxx --auth_token yyyyyyy
- For local development, copy/paste the environment variable commands the configurator provides to your shell.
export TWILIO_ACCOUNT_SID=ACxxxxxx export TWILIO_AUTH_TOKEN=yyyyyyyyy export TWILIO_APP_SID=APzzzzzzzzzz export TWILIO_CALLER_ID=+15556667777
Automagic configuration comes with a number of features.
python configure.py --help
to see them all.
local_settings.py is a file available in the hackpack route for you to configure your twilio account credentials manually. Be sure not to expose your Twilio account to a public repo though.
ACCOUNT_SID = "ACxxxxxxxxxxxxx"
AUTH_TOKEN = "yyyyyyyyyyyyyyyy"
TWILIO_APP_SID = "APzzzzzzzzz"
TWILIO_CALLER_ID = "+17778889999"
The configurator will automatically use your environment variables if you already have a TwiML app and phone number you would prefer to use. When these environment variables are present, it will configure the Twilio and Heroku apps all to use the hackpack.
- Set environment variables locally.
export TWILIO_ACCOUNT_SID=ACxxxxxxxxxxxxxx export TWILIO_AUTH_TOKEN=yyyyyyyyyyyyyyyyy export TWILIO_APP_SID=APzzzzzzzzzzzzzzzzzz export TWILIO_CALLER_ID=+15556667777
- Run configurator
python configure.py
Getting your local environment setup to work with this hackpack is similarly easy. After you configure your hackpack with the steps above, use this guide to get going locally:
- Install the dependencies.
make init
- Launch local development webserver
foreman start
-
Open browser to http://localhost:5000.
-
Tweak away on
app.py
.
Things that aren't done:
- It uses phone numbers instead of FK numbers. Need to change it so that it uses FK numbers and looks up the phone number from the database.
- Make it go to voicemail.
- Use configurable Twilio information.