forked from unitedstates/contact-congress
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.py
384 lines (273 loc) · 10.2 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
from gevent.monkey import patch_all
patch_all()
import random
import urlparse
import requests
from threading import Thread
from datetime import datetime, timedelta
import pystache
import twilio.twiml
from flask import (abort, after_this_request, Flask, request, render_template,
url_for)
from flask_cache import Cache
from flask_jsonpify import jsonify
from raven.contrib.flask import Sentry
from twilio import TwilioRestException
from models import db, aggregate_stats, log_call, call_count
from political_data import PoliticalData
app = Flask(__name__)
app.config.from_object('config.ConfigProduction')
cache = Cache(app, config={'CACHE_TYPE': 'simple'})
sentry = Sentry(app)
db.init_app(app)
call_methods = ['GET', 'POST']
data = PoliticalData()
def make_cache_key(*args, **kwargs):
path = request.path
args = str(hash(frozenset(request.args.items())))
return (path + args).encode('utf-8')
def play_or_say(resp_or_gather, msg_template, **kwds):
# take twilio response and play or say a mesage
# can use mustache templates to render keword arguments
msg = pystache.render(msg_template, kwds)
if msg.startswith('http'):
resp_or_gather.play(msg)
elif msg:
resp_or_gather.say(msg)
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'])
if not campaign:
return None, None
# 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']:
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 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)
if not params or not campaign:
abort(404)
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)
if not params or not campaign:
abort(404)
# initiate the call
try:
call = app.config['TW_CLIENT'].calls.create(
to=params['userPhone'],
from_=random.choice(campaign['numbers']),
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)
#result = jsonify(message=err.msg.split(':')[1].strip())
result.status_code = 500
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 not params or not campaign:
abort(404)
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)
if not params or not campaign:
abort(404)
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)
if not params or not campaign:
abort(404)
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)
if not params or not campaign:
abort(404)
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 {} in make_single_call()'.format(
i, full_name, congress_phone, params['userPhone'])
resp.dial(congress_phone, callerId=params['userPhone'],
timeLimit=app.config['TW_TIME_LIMIT'],
timeout=app.config['TW_TIMEOUT'], hangupOnStar=True,
action=url_for('call_complete', **params))
return str(resp)
@app.route('/call_complete', methods=call_methods)
def call_complete():
params, campaign = parse_params(request)
if not params or not campaign:
abort(404)
log_call(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, _ = parse_params(request)
if not params:
abort(404)
return jsonify({
'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')
@cache.cached(timeout=60)
@app.route('/count')
def count():
@after_this_request
def add_expires_header(response):
expires = datetime.utcnow()
expires = expires + timedelta(seconds=60)
expires = datetime.strftime(expires, "%a, %d %b %Y %H:%M:%S GMT")
response.headers['Expires'] = expires
return response
campaign = request.values.get('campaign', 'default')
return jsonify(campaign=campaign, count=call_count(campaign))
@cache.cached(timeout=60, key_prefix=make_cache_key)
@app.route('/stats')
def stats():
password = request.values.get('password', None)
campaign = request.values.get('campaign', 'default')
if password == app.config['SECRET_KEY']:
return jsonify(aggregate_stats(campaign))
else:
return jsonify(error="access denied")
def open_website(url, meta):
return requests.post(url, params={'description': 'Call succesfully completed', 'meta': meta})
@app.route('/eff_test')
def eff_test():
callback_url = request.values.get('callback_url', None)
meta = request.values.get('meta', None)
if callback_url:
t = Thread(target=open_website, args=[callback_url, meta])
t.daemon = True
t.start()
return jsonify(message="success")
if __name__ == '__main__':
# load the debugger config
app.config.from_object('config.Config')
app.run(host='0.0.0.0')