Skip to content

Instantly share code, notes, and snippets.

@simonvc
Created July 29, 2013 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonvc/6105489 to your computer and use it in GitHub Desktop.
Save simonvc/6105489 to your computer and use it in GitHub Desktop.
hackathon twilio code
from flask import Flask, request, redirect
from pony.orm import *
import twilio.twiml
from twilio.rest import TwilioRestClient
import logging
log = logging.getLogger('werkzeug')
db = Database('sqlite', 'responses.sqlite', create_db=True)
class Response(db.Entity):
number = Required(unicode)
terrorist_number = Optional(unicode)
male = Optional(bool)
bright_colours = Optional(bool)
bright_hair = Optional(bool)
alone = Optional(bool)
hours_missed = Optional(int)
db.generate_mapping(create_tables=True)
app = Flask(__name__)
responses = {
}
from_number = ""
@app.route("/", methods=['GET', 'POST'])
def hello_monkey():
from_number = request.values.get('From', None)
resp = twilio.twiml.Response()
with resp.gather(numDigits=1, action="/create-entry", method="POST") as g:
g.say("To report a TERRORIST, press 1.")
#log.warning("this is the number you are calling from: " + ' '.join(from_number))
return str(resp)
@app.route("/create-entry", methods=['GET', 'POST'])
def handle_key():
digit_pressed = request.values.get('Digits', None)
resp = twilio.twiml.Response()
if digit_pressed == "1":
with resp.gather(timeout= 30,finishOnKey ="#", action="/report-terrorist", method="POST") as g:
g.say("Type in the suspects telephone number.")
#log.warning("digits pressed before handle: " + request.values.get('Digits',None))
return str(resp)
else:
resp.say("taking you back to start")
resp.redirect("/")
return str(resp)
@app.route("/report-terrorist",methods=['GET', 'POST'])
def report_terrorist():
from_number = request.values.get('From', None)
digits_pressed = request.values.get('Digits',None)
#log.warning("digits pressed: " + digits_pressed + " first three digits: " + digits_pressed[:3] + " digits lenght: " + str(len(digits_pressed)))
resp = twilio.twiml.Response()
if (digits_pressed[:3] == "447" and len(digits_pressed) > 10) or (digits_pressed[:2] == "07" and len(digits_pressed) > 9) :
legal_number = ""
if digits_pressed[:2] == "44":
legal_number = "+" + digits_pressed
elif digits_pressed[:2] == "07":
legal_number = "+" + digits_pressed[2:]
log.warning("this is the number put in: " + legal_number)
with db_session:
r = Response.get(number=from_number)
if r:
resp.say("thanks for calling again!")
r.delete()
r = Response(number = from_number)
r.terrorist_number = legal_number
####### this is where we create the db entry for the first time
with resp.gather(numDigits=1, action="/handle-q1", method="POST") as g:
g.say("Are they male or female, press 1 for male 2 for female.")
return str(resp)
else:
resp.say("wrong number, try again")
with resp.gather(timeout= 30,finishOnKey ="#", action="/report-terrorist", method="POST") as g:
g.say("Type in the suspects telephone number.")
#log.warning("digits pressed before handle: " + request.values.get('Digits',None))
return str(resp)
@app.route("/handle-q1",methods=['GET', 'POST'])
def handle_q1():
from_number = request.values.get('From', None)
digit_pressed = request.values.get('Digits', None)
from_number = request.values.get('From', None)
resp = twilio.twiml.Response()
if digit_pressed == "1":
with db_session:
r = Response.get(number=from_number)
r.male = True
with resp.gather(numDigits=1, action="/handle-q2", method="POST") as g:
g.say("did they sleep alone last night, press 1 for yes 2 for no")
return str(resp)
elif digit_pressed == "2":
with db_session:
r = Response.get(number=from_number)
r.male = False
with resp.gather(numDigits=1, action="/handle-q2", method="POST") as g:
g.say("did they sleep alone last night, press 1 for yes 2 for no")
return str(resp)
else:
with resp.gather(numDigits=1, action="/handle-q1", method="POST") as g:
g.say("Press 1 for male 2 for female.")
return str(resp)
@app.route("/handle-q2",methods=['GET', 'POST'])
def handle_q2():
from_number = request.values.get('From', None)
digit_pressed = request.values.get('Digits', None)
from_number = request.values.get('From', None)
resp = twilio.twiml.Response()
if digit_pressed == "1":
with db_session:
r = Response.get(number=from_number)
r.alone = True
with resp.gather(numDigits=1, action="/handle-q3", method="POST") as g:
g.say("Are they wearing bright colors? Press 1 for yes or 2 for no")
return str(resp)
elif digit_pressed == "2":
with db_session:
r = Response.get(number=from_number)
r.alone = False
with resp.gather(numDigits=1, action="/handle-q3", method="POST") as g:
g.say("Are they wearing bright colors? Press 1 for yes or 2 for no")
return str(resp)
else:
with resp.gather(numDigits=1, action="/handle-q2", method="POST") as g:
g.say("Press 1 for yes or 2 for no. If you are retarted please just grunt now.")
return str(resp)
@app.route("/handle-q3",methods=['GET', 'POST'])
def handle_q3():
from_number = request.values.get('From', None)
digit_pressed = request.values.get('Digits', None)
from_number = request.values.get('From', None)
resp = twilio.twiml.Response()
if digit_pressed == "1":
with db_session:
r = Response.get(number=from_number)
r.bright_colours = True
with resp.gather(numDigits=1, action="/handle-q4", method="POST") as g:
g.say("Do they have light hair? press 1 for yes or 2 for no.")
return str(resp)
elif digit_pressed == "2":
with db_session:
r = Response.get(number=from_number)
r.bright_colours = False
with resp.gather(numDigits=1, action="/handle-q4", method="POST") as g:
g.say("Do they have light hair? press 1 for yes or 2 for no.")
return str(resp)
else:
with resp.gather(numDigits=1, action="/handle-q3", method="POST") as g:
g.say("Press 1 for yes or 2 for no. If you are retarted please just grunt now.")
return str(resp)
@app.route("/handle-q4",methods=['GET', 'POST'])
def handle_q4():
from_number = request.values.get('From', None)
digit_pressed = request.values.get('Digits', None)
from_number = request.values.get('From', None)
resp = twilio.twiml.Response()
if digit_pressed == "1":
with db_session:
r = Response.get(number=from_number)
r.bright_hair = True
with resp.gather(numDigits=1, action="/handle-q5", method="POST") as g:
g.say("How many hours would it take for them to be missed? Use your keypad to enter a number in hours.")
return str(resp)
elif digit_pressed == "2":
with db_session:
r = Response.get(number=from_number)
r.bright_hair = False
with resp.gather(numDigits=1, action="/handle-q5", method="POST") as g:
g.say("How many hours would it take for them to be missed? Use your keypad to enter a number in hours.")
return str(resp)
else:
with resp.gather(numDigits=1, action="/handle-q4", method="POST") as g:
g.say("Press 1 for yes or 2 for no. If you are retarted please just grunt now.")
return str(resp)
@app.route("/handle-q5",methods=['GET', 'POST'])
def handle_q5():
from_number = request.values.get('From', None)
digit_pressed = request.values.get('Digits', None)
from_number = request.values.get('From', None)
resp = twilio.twiml.Response()
with db_session:
r = Response.get(number=from_number)
r.hours_missed = int(digit_pressed)
resp.say("Thank you citizen. Your cooperation keeps us all safe.")
resp.redirect("/make-call")
return str(resp)
@app.route('/make-call', methods = ['GET','POST'])
def make_call():
resp = twilio.twiml.Response()
mySid=''
myAuthToken=''
client=TwilioRestClient(mySid, myAuthToken)
with db_session:
r = Response.get(number=from_number)
call = client.calls.create(to=r.terrorist_number, from_="+44 5", url='http://twimlets.com/message?Message[0]=Thank+You$')
return str(resp)
if __name__ == "__main__":
app.run(debug=True,host="0.0.0.0",port=5001)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment