-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlexa.py
215 lines (171 loc) · 6.69 KB
/
Alexa.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
import json
import logging as log
import re
from transithelper.Constants import city_constants
from flask import render_template
from flask_ask import question, statement
from transithelper.intents import CheckIntent
from transithelper.intents import GetIntent
from transithelper.intents import SetIntent
FULL_NAME = 'full_name'
CITY = 'city'
AGENCY = 'agency'
STOP = 'stop'
ROUTE = 'route'
PRESET = 'preset'
def launch_intent(city):
log.info("city=%s" % city)
example_agency = city_constants[city]['example_agency']
example_route = city_constants[city]['example_route']
example_stop = city_constants[city]['example_stop']
agencies, num_agencies = generate_agencies(city)
welcome_text = render_template('welcome', city=city, num_agencies=num_agencies, agencies=agencies,
agency=example_agency, route=example_route, stop=example_stop)
return question(welcome_text) \
.simple_card('Welcome to %sTransit' % city, remove_html(welcome_text)) \
.reprompt(render_template('help', city=city, agency=example_agency, route=example_route, stop=example_stop))
def help_intent(city):
log.info("city=%s" % city)
example_agency = city_constants[city]['example_agency']
example_route = city_constants[city]['example_route']
example_stop = city_constants[city]['example_stop']
website = city_constants[city]['website']
help_text = render_template('help', city=city, agency=example_agency, route=example_route, stop=example_stop)
help_card = render_template('help_card', agency=example_agency, route=example_route, stop=example_stop,
website=website)
return question(help_text).simple_card('%sTransit Help' % city, help_card)
def stop_intent():
return statement('ok')
def check_intent(request, city, route, stop, agency):
log.info("city=%s" % city)
log.info('Request object = %s' % request)
city_full = city_constants[city]['full_name']
if request['dialogState'] != 'COMPLETED':
return delegate_dialog()
param_map, ret_value = check_params(request, {ROUTE: route, STOP: stop, AGENCY: agency})
if not param_map:
return ret_value
else:
route = param_map[ROUTE]
stop = param_map[STOP]
agency = param_map[AGENCY]
message = CheckIntent.check(route, stop, agency, city_full)
log.info('Response message = %s', message)
return generate_statement_card(message, 'Check Status')
def set_intent(request, city, user, route, stop, preset, agency):
log.info("city=%s" % city)
log.info('Request object = %s' % request)
city_full = city_constants[city]['full_name']
if request['dialogState'] != 'COMPLETED':
return delegate_dialog()
preset = preset.upper()
param_map, ret_value = check_params(request, {ROUTE: route, STOP: stop, PRESET: preset, AGENCY: agency})
if not param_map:
return ret_value
else:
route = param_map[ROUTE]
stop = param_map[STOP]
preset = param_map[PRESET]
agency = param_map[AGENCY]
message = SetIntent.add(user, route, stop, preset, agency, city_full)
log.info('Response message = %s', message)
return generate_statement_card(message, 'Set Status')
def get_intent(request, city, user, preset, agency):
log.info("city=%s" % city)
log.info('Request object = %s' % request)
city_full = city_constants[city]['full_name']
if request['dialogState'] != 'COMPLETED':
return delegate_dialog()
if not preset:
preset = 'A'
param_map, ret_value = check_params(request, {PRESET: preset, AGENCY: agency})
if not param_map:
return ret_value
else:
preset = param_map[PRESET]
agency = param_map[AGENCY]
# agency = ('%s-%s' % (city, agency)).replace(' ', '-')
message = GetIntent.get(user, preset, agency, city_full)
log.info('Response message = %s', message)
return generate_statement_card(message, 'Get Status')
def generate_agencies(city):
agencies = city_constants[city]['agencies']
length = len(agencies.split(','))
if 'and' in agencies:
length += 1
return agencies, length
def remove_html(text):
text = re.sub(r'<[^<]*?>', '', text)
text = re.sub(r'\\n', ' ', text)
text = re.sub(r'\s+', ' ', text)
return text.strip()
def check_params(request, params_map):
for map_key in params_map.keys():
log.info('%s=%s' % (map_key, params_map[map_key]))
if map_key == 'route':
if params_map[map_key] == '?':
return None, request_slot('route')
try:
params_map[map_key] = find_parameter_resolutions(request, map_key) or params_map[map_key]
except KeyError:
return None, request_slot('route')
elif map_key == 'stop':
if not re.match(r'\d+', params_map[map_key]):
return None, request_slot('stop')
elif map_key == 'preset':
try:
params_map[map_key] = find_parameter_resolutions(request, map_key) or params_map[map_key]
except KeyError:
return None, request_slot('preset')
elif map_key == 'agency':
try:
params_map[map_key] = find_parameter_resolutions(request, map_key)
except KeyError:
return None, request_slot('agency')
else:
pass
return params_map, None
def request_slot(slot):
return json.dumps({
'response': {
'outputSpeech': {
'type': 'PlainText',
'text': 'Which %s?' % slot
},
'directives': [
{
'type': 'Dialog.ElicitSlot',
"slotToElicit": slot
}
],
'shouldEndSession': False
},
'sessionAttributes': {}
})
def delegate_dialog():
return json.dumps({
'response': {
'directives': [
{
'type': 'Dialog.Delegate'
}
],
'shouldEndSession': False
},
'sessionAttributes': {}
})
def generate_statement_card(speech, title):
return statement(speech).simple_card(title, remove_html(speech))
def find_parameter_resolutions(request, param):
slots = request['intent']['slots']
slot = slots[param]
if 'resolutions' not in slot:
return None
for resolution in slot['resolutions']['resolutionsPerAuthority']:
try:
if resolution['status']['code'] == 'ER_SUCCESS_MATCH':
for value in resolution['values']:
return value['value']['name']
except KeyError or IndexError:
continue
raise KeyError