-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.py
More file actions
executable file
·330 lines (262 loc) · 12.8 KB
/
message.py
File metadata and controls
executable file
·330 lines (262 loc) · 12.8 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
'''
messaging.py
Author: Liwei Jiang
Date: 24/07/2018
Usage:
Functions that in charge of the interaction between the user and chatbot.
External messaging functions sending data to Facebook Messenger.
'''
import os
import json
import random
import time
import messaging_API
import database as db
import copy
from utils import *
def persistent_menu(template_conversation):
'''
This function sets up the persistence menu.
Args:
template_conversation: the json structure containing the conversation and state templates
Returns:
None
'''
messaging_API.send_persistent_menu(json.dumps(
template_conversation["STATE"]["PERSISTENT_MENU"]))
def init_payload(template_conversation):
'''
This function initializes the payloads.
Args:
template_conversation: the json structure containing the conversation and state templates
Returns:
None
'''
messaging_API.send_get_started(json.dumps(
template_conversation["STATE"]["GET_STARTED"]))
def send_image(mysql, recipient_id, payload, chatbot_text, image_id):
'''
This function sends an image to the specified recipient.
Args:
mysql: database
recipient_id: the recipient's unique id assigned by Facebook Messenger
payload: chatbot's payload state
chatbot_text: the json structure containing the chatbot's text/conversation source
image_id: the name/id of the image being sent, specified in chatbot_text
Returns:
None
'''
image_data = chatbot_text[payload]["image"][image_id]
random.shuffle(image_data["image_url"])
image_data["image_url"][0] = image_data["image_url"][0].format(
os.environ["PORT"])
messaging_API.send_image(mysql, recipient_id, image_data)
def send_congratulation_image(mysql, recipient_id, template_conversation):
'''
This function sends a congratulation image to the specified recipient.
Args:
mysql: database
recipient_id: the recipient's unique id assigned by Facebook Messengerng sent, specified in chatbot_text
template_conversation: the json structure containing the conversation and state templates
Returns:
None
'''
if random.random() < 0.4:
image_data = template_conversation["STATE"]["CONGRATULATION"]["image"]
random.shuffle(image_data["image_url"])
image_data["image_url"][0] = image_data["image_url"][0].format(
os.environ["PORT"])
messaging_API.send_image(mysql, recipient_id, image_data)
def send_paragraph(mysql, recipient_id, payload, chatbot_text, template_conversation, paragraph_id):
'''
This function sends a set of sentences to the specified recipient.
Args:
mysql: database
recipient_id: the recipient's unique id assigned by Facebook Messenger
payload: chatbot's payload state
chatbot_text: the json structure containing the chatbot's text/conversation source
template_conversation: the json structure containing the conversation and state templates
paragraph_id: the name/id of the paragraph being sent, specified in chatbot_text
Returns:
None
'''
paragraph_data = chatbot_text[payload]["paragraph"][paragraph_id]
num_sentence = len(paragraph_data)
for i in range(num_sentence):
message_data = paragraph_data[i]
messaging_API.send_message(
mysql, recipient_id, template_conversation, message_data)
def send_conversation(mysql, recipient_id, payload, chatbot_text, template_conversation, conversation_id):
'''
This function sends a list of texts, with a short delay and typing action in between scentences,
along with a set of quick reply button to continue the conversation, to the specified recipient.
Args:
mysql: database
recipient_id: the recipient's unique id assigned by Facebook Messenger
payload: chatbot's payload state
chatbot_text: the json structure containing the chatbot's text/conversation source
template_conversation: the json structure containing the conversation and state templates
conversation_id: the name/id of the conversation being sent, specified in chatbot_text
Returns:
None
'''
conversation_data = chatbot_text[payload]["conversation"][conversation_id]
message_data = conversation_data["message"]
for msg in message_data[:-1]:
messaging_API.send_message(
mysql, recipient_id, template_conversation, msg)
quick_reply_data = conversation_data["quick_reply"]
messaging_API.send_quick_reply(
mysql, recipient_id, template_conversation, quick_reply_data, message_data[-1])
def send_format_quick_reply_text(mysql, recipient_id, template_conversation, state, format_fill_text):
'''
This function sends a quick reply with formatted text message.
Args:
mysql: database
recipient_id: the recipient's unique id assigned by Facebook Messenger
template_conversation: the json structure containing the conversation and state templates
state: the state's name, specified in template_conversation
format_fill_text: the text to be filled into the placeholder of the message text
Returns:
None
'''
quick_reply_data = copy.deepcopy(template_conversation["STATE"][state]["quick_reply"])
quick_reply_data["message"]["text"] = quick_reply_data["message"]["text"].format(format_fill_text)
uid = messaging_API.send_quick_reply(mysql, recipient_id, template_conversation, quick_reply_data)
# return uid so that we can log the information in the [user_history] dataset when the bot sends a question
return uid
def send_choose_subject(mysql, recipient_id, template_conversation):
'''
This function asks the specified recipient to choose a subject.
Args:
mysql: database
recipient_id: the recipient's unique id assigned by Facebook Messenger
template_conversation: the json structure containing the conversation and state templates
Returns:
'''
quick_reply_data = template_conversation["STATE"]["CHOOSE_SUBJECT"]["quick_reply"]
messaging_API.send_quick_reply(
mysql, recipient_id, template_conversation, quick_reply_data)
def send_question(mysql, recipient_id, template_conversation, qa_model, cache):
'''
This function sends a question to the specified recipient.
Args:
mysql: database
recipient_id (str): the recipient's unique id assigned by Facebook Messenger
template_conversation: the json structure containing the conversation and state templates
qa_model: the question answering model containing information about the question dataset
cache: a dictionary storing useful information in memory for each user
Return:
None
'''
subject = cache[recipient_id]['current_subject'] if cache[recipient_id]['current_subject'] != None else 'random'
question, qid = qa_model.pickQuestion(recipient_id, subject)
message_data_1 = template_conversation["STATE"]["QUESTION"]["message_1"]
message_data_2 = template_conversation["STATE"]["QUESTION"]["message_2"]
messaging_API.send_message(
mysql, recipient_id, template_conversation, message_data_1)
if random.random() < 0.5:
messaging_API.send_message(mysql, recipient_id, template_conversation, message_data_2)
uid = send_format_quick_reply_text(
mysql, recipient_id, template_conversation, "QUESTION", question)
# insert the question to the [user_history] table
db.insert_user_history(mysql, recipient_id, qid[1], subject, begin_uid=uid)
update_cache(cache, recipient_id, current_qid=qid, begin_uid=uid)
def send_say_hi(mysql, recipient_id, template_conversation, recipient_firstname):
'''
This function sends a hi message to the specified recipient.
Args:
mysql: database
recipient_id: the recipient's unique id assigned by Facebook Messenger
template_conversation: the json structure containing the conversation and state templates
recipient_firstname: the recipient's first name
Returns:
None
'''
send_format_quick_reply_text(
mysql, recipient_id, template_conversation, "SAY_HI", recipient_firstname)
def send_correct_answer(mysql, recipient_id, payload, template_conversation, qa_model, cache):
'''
This function sends the correct answer of a question to the specified recipient.
Args:
mysql: database
recipient_id: the recipient's unique id assigned by Facebook Messenger
payload: chatbot's payload state
template_conversation: the json structure containing the conversation and state templates
qa_model: the question answering model containing information about the question dataset
cache: a dictionary containing the information in memory such as current_qid and current_subject needed for the quizbot
Returns:
None
'''
qid = cache[recipient_id]['current_qid']
correct_answer = qa_model.getAnswer(qid[0])
send_format_quick_reply_text(
mysql, recipient_id, template_conversation, "CORRECT_ANSWER", correct_answer)
def send_explanation(mysql, recipient_id, template_conversation, qa_model, cache):
'''
This function sends the explanation of a question to the specified recipient.
Args:
recipient_id: the recipient's unique id assigned by Facebook Messenger
template_conversation: the json structure containing the conversation and state templates
mysql: database
explanation: the explanation of a question to be sent to the recipient
Returns:
None
'''
qid = cache[recipient_id]['current_qid']
explanation_sentence = qa_model.getSupport(qid[0])
message_data = template_conversation["STATE"]["EXPLANATION"]["message"]
messaging_API.send_message(
mysql, recipient_id, template_conversation, message_data)
if cache[recipient_id]['if_explanation_text'] and qid[1] >= 100 and qid[1] < 150:
explanation_sentence = explanation_sentence.split("\n")
explanation_sentence = explanation_sentence[0]
send_format_quick_reply_text(
mysql, recipient_id, template_conversation, "EXPLANATION", explanation_sentence)
def send_hint(mysql, recipient_id, chatbot_text, template_conversation, qa_model, cache):
'''
This function sends a list of hints(distractors) to the specified recipient.
Args:
mysql: database
recipient_id: the recipient's unique id assigned by Facebook Messenger
chatbot_text: the json structure containing the chatbot's text/conversation source
template_conversation: the json structure containing the conversation and state templates
qa_model: the question answering model containing information about the question dataset
cache: a dictionary containing the information in memory such as current_qid and current_subject needed for the quizbot
Returns:
None
'''
qid = cache[recipient_id]['current_qid']
message_text = ""
options = []
index = ["1️⃣", "2️⃣", "3️⃣", "4️⃣",
"5️⃣", "6️⃣", "7️⃣", "8️⃣", "9️⃣", "🔟"]
chatbot_text_local = copy.deepcopy(chatbot_text)
for x in qa_model.DKB[qid[0]]:
options.append({
"content_type": "text",
"title": str(x),
"payload": "BUTTON_DKB_" + str(x)
})
for x in qa_model.AKB[qid[0]]:
options.append({
"content_type": "text",
"title": str(x),
"payload": "BUTTON_AKB_" + str(x)
})
random.shuffle(options)
for i in range(len(options)):
j = len(options) - i - 1
message_text += index[i % 10]
message_text += " "
message_text += str(options[i]["title"])
message_text += "\n"
chatbot_text_local["NEED_HINT"]["conversation"]["conversation_1"]["quick_reply"]["source"].insert(
0, "LOCAL")
chatbot_text_local["NEED_HINT"]["conversation"]["conversation_1"]["quick_reply"]["title"].insert(
0, index[j % 10])
chatbot_text_local["NEED_HINT"]["conversation"]["conversation_1"]["quick_reply"]["payload"].insert(
0, options[j]["payload"])
chatbot_text_local["NEED_HINT"]["conversation"]["conversation_1"]["message"][1]["text"] = message_text
send_conversation(mysql, recipient_id, "NEED_HINT",
chatbot_text_local, template_conversation, "conversation_1")