-
Notifications
You must be signed in to change notification settings - Fork 0
/
Response.py
92 lines (86 loc) · 4.3 KB
/
Response.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
class Responser:
def __init__(self, event, dialog):
self.dialog = dialog
self.dialog_type = str(type(self.dialog))[str(type(self.dialog)).index('.') + 1: -2]
self.event = event
def get_response(self):
match self.dialog_type:
case 'Dialog':
return self.get_response_dialog()
case 'MiddleWareDialog':
return self.get_response_dialog()
case 'StartDialog':
return self.get_response_start_dialog()
case 'TimeoutDialog':
return self.get_response_timeout_dialog()
def get_response_dialog(self):
dialog_response = self.dialog.get_response_info(self.event)
try:
return {
"response": {
"text": dialog_response["text"],
"tts": dialog_response["tts"],
"card": dialog_response["card"] if "card" in dialog_response else None,
"buttons": self.create_buttons(dialog_response["buttons"]),
"end_session": dialog_response["end_session"] if "end_session" in dialog_response else False,
},
"session": self.event["session"],
"session_state": dialog_response["session_state"],
"user_state_update": dialog_response["user_state_update"] if "user_state_update" in dialog_response else None,
"version": self.event["version"]
}
except:
return {
"response": {
"text": "Простите, не смогла распознать вашу команду\n"
"Повторите еще раз или обратитесь в помощь или в 'Что ты умеешь'",
"tts": "Простите, не смогла распознать вашу команду"
"Повторите еще раз или обратитесь в помощь или в 'Что ты умеешь'",
"card": None,
"buttons": Responser.create_buttons(['Помощь', 'Что ты умеешь']),
"end_session": False
},
"session": self.event["session"],
"session_state": self.event['state']['session'],
"version": self.event["version"]
}
def get_response_start_dialog(self):
dialog_response = self.dialog.get_response_info(self.event)
return {
"response": {
"text": dialog_response["text"],
"tts": dialog_response["tts"],
"card": dialog_response["card"] if "card" in dialog_response else None,
"buttons": self.create_buttons(dialog_response["buttons"]),
"end_session": dialog_response["end_session"] if "end_session" in dialog_response else False,
},
"session": self.event["session"],
"session_state": dialog_response["session_state"],
"version": self.event["version"]
}
def get_response_timeout_dialog(self):
dialog_response = self.dialog.get_response_info(self.event)
return {
"response": {
"text": dialog_response["text"],
"tts": dialog_response["tts"],
"card": dialog_response["card"] if "card" in dialog_response else None,
"buttons": self.create_buttons(dialog_response["buttons"]),
"end_session": dialog_response["end_session"] if "end_session" in dialog_response else False,
},
"session": self.event["session"],
"session_state": dialog_response["session_state"],
"user_state_update": dialog_response["user_state_update"] if "user_state_update" in dialog_response else None,
"version": self.event["version"]
}
@staticmethod
def create_buttons(buttons):
result = []
for button in buttons:
if isinstance(button, str):
result.append({"title": button, "hide": True})
continue
result.append(button)
return result
def is_authorize(self):
return "access_token" in self.event["session"]["user"]