-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
409 lines (315 loc) · 10.3 KB
/
server.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import re
import os
import hmac
import json
import logging
import requests
from hashlib import sha1
from functools import wraps
from base64 import b64encode
from datetime import datetime
from urllib.parse import urlencode
from requests.structures import CaseInsensitiveDict
import googlemaps
from flask import url_for, Flask, request, Response as FlaskResponse
from lxml.html import fromstring
import humanize
from twiml import Response
from auth import AUTH, ON_HEROKU
from payphones import PayPhones
app = Flask(__name__)
logging.basicConfig(level=logging.DEBUG)
payphone_client = PayPhones()
gmaps = googlemaps.Client(key=AUTH['GOOGLE_MAPS_DIRECTIONS'])
ID_NUM_DIGITS = 9
ADDRESSTO = os.environ.get('ADDRESSTO', '6c Farnham Street, Bentley')
FULL_STOP = ' . '
REPLACEMENTS = {
'Stn': 'Station',
'Ave': 'Avenue',
'Rd': 'Road',
'Sq': 'Square'
}
REPLACEMENT_RE = re.compile(
r'(\W|^)({})(\W|$)'.format('|'.join(REPLACEMENTS))
)
TRANSIT_STEP_TEMPLATE = (
'Take the {route_name} from stop "{departure_stop}", towards "{towards}" '
'at {departure_time}, and disembark at "{arrival_stop}" after {duration}'
)
def twiml(func):
@wraps(func)
def wrapper(*args, **kwargs):
res = func(*args, **kwargs)
return FlaskResponse(res.toxml(), mimetype='text/xml')
return wrapper
def params_and_url_for(endpoint, params):
return url_for(endpoint) + '?' + urlencode(params)
@app.route('/location/id_recieved', methods=['POST'])
@twiml
def id_recieved():
return id_recieved_response(request.form['Digits'])
def id_recieved_response(digits):
res = Response()
logging.info('Digits: "%s"', digits)
if not re.match(r'\d{%d}' % ID_NUM_DIGITS, digits):
return res.say('Invalid eye d number').hangup()
if digits == ''.join(map(str, range(1, ID_NUM_DIGITS+1))):
return res.play(
'/static/Gorillaz%20-%20Film%20Music%20(Official%20Visual).mp3'
).hangup()
# insert a wildcard where the punctuation is in the phone id
payphone_id = digits[:-1] + '%' + digits[-1]
logging.info('Looking for payphone with id like: "%s"', payphone_id)
payphones = payphone_client.by_cabinet_id(payphone_id)
payphones = [
CaseInsensitiveDict(payphone['properties'])
for payphone in payphones
]
if not payphones:
return res.say('Payphone could not be found').hangup()
if len(payphones) > 1:
return select_from_payphones(payphones)
else:
properties = CaseInsensitiveDict(payphones[0])
message = 'Payphone found in {}'.format(properties['SSC_Name'])
res.say(message)
logging.info(message)
return do_for_payphone(res, format_lat_lon(properties))
def select_from_payphones(payphones):
res = Response()
action = params_and_url_for(
'select_payphone_suburb',
{
'phones': json.dumps([
format_lat_lon(payphone)
for payphone in payphones
])
}
)
with res.gather(numDigits=1, action=action) as g:
g.say(
'{} payphones were found. Please select your current suburb'
'from the following'
.format(len(payphones))
)
for idx, payphone in enumerate(payphones, 1):
g.say('Press {} for {}'.format(
idx,
payphone['SSC_NAME']
))
return res
format_lat_lon = '{Latitude}, {Longitude}'.format_map
@app.route('/select_payphone_suburb', methods=['POST'])
@twiml
def select_payphone_suburb():
return select_payphone_suburb_response(
int(request.form['Digits']),
json.loads(request.args['phones'])
)
def select_payphone_suburb_response(idx, payphones):
res = Response()
try:
payphone = payphones[idx - 1] # we 1 index for useability
except IndexError:
return res.say('Invalid suburb selection').hangup()
logging.info('Selected payphone at latlon %s', payphone)
res.say('Selected payphone')
return do_for_payphone(res, payphone)
def do_for_payphone(res, latlon):
action = params_and_url_for(
'payphone_found',
{'latlon': latlon}
)
with res.gather(numDigits=1, action=action) as g:
g.say(
'Please enter, 1 for walking instructions, or 2 for public '
'transportation instructions'
)
return res
def _replace_part(match):
return '{}{}{}'.format(
match.group(1),
REPLACEMENTS[match.group(2)],
match.group(3)
)
def checksum(url, incoming, auth_token):
calced = url + ''.join(map(''.join, sorted(incoming.items())))
logging.info('Raw: %s', calced)
calced = hmac.new(
auth_token.encode(),
calced.encode(),
sha1
)
return b64encode(calced.digest()).decode()
def only_from_twilio(func):
@wraps(func)
def wrapper(*args, **kwargs):
logging.info(request.args)
url = request.url
# handle cloudflare ssl proxying
if "Cf-Ray" in request.headers:
url = url.replace('http:', 'https:')
calced = checksum(
url,
request.form,
AUTH['TWILIO_AUTH_TOKEN']
)
signature = request.headers['X-Twilio-Signature']
logging.info('Calced: %s', calced)
logging.info('Proved: %s', signature)
if calced != signature:
return 'Bad signature', 403
return func(*args, **kwargs)
return wrapper
@app.route('/speech', methods=['GET', 'POST'])
@only_from_twilio
def speech():
mimetype = 'audio/wav'
res = requests.get(
'https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize',
params={
'text': request.values['text'],
'accept': mimetype,
'voice': 'en-US_AllisonVoice'
},
auth=(AUTH['SPEECH_USERNAME'], AUTH['SPEECH_PASSWORD']),
stream=True
)
return FlaskResponse(res.raw, mimetype=mimetype)
def parse_instruction(instruction):
# strip out html tags
instruction = fromstring(instruction)
for inst in reversed(list(instruction.iter())):
if inst.text and inst.tag in {'p', 'div'}:
if inst.getchildren():
last = inst.getchildren()[-1]
if last.tail:
last.tail += FULL_STOP
else:
last.text += FULL_STOP
else:
inst.text += FULL_STOP
# convert XML tree to plain string
instruction = ''.join(instruction.itertext()).strip()
# remove excess whitespace
instruction = ' '.join(instruction.split())
# mend weird pattern
instruction = instruction.replace('. .', '.')
instruction = mend_short_place_names(instruction)
logging.info('Instruction: %s', instruction)
return instruction.strip()
def mend_short_place_names(instruction):
# replace short versions of address parts with their full versions
# ie, Stn -> Station
return REPLACEMENT_RE.sub(_replace_part, instruction)
@app.route('/location/payphone_found', methods=['POST'])
@twiml
def payphone_found():
return payphone_found_response(
request.values['Digits'],
request.args['latlon']
)
def payphone_found_response(digits, from_):
res = Response()
if digits not in {'1', '2'}:
return res.say('Invalid input').hangup()
mode = {'1': 'walking', '2': 'transit'}[digits]
to = ADDRESSTO
departure_time = datetime.now()
logging.info(
'Travelling from %s to "%s" at %s using %s',
from_,
to,
departure_time,
mode
)
directions_result = gmaps.directions(
from_,
to,
mode=mode,
departure_time=departure_time
)
if not directions_result:
return res.say('No routes could be found').hangup()
directions_result = directions_result[0]
for leg in directions_result['legs']:
for step in leg['steps']:
if step['travel_mode'] == 'TRANSIT':
instruction = parse_transit_step(step)
else:
instruction = parse_instruction(step['html_instructions'])
res.say(instruction)
res.pause(length=1)
res.say("End of instructions")
action = params_and_url_for(
'possibly_repeat',
{
'latlon': from_,
'Digits': digits
}
)
with res.gather(numDigits=1, action=action) as gat:
gat.say(
'Enter 1 to repeat instructions, or hang up.',
)
return res
@app.errorhandler(500)
@app.errorhandler(400)
@twiml
def error(exc=None):
return (
Response()
.say("I'm sorry, something seems to have gone wrong. Goodbye")
.hangup()
)
@app.route('/possibly_repeat', methods=['POST'])
@twiml
def possibly_repeat():
digits = request.form['Digits']
if digits == '1':
return payphone_found_response(
request.args['Digits'],
request.args['latlon']
)
return Response().say('Okay, goodbye').hangup()
def parse_transit_step(step):
transit_details = step['transit_details']
text = TRANSIT_STEP_TEMPLATE.format(
route_name=transit_details['line']['short_name'],
departure_stop=transit_details['departure_stop']['name'],
towards=transit_details['headsign'],
departure_time=transit_details['departure_time']['text'],
arrival_stop=transit_details['arrival_stop']['name'],
duration=step['duration']['text']
)
text = re.sub(
r'\w?mins\w?',
'minutes',
text
)
text = mend_short_place_names(text)
logging.info('Transit instruction: %s', text)
return text
@app.route('/location', methods=['POST'])
@twiml
def location():
res = Response()
with res.gather(numDigits=ID_NUM_DIGITS,
action=url_for('id_recieved')) as g:
g.say(
'Please enter the {} digit payphone identification number'
.format(humanize.apnumber(ID_NUM_DIGITS))
)
return res
@app.route('/request', methods=['POST'])
def request_twiml():
return location()
@app.route('/')
def index():
return 'Sod off'
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
app.debug = not ON_HEROKU
port = int(os.environ['PORT']) if ON_HEROKU else 5555
app.run(port=port, host='0.0.0.0')