-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_old.py
83 lines (67 loc) · 3.48 KB
/
index_old.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
from ability_000 import ScheduleParser
def handler(event, context):
"""
Entry-point for Serverless Function.
:param event: request payload.
:param context: information about current execution context.
:return: response to be serialized as JSON.
"""
try:
if 'session' in event and 'message_id' in event['session']:
match event['session']['message_id']:
# Назовите Станцию Отправления
case 0:
text = 'Назовите Станцию Отправления:'
end_session = 'false'
session_dict = None
# Назовите Станцию Прибытия
case 1:
if 'request' in event and \
'original_utterance' in event['request'] \
and len(event['request']['original_utterance']) > 0:
text = 'Назовите Станцию Прибытия:'
end_session = 'false'
session_dict = {'st1': event['request']['original_utterance']}
# Возврат расписания
case 2:
if 'request' in event and \
'original_utterance' in event['request'] \
and len(event['request']['original_utterance']) > 0:
if 'state' in event and \
'session' in event['state'] and \
'st1' in event['state']['session']:
st1 = event['state']['session']['st1']
else:
raise 'State error'
st2 = event['request']['original_utterance']
try:
schedule = ScheduleParser(str(st1), str(st2)).parse()
if schedule["error"] is None:
text = 'Ближайшее отправление: ' + schedule['r1'][0] + ' с прибытием ' + schedule['r1'][1] + \
'. Второе ближайшее отправление: ' + schedule['r2'][0] + ' с прибытием ' + schedule['r2'][1] + \
'. Третье ближайшее отправление: ' + schedule['r3'][0] + ' с прибытием ' + schedule['r3'][1]
end_session = 'true'
session_dict = None
else:
text = schedule['error']
end_session = 'true'
session_dict = None
except Exception as e:
text = str(e)
end_session = 'true'
session_dict = None
except Exception as e:
text = str(e)
end_session = 'true'
session_dict = None
return {
'version': event['version'],
'session': event['session'],
'response': {
# Respond with the original request or welcome the user if this is the beginning of the dialog and the request has not yet been made.
'text': text,
# Don't finish the session after this response.
'end_session': end_session
},
'session_state': session_dict
}