-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
449 lines (373 loc) · 21.4 KB
/
bot.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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
# async lib
import asyncio
import aiohttp
# standard python lib
import os
import json
# bot builder lib
from botbuilder.core import ActivityHandler, TurnContext, CardFactory, MessageFactory, ConversationState, UserState
from botbuilder.core.teams import TeamsActivityHandler
from botbuilder.schema import ChannelAccount, HeroCard, CardImage, CardAction, ActionTypes, ActivityTypes
# state-related lib
from state_management import ConversationData, UserProfile
class MyBot(TeamsActivityHandler):
def __init__(self, user_state: UserState, conversation_state: ConversationState):
self.conversation_state = conversation_state
self.user_state = user_state
self.conversation_state_accessor = self.conversation_state.create_property("ConversationData")
self.user_state_accessor = self.user_state.create_property("UserProfile")
self.user_profile = None
self.conversation_data = None
# secret reset
self.reset = 'Ocy00Ulahd87wkKOiNAiKorFlViI2'
# API endpoint
self.API_base = 'https://project-ujiyan.azurewebsites.net/'
# See https://aka.ms/about-bot-activity-message to learn more about the message and other activity types.
async def on_turn(self, turn_context: TurnContext):
await super().on_turn(turn_context)
await self.conversation_state.save_changes(turn_context)
await self.user_state.save_changes(turn_context)
async def reset_all_state(self):
self.user_profile.student_id = None
self.user_profile.student_name = None
self.conversation_data.problem_set = []
self.conversation_data.counter = 1
self.conversation_data.answers = {}
self.conversation_data.test_ID = ''
self.conversation_data.test_title = ''
self.conversation_data.on_test_session = False
self.conversation_data.on_submit_session = False
self.conversation_data.on_register_complete = False
self.conversation_data.on_complete_answer = False
self.already_welcome = False
async def reset_and_submit(self):
# aiohttp session
session = aiohttp.ClientSession()
# sumbit
submit_url = os.path.join(self.API_base,'submissions','create')
# answer dict creation
answers = []
_keys = sorted(self.conversation_data.answers.keys())
for _key in _keys:
answers.append({'answer':self.conversation_data.answers[_key]['ans'],
'problem_id':self.conversation_data.answers[_key]['q_id']})
params = {'student_id':self.user_profile.student_id, 'test_id':self.conversation_data.test_ID, 'submissions': answers}
# print(submit_url)
# print(params)
resp = await session.post(submit_url, json=params)
# print(resp.status)
await session.close()
# reset conversation state
self.conversation_data.problem_set = []
self.conversation_data.counter = 1
self.conversation_data.answers = {}
self.conversation_data.test_ID = ''
self.conversation_data.test_title = ''
self.conversation_data.on_test_session = False
self.conversation_data.on_submit_session = False
async def asign_test_ID(self, test_id):
self.conversation_data.test_ID = test_id
async def switch_on_test_session(self):
self.conversation_data.on_test_session = ~self.conversation_data.on_test_session
async def switch_on_submit_session(self):
self.conversation_data.on_submit_session = ~self.conversation_data.on_submit_session
async def parse_problem_set(self, json_dump):
self.conversation_data.test_title = json_dump['title']
# print(json_dump['problems'])
self.conversation_data.problem_set.extend(json_dump['problems'])
# print(self.problem_set)
async def get_problems(self, problem_id):
target_path = os.path.join(self.API_base, 'tests', problem_id)
# target_path = self.API_base
# print(target_path)
session = aiohttp.ClientSession()
async with session.get(target_path) as resp:
status = resp.status
# print(status)
response = await resp.json()
await session.close()
return status, response
async def register_student(self):
session = aiohttp.ClientSession()
path = self.API_base
path = os.path.join(path,'students')
async with session.post(path, json={'name':self.user_profile.student_name}) as resp:
data = await resp.json()
self.user_profile.student_id = data['id']
await session.close()
async def get_student_id(self):
return self.user_profile.student_id
async def count_up(self):
self.conversation_data.counter = min(self.conversation_data.counter+1, len(self.conversation_data.problem_set))
async def count_down(self):
self.conversation_data.counter = max(self.conversation_data.counter-1, 1)
async def get_stored_answer(self):
return self.conversation_data.answers[-1].lower()
async def update_collected_answer(self, answer, q_id):
self.conversation_data.answers[str(self.conversation_data.counter)] = {'q_id':q_id, 'ans':answer}
await self.count_up()
async def check_collected_answer(self, turn_context: TurnContext):
if len(self.conversation_data.answers.keys()) == len(self.conversation_data.problem_set):
# print(self.conversation_data.answers.keys())
# print(len(self.conversation_data.problem_set))
await turn_context.send_activity("All questions have been answered. Please type 'submit' for submission.")
async def on_message_activity_commented(self, turn_context: TurnContext):
# first and foremost, retreive data from memory state
self.user_profile = await self.user_state_accessor.get(turn_context, UserProfile)
# user_profile = await self.user_state_accessor.get(turn_context, UserProfile)
self.conversation_data = await self.conversation_state_accessor.get(turn_context, ConversationData)
# conversation_data = await self.conversation_state_accessor.get(turn_context, ConversationData)
# already welcomed or not?
if not self.conversation_data.already_welcome:
self.conversation_data.already_welcome = True
await turn_context.send_activity("Hello and welcome to this test-taking chatbot! Please input your name to register.")
return
await turn_context.send_activity(f"{turn_context.activity.channel_id}")
await turn_context.send_activity(f"You said {turn_context.activity.text}")
if turn_context.activity.value is not None:
await turn_context.send_activity(f"Type {type(turn_context.activity.value)}")
_response = json.loads(turn_context.activity.value)
await turn_context.send_activity(f"Type {type(_response)}")
_response = _response['ans']
await turn_context.send_activity(f"The value {_response}")
card = HeroCard(
title="This is what you said:",
text="{}".format(turn_context.activity.text),
buttons=[
CardAction(type=ActionTypes.message_back, title='Yes', text='Yes', value={'ans': 'False'}),
CardAction(type=ActionTypes.message_back, title='Yes', text='No', display_text='What', value='Test'),
CardAction(type=ActionTypes.message_back, title='Yes', text='{"ans":"False"}', display_text='Yes', value='Test')
# CardAction(type=ActionTypes.message_back, title='No', value={'ans': 'False'})
]
)
await turn_context.send_activity(MessageFactory.attachment(CardFactory.hero_card(card)))
async def on_message_activity(self, turn_context: TurnContext):
# first and foremost, retreive data from memory state
self.user_profile = await self.user_state_accessor.get(turn_context, UserProfile)
# user_profile = await self.user_state_accessor.get(turn_context, UserProfile)
self.conversation_data = await self.conversation_state_accessor.get(turn_context, ConversationData)
# conversation_data = await self.conversation_state_accessor.get(turn_context, ConversationData)
# this is bad code practice with no meaning
# but I will leave it here
# await turn_context.send_activity(f"{ turn_context.activity.text }")
# if turn_context.activity.text is not None:
# user_input = turn_context.activity.text
# else:
# user_input = None
# await turn_context.send_activity(f"{turn_context.activity.channel_id}")
# special reset
if turn_context.activity.text is not None:
if turn_context.activity.text.strip() == self.reset:
await turn_context.send_activity("Resetting...")
await self.reset_all_state()
await turn_context.send_activity("Hello and welcome to this test-taking chatbot! Please input your name to register.")
return
# already welcomed or not?
if not self.conversation_data.already_welcome:
self.conversation_data.already_welcome = True
await turn_context.send_activity("Hello and welcome to this test-taking chatbot! Please input your name to register.")
return
if not self.conversation_data.on_register_complete:
await self.__send_registration_card(turn_context)
elif (not self.conversation_data.on_test_session and not self.conversation_data.on_submit_session) and turn_context.activity.text.strip()[:2]!='id':
# await turn_context.send_activity(f"{ turn_context.activity.text.strip()[:2] }")
await self.__send_intro_card(turn_context)
# check test ID
elif (not self.conversation_data.on_test_session and not self.conversation_data.on_submit_session) and turn_context.activity.text.strip()[:2]=='id':
test_id = turn_context.activity.text.strip()[2:]
if len(test_id) == 8:
await turn_context.send_activity("Fetching the requested test id...")
status, to_parse = await self.get_problems(test_id)
# await turn_context.send_activity(f"{ status }")
if status == 404:
await turn_context.send_activity(f"Test ID of {test_id} is not found. Please insert the correct test ID.")
else:
await self.asign_test_ID(test_id)
await self.parse_problem_set(to_parse)
await turn_context.send_activity(f"Test titled {to_parse['title'].capitalize()} is found. There are {len(self.conversation_data.problem_set)} question(s). To submit your answers, type 'submit'. Please type anything to start the test.")
await self.switch_on_test_session()
else:
await turn_context.send_activity("Test ID should be 8-digits number. Please re-enter the test ID.")
# start test session
elif self.conversation_data.on_test_session and not self.conversation_data.on_submit_session:
if turn_context.activity.text is not None and turn_context.activity.value is None:
if turn_context.activity.text.strip().lower() == 'submit':
await self.switch_on_test_session()
await self.switch_on_submit_session()
await self.__on_submit_activity(turn_context)
# await self.conversation_state.save_changes(turn_context)
# await self.user_state.save_changes(turn_context)
return
else:
await self.__send_question_card(turn_context)
elif turn_context.activity.value is not None:
if turn_context.activity.channel_id == 'line':
_value = json.loads(turn_context.activity.value['data'])
elif turn_context.activity.channel_id == 'msteams':
_value = turn_context.activity.value
elif turn_context.activity.channel_id == 'facebook':
_value = json.loads(turn_context.activity.value)
else:
_value = turn_context.activity.value
_answer = _value['ans']
_context = _value['msg']
_question_id = _value['q_id']
if _answer == 'BACK':
await self.count_down()
await turn_context.send_activity("Here is the previous question")
elif _answer == 'NEXT':
await self.count_up()
await turn_context.send_activity("Here is the next question")
else:
await turn_context.send_activity(f"{ _context }")
await self.update_collected_answer(_answer, _question_id)
await turn_context.send_activity(f"{len(self.conversation_data.answers.keys())}/{len(self.conversation_data.problem_set)} question(s) have been answered.")
await self.__send_question_card(turn_context)
else:
await self.__send_question_card(turn_context)
await self.check_collected_answer(turn_context)
# start submission session
elif not self.conversation_data.on_test_session and self.conversation_data.on_submit_session:
await self.__on_submit_activity(turn_context)
# await self.conversation_state.save_changes(turn_context)
# await self.user_state.save_changes(turn_context)
async def on_members_added_activity(
self,
members_added: ChannelAccount,
turn_context: TurnContext
):
self.conversation_data = await self.conversation_state_accessor.get(turn_context, ConversationData)
for member_added in members_added:
if member_added.id != turn_context.activity.recipient.id:
self.conversation_data.already_welcome = True
await turn_context.send_activity("Hello and welcome to this test-taking chatbot! Please input your name to register.")
async def __send_registration_card(self, turn_context: TurnContext):
# try:
# await turn_context.send_activity(f"activit value: {json.loads(turn_context.activity.value)}")
# except:
# pass
# debug. but more like dafuk
# await turn_context.send_activity(f"{turn_context.activity.text}")
# await turn_context.send_activity(f"{turn_context.activity.value}")
# if turn_context.activity.value is not None:
# await turn_context.send_activity(f"{turn_context.activity.value}")
# await turn_context.send_activity(f"{turn_context.activity.value['data']}")
# aa = json.loads(turn_context.activity.value['data'])
# await turn_context.send_activity(f"{aa['ans']}")
# await turn_context.send_activity(f"{'ans' in aa}")
# await turn_context.send_activity(f"{aa['ans'] == 'False'}")
if turn_context.activity.text is not None and turn_context.activity.value is None:
self.user_profile.student_name = turn_context.activity.text.strip()
card = HeroCard(
title="Your name is:",
text="{}".format(self.user_profile.student_name),
buttons=[
CardAction(type=ActionTypes.message_back, title='Yes',
display_text='Yes',
text='{"ans":"True"}' if turn_context.activity.channel_id=='facebook' else None,
value={'ans': 'True'}),
CardAction(type=ActionTypes.message_back, title='No',
display_text='No',
text='{"ans":"False"}' if turn_context.activity.channel_id=='facebook' else None,
value={'ans': 'False'})
]
)
await turn_context.send_activity(MessageFactory.attachment(CardFactory.hero_card(card)))
elif turn_context.activity.value != None:
if turn_context.activity.channel_id == 'line':
ans = json.loads(turn_context.activity.value['data'])['ans']
elif turn_context.activity.channel_id == 'msteams':
ans = turn_context.activity.value['ans']
elif turn_context.activity.channel_id == 'facebook':
ans = json.loads(turn_context.activity.value)['ans']
else:
ans = turn_context.activity.value['ans']
if ans == 'True':
await self.register_student()
student_id = await self.get_student_id()
await turn_context.send_activity(f"Registration complete. Welcome { self.user_profile.student_name }! Here is your student ID {student_id}.")
self.conversation_data.on_register_complete = True
await self.__send_intro_card(turn_context)
else:
await turn_context.send_activity("Please input your name")
else:
await turn_context.send_activity("Please input your name")
async def __on_submit_activity(self, turn_context: TurnContext):
if turn_context.activity.value is None:
await self.__send_submit_card(turn_context)
else:
if turn_context.activity.channel_id == 'line':
_value = json.loads(turn_context.activity.value['data'])
elif turn_context.activity.channel_id == 'msteams':
_value = turn_context.activity.value
else:
_value = turn_context.activity.value
if _value['ans'] == 'SUBMIT':
await self.reset_and_submit()
await turn_context.send_activity("Your answer has been recorded.")
elif _value['ans'] == 'CANCEL':
await self.switch_on_test_session()
await self.switch_on_submit_session()
await self.__send_question_card(turn_context)
await self.check_collected_answer(turn_context)
async def __send_question_card(self, turn_context: TurnContext):
_fetch = self.conversation_data.problem_set[self.conversation_data.counter-1]
_question = _fetch['desc']
_question_id = _fetch['id']
_choices = _fetch['options']
_button = []
for _this in _choices:
_button.append(CardAction(type=ActionTypes.message_back,
title=_this['key']+'. '+_this['value'],
text='{"q_id":{}, "ans":{}, "msg":"Answered with {}."}'.format(_question_id, _this['key'].upper(), _this['value']) \
if turn_context.activity.channel_id=='facebook' else None,
display_text=_this['key']+'. '+_this['value'],
value={'q_id':_question_id, 'ans':_this['key'].upper(), 'msg':f"Answered with '{ _this['value'] }'."}))
if self.conversation_data.counter != 1:
_button.append(CardAction(type=ActionTypes.message_back,
title='Back',
display_text='Back',
text='{"q_id":null, "ans":"BACK", "msg":null}' if turn_context.activity.channel_id=='facebook' else None,
value={'q_id':None, 'ans':'back'.upper(),'msg':None}))
if self.conversation_data.counter != len(self.conversation_data.problem_set):
_button.append(CardAction(type=ActionTypes.message_back,
title='Next',
display_text='Next',
text='{"q_id":null, "ans":"NEXT", "msg":null}' if turn_context.activity.channel_id=='facebook' else None,
value={'q_id':None, 'ans':'next'.upper(),'msg':None}))
card = HeroCard(
title=f"Question '{ self.conversation_data.counter }'.",
text=_question,
buttons=_button
)
return await turn_context.send_activity(MessageFactory.attachment(CardFactory.hero_card(card)))
async def __send_submit_card(self, turn_context: TurnContext):
_keys = sorted(self.conversation_data.answers.keys())
_text = ''
_text = '|| Student name: '+self.user_profile.student_name+' '
for _key in _keys:
_text += f"|| { _key }. { self.conversation_data.answers[_key]['ans'] } "
if len(_keys) < len(self.conversation_data.problem_set):
_text += '|| There are question(s) you have not answered yet. Do you want to submit anyway?'
print(_text)
card = HeroCard(
title="Here are your test summary: ",
text=_text,
buttons=[
CardAction(type=ActionTypes.message_back, title='Submit', value={'ans':'submit'.upper()}),
CardAction(type=ActionTypes.message_back, title='Cancel', value={'ans':'cancel'.upper()})
]
)
return await turn_context.send_activity(MessageFactory.attachment(CardFactory.hero_card(card)))
async def __send_intro_card(self, turn_context: TurnContext):
card = HeroCard(
title=f"Hello { self.user_profile.student_name }!",
text="Welcome to the test-taking bot. "
"To start the test, please reply with the 8-digits test ID "
"starting with 'id' as shown in the example (e.g., id7E4C6B9E). ",
)
return await turn_context.send_activity(
MessageFactory.attachment(CardFactory.hero_card(card))
)