-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
189 lines (139 loc) · 6.75 KB
/
main.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
from flask import Flask, request, render_template
from json import dumps
from random import choice
from database import *
app = Flask(__name__)
session_storage = {}
@app.route('/')
def main():
return render_template('main.html')
@app.route('/post', methods=['POST'])
def post():
return dumps(handler(request.json))
def handler(req):
global session_storage
user_id = req['session']['user_id']
response = {
'version': req['version'],
'session': req['session'],
'response': {
'end_session': 'false'
}
}
if req['session']['new']:
print(1, req['request']['original_utterance'])
session_storage[user_id] = {
'location': '2',
'country': ''
}
text = choice(alice_phrase1)
response['response']['text'] = text
response['response']['tts'] = text
response['response']['buttons'] = get_suggests('yes/no')
response['response']['location'] = '2'
elif user_id in session_storage.keys() and session_storage[user_id]['location'] == '2':
print(2, req['request']['original_utterance'])
if req['request']['original_utterance'].lower() in user_access:
session_storage[user_id]['location'] = '3'
text = choice(alice_phrase2)
response['response']['text'] = text
response['response']['tts'] = text
response['response']['buttons'] = get_suggests('countries')
elif req['request']['original_utterance'].lower() in user_refused:
session_storage[user_id]['location'] = '2.1'
text = choice(alice_phrase_dop1)
response['response']['text'] = text
response['response']['tts'] = text
response['response']['buttons'] = get_suggests('ready')
elif user_id in session_storage.keys() and session_storage[user_id]['location'] == '2.1':
print(2.1, req['request']['original_utterance'])
if req['request']['original_utterance'].lower() in user_ready:
session_storage[user_id]['location'] = '3'
text = choice(alice_phrase2)
response['response']['text'] = text
response['response']['tts'] = text
response['response']['buttons'] = get_suggests('countries')
elif user_id in session_storage.keys() and session_storage[user_id]['location'] == '3':
print(3, req['request']['original_utterance'].lower().title())
if req['request']['original_utterance'].lower().title() in countries.keys():
session_storage[user_id]['location'] = '4'
country = req['request']['original_utterance'].lower().title()
session_storage[user_id]['country'] = country
text = choice(alice_phrase3).format(countries[session_storage[user_id]['country']][0][1])
response['response']['text'] = text
response['response']['tts'] = text
response['response']['buttons'] = get_suggests('yes/no')
response['response']['card'] = get_card(session_storage[user_id]['country'], text, 0)
else:
text = choice(alice_phrase_dop2)
response['response']['text'] = text
response['response']['tts'] = text
response['response']['buttons'] = get_suggests('countries')
elif user_id in session_storage.keys() and session_storage[user_id]['location'] == '4':
print(4, req['request']['original_utterance'])
if req['request']['original_utterance'].lower() in user_access:
session_storage[user_id]['location'] = '5'
text = choice(alice_phrase4).format(countries[session_storage[user_id]['country']][1][1])
response['response']['text'] = text
response['response']['tts'] = text
response['response']['buttons'] = get_suggests('yes/no')
response['response']['card'] = get_card(session_storage[user_id]['country'], text, 1)
else:
text = choice(alice_phrase_dop3)
del session_storage[user_id]
response['response']['text'] = text
response['response']['tts'] = text
response['response']['end_session'] = 'true'
elif user_id in session_storage.keys() and session_storage[user_id]['location'] == '5':
print(5, req['request']['original_utterance'])
if req['request']['original_utterance'].lower() in user_access:
session_storage[user_id]['location'] = '6'
text = choice(alice_phrase5).format(countries[session_storage[user_id]['country']][2][1])
response['response']['text'] = text
response['response']['tts'] = text
response['response']['buttons'] = get_suggests('yes/no')
response['response']['card'] = get_card(session_storage[user_id]['country'], text, 2)
else:
text = choice(alice_phrase_dop3)
del session_storage[user_id]
response['response']['text'] = text
response['response']['tts'] = text
response['response']['end_session'] = 'true'
elif user_id in session_storage.keys() and session_storage[user_id]['location'] == '6':
print(6, req['request']['original_utterance'])
if req['request']['original_utterance'].lower() in user_access:
session_storage[user_id]['location'] = '3'
text = choice(alice_phrase2)
response['response']['text'] = text
response['response']['tts'] = text
response['response']['buttons'] = get_suggests('countries')
else:
text = choice(alice_phrase_dop3)
del session_storage[user_id]
response['response']['text'] = text
response['response']['tts'] = text
response['response']['end_session'] = 'true'
else:
response['response']['text'] = text_error
response['response']['tts'] = text_error
response['response']['end_session'] = 'true'
return response
def get_suggests(what):
if what == 'yes/no':
suggests = list({'title': button, 'hide': True} for button in user_yes_no)
elif what == 'countries':
suggests = list({'title': button, 'hide': True} for button in countries.keys())
elif what == 'ready':
suggests = list({'title': button, 'hide': True} for button in user_ready)
else:
suggests = []
return suggests
def get_card(country, text, num):
return {
'type': 'BigImage',
'image_id': countries[country][num][2],
'title': countries[country][num][0],
'description': text
}
if __name__ == '__main__':
app.run()