-
Notifications
You must be signed in to change notification settings - Fork 0
/
alexa.py
169 lines (142 loc) · 4.9 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
import json
import avv
import random
from os import path
from flask import Flask
from flask_ask import Ask, request, statement, question, session, context
app = Flask(__name__)
ask = Ask(app, "/")
fileSet = None
fileOpt = None
fileChg = False
fileInit = False
flag = "None"
def initData():
global flag
global fileOpt
global fileChg
global fileSet
global fileInit
fileSet = json.loads(open("Data/option.json").read())
file = None
if not path.exists("User/{0}.json".format(session.user.userId)):
file = open("User/{0}.json".format(session.user.userId), "a")
file.write("""{"favorite":"None"}""")
file.close()
try:
file = open("User/{0}.json".format(session.user.userId), "r")
except FileNotFoundError:
print("ERROR")
fileOpt = json.loads(file.read())
fileInit = True
@ask.launch
def start_skill():
global flag
if not fileInit:
initData()
if (fileOpt["favorite"] == "None"):
speech_text = "Welche Station möchtest du als Favorit auswählen?"
flag = "setFavStation"
else:
speech_text = "Was möchtest du tuhen?"
flag = "None"
return question(speech_text).reprompt(speech_text).simple_card(speech_text)
@ask.intent("AMAZON.CancelIntent")
def AmazonCancelIntent():
pass
@ask.intent("AMAZON.HelpIntent")
def AmazonHelpIntent():
pass
@ask.intent("AMAZON.NavigateHomeIntent")
def AmazonNavigateHomeIntent():
pass
@ask.intent("AMAZON.StopIntent")
def AmazonStopIntent():
return statement("")
@ask.intent("HaltIntent")
def HaltIntent(STATION, TRANSPORT, ARTIKEL):
global flag
global fileOpt
global fileSet
global fileChg
if not fileInit:
initData()
station = None
if STATION != None:
station = avv.searchForStation(STATION)
elif fileOpt["favorite"] == "None":
flag = "setFavStation"
return question("Du hast keinen Favoriten angegeben... Welche Station möchtest du als Favorit auswählen?")
else:
station = avv.searchForStation(fileOpt["favorite"])
data = None
if station["match"]:
data = avv.getStationBoard(station["obj"])
else:
print("Error")
return statement("Das konnte ich leider nicht verstehen!")
sorKeys = sorted(data.keys())
ret_msg = "<speak>"+random.choice(fileSet["intro"].replace(station["obj"]["name"]))
for group in sorKeys:
lines = []
for element in data[group]:
if group == "None":
break
element = data[group][element]
data_lex = {"name": element["type"]["name"], "dirText": element["dirText"], "del": "..."}
# Platform
if group.startswith("H"):
data_lex["pltName"] = group.replace("H.", "Haltestelle ")
else:
data_lex["pltName"] = "Gleis " + str(group)
# Time
time = avv.convertTime(element["dep"]["time"])
data_lex["dep_hours"] = time[0]
data_lex["dep_minutes"] = time[1]
if element["dep"]["time"] != element["dep"]["delay"]:
time_del = avv.convertTime(element["dep"]["delay"])
data_lex["del_hours"] = time_del[0]
data_lex["del_minutes"] = time_del[1]
if time[0] * 60 + time[1] < time_del[0] * 60 + time_del[1]:
data_lex["del_extMin"] = (((24 - time[0]) * 60) + time[1]) + ((time_del[0] * 60) + time_del[1])
data_lex["del"] = random.choice(fileSet["board"]["sentences"]["delMsg"]).format(data_lex)
if len(lines) == 0:
lines.append(random.choice(fileSet["board"]["sentences"]["newPlt"]).format(data_lex))
elif len(lines) >= fileSet["board"]["maxItems"]:
break
else:
lines.append(random.choice(fileSet["board"]["sentences"]["fromPlt"]).format(data_lex))
first = True
for i in lines:
if ret_msg == "<speak>":
ret_msg += i
first = False
elif first:
first = False
ret_msg += """<break time="1.5s"/> """ + i
else:
ret_msg += """<break time="0.5s"/>""" + i
ret_msg += "</speak>"
return statement(ret_msg)
@ask.intent("RouteIntent")
def RouteIntent(DEP,ARR):
global flag
global fileOpt
global fileChg
global fileSet
global fileInit
if not fileInit:
initData()
@ask.intent("StationIntent")
def StationIntent(STATION):
global flag
global fileOpt
global fileChg
if flag == "setFavStation":
fileOpt["favorite"] = STATION
with open("User/{0}.json".format(session.user.userId), 'w') as outfile:
json.dump(fileOpt, outfile)
return statement("Okey... Ich setze {} als Favorite".format(STATION))
return statement("Das konnte ich leider nicht verstehen!")
if __name__ == '__main__':
app.run(debug=True)