-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathapp.py
297 lines (213 loc) · 8.16 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import random
import urlparse
import twilio.twiml
from flask import Flask, request, render_template, url_for
from flask.ext.jsonpify import jsonify
from flask.ext.sqlalchemy import SQLAlchemy
from raven.contrib.flask import Sentry
from twilio import TwilioRestException
from models import aggregate_stats, log_call
from utils import play_or_say
from political_data import PoliticalData
app = Flask(__name__)
app.config.from_object('config.ConfigProduction')
db = SQLAlchemy(app)
sentry = Sentry(app)
call_methods = ['GET', 'POST']
data = PoliticalData()
def full_url_for(route, **kwds):
return urlparse.urljoin(app.config['APPLICATION_ROOT'],
url_for(route, **kwds))
def parse_params(r):
params = {
'userPhone': r.values.get('userPhone'),
'campaignId': r.values.get('campaignId', 'default'),
'zipcode': r.values.get('zipcode', None),
'repIds': r.values.getlist('repIds')
}
# lookup campaign by ID
campaign = data.get_campaign(params['campaignId'])
# add repIds to the parameter set, if spec. by the campaign
if campaign.get('repIds', None):
if isinstance(campaign['repIds'], basestring):
params['repIds'] = [campaign['repIds']]
else:
params['repIds'] = campaign['repIds']
# get representative's id by zip code
if params['zipcode'] and params['repIds']:
params['repIds'] = data.locate_member_ids(
params['zipcode'], campaign)
if 'random_choice' in campaign:
# pick a random choice among a selected set of members
params['repIds'] = [random.choice(campaign['random_choice'])]
return params, campaign
def intro_zip_gather(params, campaign):
resp = twilio.twiml.Response()
play_or_say(resp, campaign['msg_intro'])
return zip_gather(resp, params, campaign)
def zip_gather(resp, params, campaign):
with resp.gather(numDigits=5, method="POST",
action=url_for("zip_parse", **params)) as g:
play_or_say(g, campaign['msg_ask_zip'])
return str(resp)
def dialing_config(params):
return dict(
timeLimit=app.config['TW_TIME_LIMIT'],
timeout=app.config['TW_TIMEOUT'],
hangupOnStar=True, # allow the user to hangup and move onto next call
action=url_for('call_complete', **params))
def make_calls(params, campaign):
"""
Connect a user to a sequence of congress members.
Required params: campaignId, repIds
Optional params: zipcode,
"""
resp = twilio.twiml.Response()
n_reps = len(params['repIds'])
play_or_say(resp, campaign['msg_call_block_intro'],
n_reps=n_reps, many_reps=n_reps > 1)
resp.redirect(url_for('make_single_call', call_index=0, **params))
return str(resp)
@app.route('/make_calls', methods=call_methods)
def _make_calls():
params, campaign = parse_params(request)
return make_calls(params, campaign)
@app.route('/create', methods=call_methods)
def call_user():
"""
Makes a phone call to a user.
Required Params:
userPhone
campaignId
Optional Params:
zipcode
repIds
"""
# parse the info needed to make the call
params, campaign = parse_params(request)
# initiate the call
try:
call = app.config['TW_CLIENT'].calls.create(
to=params['userPhone'],
from_=campaign['number'],
url=full_url_for("connection", **params),
timeLimit=app.config['TW_TIME_LIMIT'],
timeout=app.config['TW_TIMEOUT'],
status_callback=full_url_for("call_complete_status", **params))
result = jsonify(message=call.status, debugMode=app.debug)
result.status_code = 200 if call.status != 'failed' else 500
except TwilioRestException, err:
result = jsonify(message=err.msg.split(':')[1].strip())
result.status_code = 200
return result
@app.route('/connection', methods=call_methods)
def connection():
"""
Call handler to connect a user with their congress person(s).
Required Params:
campaignId
Optional Params:
zipcode
repIds (if not present go to incoming_call flow and asked for zipcode)
"""
params, campaign = parse_params(request)
if params['repIds']:
resp = twilio.twiml.Response()
play_or_say(resp, campaign['msg_intro'])
action = url_for("_make_calls", **params)
with resp.gather(numDigits=1, method="POST", timeout=10,
action=action) as g:
play_or_say(g, campaign['msg_intro_confirm'])
return str(resp)
else:
return intro_zip_gather(params, campaign)
@app.route('/incoming_call', methods=call_methods)
def incoming_call():
"""
Handles incoming calls to the twilio numbers.
Required Params: campaignId
Each Twilio phone number needs to be configured to point to:
server.com/incoming_call?campaignId=12345
from twilio.com/user/account/phone-numbers/incoming
"""
params, campaign = parse_params(request)
return intro_zip_gather(params, campaign)
@app.route("/zip_parse", methods=call_methods)
def zip_parse():
"""
Handle a zip code entered by the user.
Required Params: campaignId, Digits
"""
params, campaign = parse_params(request)
zipcode = request.values.get('Digits', '')
rep_ids = data.locate_member_ids(zipcode, campaign)
if app.debug:
print 'DEBUG: zipcode = {}'.format(zipcode)
if not rep_ids:
resp = twilio.twiml.Response()
play_or_say(resp, campaign['msg_invalid_zip'])
return zip_gather(resp, params, campaign)
params['zipcode'] = zipcode
params['repIds'] = rep_ids
return make_calls(params, campaign)
@app.route('/make_single_call', methods=call_methods)
def make_single_call():
params, campaign = parse_params(request)
i = int(request.values.get('call_index', 0))
params['call_index'] = i
member = [l for l in data.legislators
if l['bioguide_id'] == params['repIds'][i]][0]
congress_phone = member['phone']
full_name = unicode("{} {}".format(
member['firstname'], member['lastname']), 'utf8')
resp = twilio.twiml.Response()
if 'voted_with_list' in campaign and \
params['repIds'][i] in campaign['voted_with_list']:
play_or_say(
resp, campaign['msg_repo_intro_voted_with'], name=full_name)
else:
play_or_say(resp, campaign['msg_rep_intro'], name=full_name)
if app.debug:
print u'DEBUG: Call #{}, {} ({}) from make_single_call()'.format(
i, full_name, congress_phone)
resp.dial(congress_phone, **dialing_config(params))
return str(resp)
@app.route('/call_complete', methods=call_methods)
def call_complete():
params, campaign = parse_params(request)
log_call(db, params, campaign, request)
resp = twilio.twiml.Response()
i = int(request.values.get('call_index', 0))
if i == len(params['repIds']) - 1:
# thank you for calling message
play_or_say(resp, campaign['msg_final_thanks'])
else:
# call the next representative
params['call_index'] = i + 1 # increment the call counter
play_or_say(resp, campaign['msg_between_thanks'])
resp.redirect(url_for('make_single_call', **params))
return str(resp)
@app.route('/call_complete_status', methods=call_methods)
def call_complete_status():
# asynch callback from twilio on call complete
params, campaign = parse_params(request)
return jsonify(dict(
phoneNumber=request.values.get('To', ''),
callStatus=request.values.get('CallStatus', 'unknown'),
repIds=params['repIds'],
campaignId=params['campaignId']))
@app.route('/demo')
def demo():
return render_template('demo.html')
@app.route('/stats')
def stats():
pwd = request.values.get('password', None)
campaign = data.get_campaign(request.values.get('campaignId', 'default'))
if pwd == app.config['SECRET_KEY']:
return jsonify(aggregate_stats(campaign['id']))
else:
return jsonify(error="access denied")
if __name__ == "__main__":
# load the debugger config
app.config.from_object('config.Config')
app.run(host='0.0.0.0')