-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbus_toulouse.py
175 lines (128 loc) · 5.43 KB
/
bus_toulouse.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from ask_sdk_core.skill_builder import SkillBuilder
from ask_sdk_core.utils import is_request_type, is_intent_name
from ask_sdk_model.ui import SimpleCard
from os import environ
from tisseo import prochains_passages
from six import PY2
try:
from HTMLParser import HTMLParser
except ImportError:
from html.parser import HTMLParser
################################################
class SSMLStripper(HTMLParser):
def __init__(self):
self.reset()
self.full_str_list = []
if not PY2:
self.strict = False
self.convert_charrefs = True
def handle_data(self, d):
self.full_str_list.append(d)
def get_data(self):
return ''.join(self.full_str_list)
################################################
_TISSEO_API_KEY = environ['TISSEO_API_KEY']
if _TISSEO_API_KEY == "":
raise KeyError("TISSEO_API_KEY environment variable must be set")
skill_name = "Bus Toulouse"
help_text = ("Vous pouvez demander : "
"Quand passe le prochain bus à l'arrêt Moulin Armand ?")
arret_bus_slot = "arret_bus"
destination_slot = "destination"
ligne_slot = "ligne"
sb = SkillBuilder()
@sb.request_handler(can_handle_func=is_request_type("LaunchRequest"))
def launch_request_handler(handler_input):
# Handler for Skill Launch
speech = "Bienvenue dans la skill des bus de Toulouse."
handler_input.response_builder.speak(
speech + " " + help_text).ask(help_text)
return handler_input.response_builder.response
@sb.request_handler(can_handle_func=is_intent_name("AMAZON.HelpIntent"))
def help_intent_handler(handler_input):
# Handler for Help Intent
handler_input.response_builder.speak(help_text).ask(help_text)
return handler_input.response_builder.response
@sb.request_handler(
can_handle_func=lambda input:
is_intent_name("AMAZON.CancelIntent")(input) or
is_intent_name("AMAZON.StopIntent")(input))
def cancel_and_stop_intent_handler(handler_input):
# Single handler for Cancel and Stop Intent
speech_text = "Au revoir!"
return handler_input.response_builder.speak(speech_text).response
@sb.request_handler(can_handle_func=is_request_type("SessionEndedRequest"))
def session_ended_request_handler(handler_input):
# Handler for Session End
return handler_input.response_builder.response
@sb.request_handler(can_handle_func=is_intent_name(
"demande_des_prochains_passages_a_un_arret"))
def demande_des_prochains_passages_a_un_arret(handler_input):
slots = handler_input.request_envelope.request.intent.slots
arret_bus_demande = None
destination = None
ligne = None
if arret_bus_slot in slots:
arret_bus_demande = slots[arret_bus_slot].value
if destination_slot in slots:
destination = slots[destination_slot].value
if ligne_slot in slots:
ligne = slots[ligne_slot].value
liste_p = prochains_passages(stop_area_name=arret_bus_demande,
destination=destination,
line=ligne)
if len(liste_p) < 1:
speech = "Dans les prochaines heures, \
aucun passage prévu à l'arrêt {}.".format(arret_bus_demande)
else:
speech = "A l'arrêt {}, ".format(arret_bus_demande)
for p in liste_p:
if p is None:
speech = "Dans les prochaines heures, \
aucun passage prévu à l'arrêt {}.".format(arret_bus_demande)
else:
if p.ligne == "A":
speech += "Le métro ligne {},".format(p.ligne)
elif p.ligne == "B":
speech += "Le métro ligne {}".format(p.ligne)
# No comma here because it is pronounced "Bi" and not "Bé"
elif p.ligne in ["T1", "T2"]:
speech += "Le tramway {},".format(p.ligne)
else:
speech += "Le bus {},".format(p.ligne)
speech += " à destination de {}, passera dans {}. ".format(
p.destination, p.timedelta_str)
handler_input.response_builder.speak(speech)
return handler_input.response_builder.response
def convert_speech_to_text(ssml_speech):
# convert ssml speech to text, by removing html tags
s = SSMLStripper()
s.feed(ssml_speech)
return s.get_data()
@sb.global_response_interceptor()
def add_card(handler_input, response):
# Add a card by translating ssml text to card content
response.card = SimpleCard(
title=skill_name,
content=convert_speech_to_text(response.output_speech.ssml))
@sb.global_response_interceptor()
def log_response(handler_input, response):
# Log response from alexa service
print("Alexa Response: {}\n".format(response))
@sb.global_request_interceptor()
def log_request(handler_input):
# Log request to alexa service
print("Alexa Request: {}\n".format(handler_input.request_envelope.request))
@sb.exception_handler(can_handle_func=lambda i, e: True)
def all_exception_handler(handler_input, exception):
# Catch all exception handler, log exception and
# respond with custom message
print("Encountered following exception: {}".format(exception))
speech = "Désolé, je n'ai pas compris. \
Dite aide pour obtenir des exemples d'utilisation."
handler_input.response_builder.speak(speech).ask(speech)
return handler_input.response_builder.response
# Handler to be provided in lambda console.
handler = sb.lambda_handler()