forked from womensp2p/text-to-call-webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
101 lines (87 loc) · 3.32 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import os
from flask import Flask
from flask import render_template
from flask import url_for
from flask import request
from twilio import twiml
from twilio.rest import TwilioRestClient
from twilio.util import TwilioCapability
# Declare and configure application
app = Flask(__name__, static_url_path='/static')
app.config.from_pyfile('local_settings.py')
# Voice Request URL
@app.route('/voice', methods=['GET', 'POST'])
def voice():
message = request.args['message']
language = request.args['language']
response = twiml.Response()
response.say(message, voice="woman", language=language)
return str(response)
# SMS Request URL
@app.route('/sms', methods=['GET', 'POST'])
def sms():
response = twiml.Response()
response.sms("Congratulations! You deployed the Twilio Hackpack" \
" for Heroku and Flask.")
return str(response)
# Twilio Client demo template
@app.route('/client')
def client():
configuration_error = None
for key in ('TWILIO_ACCOUNT_SID', 'TWILIO_AUTH_TOKEN', 'TWILIO_APP_SID',
'TWILIO_CALLER_ID'):
if not app.config[key]:
configuration_error = "Missing from local_settings.py: " \
"%s" % key
token = None
if not configuration_error:
capability = TwilioCapability(app.config['TWILIO_ACCOUNT_SID'],
app.config['TWILIO_AUTH_TOKEN'])
capability.allow_client_incoming("joey_ramone")
capability.allow_client_outgoing(app.config['TWILIO_APP_SID'])
token = capability.generate()
params = {'token': token}
return render_template('client.html', params=params,
configuration_error=configuration_error)
# Installation success page
@app.route('/')
def index():
params = {
'Voice Request URL': url_for('.voice', _external=True),
'SMS Request URL': url_for('.sms', _external=True),
'Client URL': url_for('.client', _external=True)}
return render_template('index.html', params=params,
configuration_error=None)
@app.route('/callmemaybe', methods=['GET', 'POST'])
def callmemaybe():
configuration_error = None
for key in ('TWILIO_ACCOUNT_SID', 'TWILIO_AUTH_TOKEN', 'TWILIO_APP_SID',
'TWILIO_CALLER_ID'):
if not app.config[key]:
configuration_error = "Missing from local_settings.py: " \
"%s" % key
token = None
if not configuration_error:
try:
message = request.form['message']
number = request.form['number']
language = request.form['language']
except Exception:
message = False
if message:
client = TwilioRestClient(app.config['TWILIO_ACCOUNT_SID'],
app.config['TWILIO_AUTH_TOKEN'])
call = client.calls.create(to=number,
from_=app.config['TWILIO_CALLER_ID'],
url=url_for('.voice',
message=message,
language=language,
_external=True))
return render_template('callmemaybe.html',
configuration_error=configuration_error)
# If PORT not specified by environment, assume development config.
if __name__ == '__main__':
port = int(os.environ.get("PORT", 5000))
if port == 5000:
app.debug = True
app.run(host='0.0.0.0', port=port)