-
Notifications
You must be signed in to change notification settings - Fork 1
/
fb.py
646 lines (579 loc) · 21 KB
/
fb.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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
import requests
import json
import logging_handler
from alphabet_detector import AlphabetDetector
import logging
from datetime import date, timedelta
from config import FB_TOKEN, PROJECT_ID
from gcp import (
access_secret_version,
store_skill_request,
store_context,
retrieve_context,
store_progress_skill,
get_progress_skills,
get_progress_skill,
delete_progress_skill,
save_notification,
)
ACCESS_TOKEN = access_secret_version(PROJECT_ID, FB_TOKEN["name"], FB_TOKEN["version"])
skill_selection_text = "Send me the name of the skill you want to learn or simply select one of the already available skills below: "
level_selection_text = "Perfect! What is your approximate level of knowledge in "
time_selection_text = "How much time for learning this topic do you have per week?"
ad = AlphabetDetector()
def handleOptin(sender_psid, received_message):
if received_message.get("type") == "one_time_notif_req":
token = received_message.get("one_time_notif_token")
payload = received_message.get("payload")
skill = payload.split("_")[3]
assignment = payload.split("_")[4]
user_time = float(payload.split("_")[5])
course_time = float(payload.split("_")[6])
time_per_day = user_time / 7.0
days_to_complete = int(course_time / time_per_day)
if days_to_complete > 10:
days_to_complete = 10
notif_date = date.today() + timedelta(days=days_to_complete)
notif_date = str(notif_date.year) + str(notif_date.month) + str(notif_date.day)
save_notification(sender_psid, skill, assignment, notif_date, token)
request = {
"text": "All right! I will check up on you after "
+ str(days_to_complete)
+ " days."
}
callSendAPI(sender_psid, request)
def handleMessage(sender_psid, received_message):
context = retrieve_context(sender_psid)
store_context(sender_psid, "")
if received_message.get("quick_reply"):
payload = received_message["quick_reply"]["payload"]
if "next_page" in payload:
generate_quick_reply_pagination(payload, sender_psid)
return
elif "previous_page" in payload:
generate_quick_reply_pagination(payload, sender_psid)
return
elif "skill_selection" in payload:
generate_quick_reply_skill(payload, sender_psid)
return
elif "level" in payload:
generate_quick_reply_level(payload, sender_psid)
return
elif "time" in payload:
generate_template_time_picker(payload, sender_psid)
return
elif received_message.get("text"):
if check_latin(sender_psid, received_message.get("text")):
pass
elif context["context"] == "submit_skill":
generate_template_skill_submission(sender_psid, received_message)
return
elif received_message["text"] == "my skills":
template_skills_progress(sender_psid)
return
elif received_message["text"] == "show skills":
generate_template_show_skills(sender_psid)
return
else:
# default request
request = {"text": "I'm not that good with understanding human speech now. " +
"Can you try using the menu, please?"}
callSendAPI(sender_psid, request)
generate_template_show_skills(sender_psid)
return
def handlePostback(sender_psid, received_postback):
payload = received_postback.get("payload")
# context = retrieve_context(sender_psid)
store_context(sender_psid, "")
if payload == "get_started":
gen_temp_get_started(sender_psid)
return
elif payload == "show_skills":
generate_template_show_skills(sender_psid)
return
elif payload == "my_skills":
template_skills_progress(sender_psid)
return
elif "apply_for" in payload:
template_skills_apply(sender_psid)
return
elif "showdetailtask" in payload:
template_skills_task(sender_psid, payload.split("_")[1])
return
elif "completed" in payload:
generate_template_complete_task(sender_psid, payload)
# template_skills_progress(sender_psid)
return
elif "startagain" in payload:
gen_temp_get_start_again(sender_psid, payload)
return
elif "remove" in payload:
gen_temp_remove(sender_psid, payload)
return
elif payload == "submit_skill":
gen_temp_submit_skill(sender_psid)
return
return
def check_latin(sender_psid, text):
if not ad.is_latin(text):
request = {"text": "Sorry! I can only speak in English!"}
callSendAPI(sender_psid, request)
return True
else:
return False
def template_skills_apply(sender_psid):
request = {"text": "Cool! Will start learning this as soon as it's implemented!"}
callSendAPI(sender_psid, request)
def gen_temp_submit_skill(sender_psid):
request = {"text": "Sure, what would you like to learn?"}
store_context(sender_psid, "submit_skill")
callSendAPI(sender_psid, request)
def gen_temp_remove(sender_psid, payload):
delete_progress_skill(sender_psid, payload.split("_")[1])
request = {"text": "The task was removed"}
callSendAPI(sender_psid, request)
template_skills_progress(sender_psid)
def gen_temp_get_start_again(sender_psid, payload):
skill_progress = get_progress_skill(sender_psid, payload.split("_")[1])
store_progress_skill(
sender_psid,
skill_progress["level"],
skill_progress["skill"],
skill_progress["time"],
0,
completed=True,
)
template_skills_progress(sender_psid)
def gen_temp_get_started(sender_psid):
name = getName(sender_psid)
request = {
"text": "Welcome, "
+ name
+ "! Hello! I'm Raido bot and I will help you learn skills you want. I can coach you on learning the whole knowledge path (like frontend dev or DevOps) or help you get the specific skill (e.g. Photoshop, Docker).",
}
callSendAPI(sender_psid, request)
request = {
"attachment": {
"type": "template",
"payload": {
"template_type": "button",
"text": "Let's start by choosing the knowledge domain. What would you like to learn?",
"buttons": [
{
"type": "postback",
"title": "Show skills",
"payload": "show_skills",
}
],
},
}
}
callSendAPI(sender_psid, request)
def generate_quick_reply_pagination(payload, sender_psid):
request = {
"text": skill_selection_text,
"quick_replies": generateQuickRepliesSkills(int(payload.split("_")[2])),
}
callSendAPI(sender_psid, request)
def generate_quick_reply_skill(payload, sender_psid):
request = {
"text": level_selection_text
+ get_skill(int(payload.split("_")[2]))["name"]
+ "?",
"quick_replies": generateQuickRepliesLevel(int(payload.split("_")[2])),
}
callSendAPI(sender_psid, request)
def generate_quick_reply_level(payload, sender_psid):
request = {
"text": time_selection_text,
"quick_replies": generateQuickRepliesTime(
int(payload.split("_")[0]), payload.split("_")[2]
),
}
callSendAPI(sender_psid, request)
def generate_template_skill_submission(sender_psid, received_message):
store_skill_request(sender_psid, received_message.get("text"))
request = {
"text": "Great! Would you like to get a one-time notification when I have this skill?"
}
callSendAPI(sender_psid, request)
def generate_template_time_picker(payload, sender_psid):
level = payload.split("_")[0]
skill = payload.split("_")[1]
time = payload.split("_")[3]
assignment = get_next_assignment(skill, level, -1)
store_progress_skill(sender_psid, level, skill, time, assignment)
skill_data = get_skill(int(skill))
task_type = skill_data["assignments"][assignment]["type"]
task_name = skill_data["assignments"][assignment]["name"]
course_time = skill_data["assignments"][assignment]["time_hours"]
request = {
"text": "Awesome! To check the progress and current task for your skill use the menu."
}
callSendAPI(sender_psid, request)
template_skills_task(sender_psid, skill)
gen_templ_one_time_req(sender_psid, skill, assignment, time, course_time)
def gen_templ_one_time_req(sender_psid, skill, assignment, user_time, course_time):
request = {
"attachment": {
"type": "template",
"payload": {
"template_type": "one_time_notif_req",
"title": "Check on your progress for the task in a couple of days?",
"payload": "one_time_request_"
+ str(skill)
+ "_"
+ str(assignment)
+ "_"
+ str(user_time)
+ "_"
+ str(course_time),
},
}
}
callSendAPI(sender_psid, request)
def generate_template_complete_task(sender_psid, payload):
skill_progress = get_progress_skill(sender_psid, payload.split("_")[1])
if skill_progress == None:
print("Skill is not existing")
logging.error("Skill is not existing for user" + str(sender_psid))
assignment = get_next_assignment(
skill_progress["skill"],
skill_progress["level"],
skill_progress["cur_assignment"],
)
if assignment == skill_progress["cur_assignment"]:
store_progress_skill(
sender_psid,
skill_progress["level"],
skill_progress["skill"],
skill_progress["time"],
skill_progress["cur_assignment"],
completed=True,
)
request = {
"text": "Congratulations! You completed learning of the "
+ get_skill(skill_progress["skill"])["name"]
}
callSendAPI(sender_psid, request)
else:
store_progress_skill(
sender_psid,
skill_progress["level"],
skill_progress["skill"],
skill_progress["time"],
assignment,
)
template_skills_task(sender_psid, skill_progress["skill"])
skill_data = get_skill(int(skill_progress["skill"]))
course_time = skill_data["assignments"][assignment]["time_hours"]
gen_templ_one_time_req(
sender_psid,
skill_progress["skill"],
assignment,
skill_progress["time"],
course_time,
)
def generate_template_show_skills(sender_psid):
request = {
"text": skill_selection_text,
"quick_replies": generateQuickRepliesSkills(0),
}
store_context(sender_psid, "skill_selection")
callSendAPI(sender_psid, request)
def find_skill(progress_list, skill_num):
for pr in progress_list:
if str(pr["skill"]) == str(skill_num):
return pr
def template_skills_task(psid, skill_num):
buttons = []
skills = get_progress_skills(psid)
print(skills)
print(skill_num)
skill = find_skill(skills, int(skill_num))
skill_data = get_skill(int(skill_num))
print(skill_data)
print("cur asignment" + str(skill["cur_assignment"]))
asign_type = skill_data["assignments"][int(skill["cur_assignment"])]["type"]
asign_name = skill_data["assignments"][int(skill["cur_assignment"])]["name"]
asign_author = skill_data["assignments"][int(skill["cur_assignment"])]["author"]
asign_rating = skill_data["assignments"][int(skill["cur_assignment"])]["rating"]
asign_url = skill_data["assignments"][int(skill["cur_assignment"])]["url"]
asign_started = skill["timestamp_creation"]
text = (
"Your new assignment is the "
+ asign_type
+ ' "'
+ asign_name
+ '" by '
+ asign_author
+ ". \n\n"
)
text += (
"Rating: " + str(asign_rating) + "\n\n" + "Started: " + str(asign_started)[:10]
)
buttons = [
{
"type": "web_url",
"title": "Go to task",
"url": asign_url,
"webview_height_ratio": "full",
},
{
"type": "postback",
"title": "Mark as done",
"payload": "completed_" + str(skill["skill"]),
},
]
request = {
"attachment": {
"type": "template",
"payload": {"template_type": "button", "text": text, "buttons": buttons, },
}
}
callSendAPI(psid, request)
def template_skills_progress(psid):
elements = []
skills = get_progress_skills(psid)
for skill in skills:
skill_data = get_skill(skill["skill"])
progress = (
str(
int(
float(skill["cur_assignment"])
/ float(len(skill_data["assignments"]))
* 100
)
).zfill(1)
+ "%"
)
asign_type = skill_data["assignments"][int(skill["cur_assignment"])]["type"]
asign_name = skill_data["assignments"][int(skill["cur_assignment"])]["name"]
asign_url = skill_data["assignments"][int(skill["cur_assignment"])]["url"]
task_text = "Task: " + asign_type + ' "' + asign_name + '".'
completed_text = "Course is completed. Good job!"
progress_text = "Progress: " + progress + " \n\n"
buttons = [
{
"type": "web_url",
"title": "Go to " + asign_type,
"url": asign_url,
"webview_height_ratio": "full",
},
# {
# "type": "postback",
# "title": "Mark as completed",
# "payload": "completed_" + str(skill["skill"]),
# },
{
"type": "postback",
"title": "Show current task",
"payload": "showdetailtask_" + str(skill["skill"]),
},
]
if skill["completed"] == True:
add_text = completed_text
progress_text = ""
buttons = [
{
"type": "postback",
"title": "Start again",
"payload": "startagain_" + str(skill["skill"]),
},
{
"type": "postback",
"title": "Remove from list",
"payload": "remove_" + str(skill["skill"]),
},
]
else:
add_text = task_text
elements.append(
{
"title": skill_data["name"],
"image_url": skill_data["image_url"],
"subtitle": progress_text + add_text,
"default_action": {
"type": "web_url",
"url": asign_url,
"webview_height_ratio": "COMPACT",
},
"buttons": buttons,
}
)
if len(elements) == 0:
request = {
"text": "Currently, you don't have any active skills. Start be selecting the skill you want to learn. You can see the list of available skills in the menu."
}
else:
request = {
"attachment": {
"type": "template",
"payload": {
"template_type": "generic",
"image_aspect_ratio": "square",
"elements": elements,
},
}
}
callSendAPI(psid, request)
def get_next_assignment(skill, level, cur_assignment):
with open("skills.json") as json_file:
skills = json.load(json_file)["skills"]
new_assignment = cur_assignment
for i, assignment in enumerate(skills[int(skill)]["assignments"]):
if int(assignment["level"]) < int(level) or i <= cur_assignment:
continue
else:
new_assignment = i
break
return new_assignment
def get_skill(num):
with open("skills.json") as json_file:
skills = json.load(json_file)["skills"]
return skills[int(num)]
def generateQuickRepliesTime(skill_num, level):
quick_replies = [
{
"content_type": "text",
"title": "3 Hours",
"payload": str(level) + "_" + str(skill_num) + "_time_1",
},
{
"content_type": "text",
"title": "5 Hours",
"payload": str(level) + "_" + str(skill_num) + "_time_3",
},
{
"content_type": "text",
"title": "8 Hours",
"payload": str(level) + "_" + str(skill_num) + "_time_7",
},
{
"content_type": "text",
"title": "13 Hours",
"payload": str(level) + "_" + str(skill_num) + "_time_15",
},
{
"content_type": "text",
"title": "21 Hours",
"payload": str(level) + "_" + str(skill_num) + "_time_15",
},
]
return quick_replies
def generateQuickRepliesLevel(skill_num):
quick_replies = [
{
"content_type": "text",
"title": "Beginner",
"payload": str(skill_num) + "_level_0",
},
{
"content_type": "text",
"title": "Middle",
"payload": str(skill_num) + "_level_1",
},
{
"content_type": "text",
"title": "Advanced",
"payload": str(skill_num) + "_level_2",
},
]
return quick_replies
def generateQuickRepliesSkills(page):
pagination = 5 # 5 skills per page
quick_replies = []
with open("skills.json") as json_file:
skills = json.load(json_file)["skills"]
skills_n = len(skills)
if page > 0:
quick_replies.append(
{
"content_type": "text",
"title": "Previous page",
"payload": "previous_page_" + str(page - 1),
}
)
for i in range(page * pagination, page * pagination + pagination):
if i >= skills_n:
break
quick_replies.append(
{
"content_type": "text",
"title": skills[i]["name"],
"payload": "skill_selection_" + str(i)
# "image_url":"http://example.com/img/red.png"
}
)
if (page * pagination + pagination - 1) < skills_n:
quick_replies.append(
{
"content_type": "text",
"title": "Next page",
"payload": "next_page_" + str(page + 1),
}
)
return quick_replies
def generate_one_time_template(token, assignment, skill):
skill_data = get_skill(int(skill))
asign_name = skill_data["assignments"][int(assignment)]["name"]
request = {
"attachment": {
"type": "template",
"payload": {
"template_type": "button",
"text": 'Hey! This is a reminder for the task "'
+ str(asign_name)
+ " you had. Did you make it and ready for a new challenge?",
"buttons": [
{
"type": "postback",
"title": "Show my skills",
"payload": "my_skills",
},
{
"type": "postback",
"title": "Mark as done",
"payload": "completed_" + str(skill),
},
],
},
}
}
callOneTimeNotif(token, request)
def callOneTimeNotif(token, _request):
request = json.dumps(
{"recipient": {"one_time_notif_token": token}, "message": _request}
)
params = {"access_token": ACCESS_TOKEN}
headers = {"Content-Type": "application/json"}
r = requests.post(
"https://graph.facebook.com/v2.6/me/messages",
params=params,
headers=headers,
data=request,
)
if r.status_code != 200:
logging.error(r.status_code)
logging.error(r.text)
def callSendAPI(sender_psid, _request):
request = json.dumps({"recipient": {"id": sender_psid}, "message": _request})
params = {"access_token": ACCESS_TOKEN}
headers = {"Content-Type": "application/json"}
r = requests.post(
"https://graph.facebook.com/v2.6/me/messages",
params=params,
headers=headers,
data=request,
)
if r.status_code != 200:
logging.error(r.status_code)
logging.error(r.text)
def getName(sender_psid):
r = requests.get(
"https://graph.facebook.com/"
+ sender_psid
+ "?fields=first_name&access_token="
+ ACCESS_TOKEN
)
return r.json().get("first_name")