• Stars
    star
    101
  • Rank 338,166 (Top 7 %)
  • Language
    Python
  • License
    MIT License
  • Created over 12 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The fastest way to start using Twilio with Python.

Twilio Hackpack for Heroku and Flask

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.

[Build Status] (http://travis-ci.org/RobSpectre/Twilio-Hackpack-for-Heroku-and-Flask) [Coverage Status] (https://coveralls.io/r/RobSpectre/Twilio-Hackpack-for-Heroku-and-Flask)

Deploy this hackpack to Heroku now!

Deploy

Features

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, tox ready.
  • PEP8 - It's good for you!
  • Python 2 and 3 - It's better for you!

Usage

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.

To start tweaking your hackpack, just edit hackpack/app.py.

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)

Installation

Step-by-step on how to deploy, configure and develop on this hackpack.

Fastest Deploy

Use Heroku to deploy this hackpack immediately:

Deploy

Getting Started

  1. Grab latest source
git clone git://github.com/RobSpectre/Twilio-Hackpack-for-Heroku-and-Flask.git 
  1. Navigate to folder and create new Heroku Cedar app
heroku create
  1. Deploy to Heroku
git push heroku master
  1. Scale your dynos
heroku scale web=1
  1. Visit the home page of your new Heroku app to see your newly configured app!
heroku open

Configuration

Want to use the built-in Twilio Client template? Configure your hackpack with three easy options.

Automagic Configuration

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:

  1. Make sure you have all dependencies installed
make init
  1. Run configure script and follow instructions.
python configure.py --account_sid ACxxxxxx --auth_token yyyyyyy
  1. 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

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"

Setting Your Own Environment Variables

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.

  1. Set environment variables locally.
export TWILIO_ACCOUNT_SID=ACxxxxxxxxxxxxxx
export TWILIO_AUTH_TOKEN=yyyyyyyyyyyyyyyyy
export TWILIO_APP_SID=APzzzzzzzzzzzzzzzzzz
export TWILIO_CALLER_ID=+15556667777
  1. Run configurator
python configure.py

Development

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:

  1. Install the dependencies.
make init
  1. Launch local development webserver
foreman start
  1. Open browser to http://localhost:5000.

  2. Tweak away on hackpack/app.py.

Testing

This hackpack comes with a full testing suite ready for nose.

make test

It also ships with an easy-to-use base class for testing your TwiML. For example, testing a basic SMS response is only two lines of code:

import test_twilio

class ExampleTest(test_twilio.TwiMLTest):
    response = self.sms("Test")
    self.assertTwiML(response)

You can also test your Gather verbs for voice apps very easily.

import test_twilio

class ExampleTest(test_twilio.TwiMLTest):
    response = self.call(digits="1")
    self.assertTwiML(response)

Branches

Two configurations are available in different branches:

  • master - Default dev mode with minimum possible code to get going.
  • production - Intended for live use with more code and dependencies appropriate to a production environment. To deploy this branch instead, adjust your procedure for the production branch:
git checkout production
git push heroku production:master

Meta

Community Contributors

Here we recognize crack members of the Twilio community who worked on this hackpack.

githalytics.com alpha

More Repositories

1

Talks-Old

Rob's presentations, in tasty hacker friendly form.
JavaScript
188
star
2

Trump-Filter

A Chrome extension that removes Donald Trump from the Internet.
JavaScript
156
star
3

Call-Your-Family

A free Twilio app to let Boston residents call their families while phone coverage is poor.
CSS
89
star
4

Twilio-Karaoke

A ten minute demo where an entire karaoke machine gets built before your very eyes.
Python
18
star
5

Reasons-Sonya-Is-Awesome

A birthday web app for my friend Sonya.
Python
17
star
6

Twilio-Queue-Example

A hackpack example using Twilio's new Queue verb.
Python
17
star
7

Caesar-Cipher

A Python package for encoding, decoding and cracking the Caesar shift cipher.
Python
13
star
8

Jeter-Filter

A Chrome extension that removes Derek Jeter from the Internet.
JavaScript
11
star
9

Hacker-Hotline

A hotline for hackers to help hackers.
Python
11
star
10

mvf1

A Python package and command line interface to control video players for MultiViewer For F1, the best way to watch Formula 1.
Python
11
star
11

Mobile-App-Distribution-with-SMS

An example of how to use Twilio SMS to distribute your mobile app.
Python
8
star
12

Realtime-MMS-Photostream

A real time gallery for messages sent with Twilio MMS.
JavaScript
7
star
13

Twilio-Connektion

A Twilio Voice app that helps you connect to someone new.
Python
6
star
14

Mustached-Message-Service

A Flask app demonstrating Twilio MMS through facial recognition/hair.
Python
6
star
15

Twilio-Queue-Demo

Do your own demonstration of Twilio's new Queue verb in Python
Python
5
star
16

Twilio-Latrine

A Twilio-powered app for managing restroom courtesy
Python
4
star
17

25-Lines-SMS-Weather

A Twilio application that returns weather data for your area code in exactly 25 lines of code.
Python
4
star
18

Tweets-on-Beats

Peeps Tweetin All ova Ya Beatz. Fo Shizzle.
Python
4
star
19

err-twilio-lookup

A plugin for the err chatbot for retrieving information on phone numbers using Twilio Lookup.
Python
4
star
20

New-York-Times-Conference-Room

Instead of hold music, use Twilio and the New York Times API to read today's headlines.
Python
4
star
21

Notes

Rob's notes from the events and conferences he attends.
4
star
22

Somebody-Put-Something-In-My-Ring

A port of Callin' Oates to Python
Python
4
star
23

Stop-Censorship-Boxee-App

A Boxee application that prompts users to call the United States Congress to stop SOPA and PROTECT-IP.
Python
3
star
24

childsafe.io

No-code, easy configuration media scanning for cloud storage to keep children safe.
HTML
3
star
25

howiknew

how i knew you were the one
PHP
3
star
26

2-Pane

A browser extension tuning Google Reader with two-pane navigation.
JavaScript
3
star
27

Facebook-IPO-SMS

A dainty Twilio app to keep you up-to-date on the Facebook IPO on the hoof.
Python
3
star
28

salt-states

A collection of the Salt states I use to run my servers.
SaltStack
3
star
29

25-Lines-SMS-Dicebag

The tiny Twilio application for the discriminating Dungeon Master.
Python
3
star
30

streamdeck

My personal settings for my Stream Deck.
Python
3
star
31

transparen.cc

A social app proxy to give users transparency into how their social data is used.
JavaScript
3
star
32

garfield

A communications platform for deterring high frequency buyers of underground commercial sex.
Python
2
star
33

humantrafficking.tips

A bot for receiving human trafficking tips via SMS and distributing to response units via email.
Python
2
star
34

RaiseCacheTwilio

SMS-based auction and raffle apps for RaiseCache using Twilio
Ruby
2
star
35

disablesslv3

A step-by-step guide on mitigating the POODLE SSLv3 vulnerability.
CSS
2
star
36

Call-Rob

Python
1
star
37

National-Library-of-Ukraine-Link-Collector

A Scrapy spider to collect direct links to volumes of academic journals for archival.
Python
1
star
38

f1bingo.com

Bingo cards for speedy race cars competing in Formula 1 races and events.
Vue
1
star
39

MofoNamer

Get your project name from a real mofo.
Python
1
star
40

Facebook-Gag

A little prank Selvidge and I pulled.
Python
1
star
41

COVID-19-Analysis

Collection of data sources and notebooks analyzing data from the COVID-19 pandemic.
Jupyter Notebook
1
star
42

A-Brooklyn-Chorus

An SMS-powered emotive installation piece built at Art Hack Day 2012.
JavaScript
1
star
43

Talks

An attempt to get my new talks in a more publicly shareable repo.
SCSS
1
star
44

Twilio-Reveal-Template

Template for my Twilio Talks using Reveal.js
JavaScript
1
star
45

quasar

πŸŒ€ The DoSomething data platform (ETL, Data Pipeline, Data Warehousing, etc.)
Python
1
star