-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
1282 lines (1076 loc) · 49.8 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
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import re
from time import sleep
from telebot import TeleBot, types, logger
import handlers
from emails import email_notification
from utils import build_keyboard, build_inline_keyboard
BOT_KEY = os.getenv('TASK_MANAGER_BOT_TOKEN')
bot = TeleBot(BOT_KEY)
EMOJI = {
'dashboard': '\U0001F5C2',
'task': '\U0001F4CB',
'user': '\U0001F465',
'back': '\u21A9',
'add': '\U0001F195',
'main': '\u25B6',
'comment': '\U0001F4DD',
'delete': '\U0000274C',
'yes': '\U00002705',
'update': '\U0000270F',
'account': '\U0001F464',
'stats': '\U0001F4CA',
'email': '\U0001F4E7',
'TO DO': '\U0001F534',
'IN PROCESS': '\U0001F7E1',
'DONE': '\U0001F7E2'
}
COMMANDS = [
f'{EMOJI["main"]} Main Menu',
f'{EMOJI["dashboard"]} Dashboards',
f'{EMOJI["task"]} My Tasks',
f'{EMOJI["comment"]} My Comments',
f'{EMOJI["back"]} Back to Main Menu',
f'{EMOJI["email"]} Send Email'
]
@bot.message_handler(commands=['start', 'help'])
def send_options(message):
user = handlers.get_user(message.chat.id)
if user != 'Not found':
text = "This is a Task Manager Bot. You can create your own dashboards" \
" or join other user's ones. Inside dashboard you can create " \
"tasks, add users and comments. Don't forget " \
"to subscribe to @BestTaskManagerNotifications_bot to receive " \
"notification about your tasks and comments!"
bot.send_message(message.chat.id, text)
sleep(1.0)
main_menu(message)
return
text = 'Welcome to Task Manager Bot. You can create a new dashboard or ' \
'join existing one. Inside dashboard you can create tasks, ' \
'add users and comments. To proceed you need to create an ' \
'account below:'
bot.send_message(message.chat.id, text,
reply_markup=build_keyboard(
f'{EMOJI["add"]} Create Account'))
@bot.message_handler(func=lambda x: x.text == f'{EMOJI["add"]} Create Account')
def create_account(message):
user = handlers.get_user(message.chat.id)
if user != 'Not found':
bot.send_message(message.chat.id, 'You already have an account.')
sleep(1.0)
main_menu(message)
return
msg = bot.send_message(message.chat.id, 'OK. Send me your name:',
reply_markup=build_keyboard(
f'{EMOJI["back"]} Back to Main Menu'))
bot.register_next_step_handler(msg, process_name_step)
def process_name_step(message):
if message.text == f'{EMOJI["back"]} Back to Main Menu':
bot.send_message(message.chat.id, 'OK. Tell me when you are ready.',
reply_markup=build_keyboard(
f'{EMOJI["add"]} Create Account'))
return
user = {'username': message.text, 'chat_id': message.chat.id, }
msg = bot.send_message(message.chat.id, f'Thank you, {user["username"]}.'
f' Send me your email:')
bot.register_next_step_handler(msg, process_email_step, user)
def process_email_step(message, user):
if message.text == f'{EMOJI["back"]} Back to Main Menu':
bot.send_message(message.chat.id, 'OK. Tell me when you are ready.',
reply_markup=build_keyboard(
f'{EMOJI["add"]} Create Account'))
return
pattern = re.compile(r'^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,3})+$')
result = re.match(pattern, message.text)
if not result:
msg = bot.send_message(
message.chat.id,
'Email seems to be invalid. Please type email in format '
'[email protected], no spaces allowed.')
bot.register_next_step_handler(msg, process_email_step, user)
return
user['email'] = message.text.lower()
bot.send_message(message.chat.id,
'Thank you, creating your account...')
create_user(message, user)
def create_user(message, user):
req = handlers.create_user(user)
sleep(1.0)
if req == 201:
bot.send_message(
message.chat.id,
"Your account has been created. You can create a new dashboard or "
"join existing one. To join a dashboard, you need to pass the "
"dashboard admin your email and they will add you. Don't forget "
"to subscribe to @BestTaskManagerNotifications_bot to receive "
"notification about your tasks and comments!",
reply_markup=build_keyboard(f'{EMOJI["add"]} Create Dashboard',
f'{EMOJI["account"]} My Account')
)
return
bot.send_message(message.chat.id,
f"Creating account failed. Please try again.")
@bot.message_handler(func=lambda x: x.text == f'{EMOJI["account"]} My Account')
def get_account_details(message):
user = handlers.get_user(message.chat.id)
if user_checker(message.chat.id, user):
return
keyboard = types.InlineKeyboardMarkup()
keyboard.add(
types.InlineKeyboardButton(
f'{EMOJI["update"]} Update Name',
callback_data=f'update_user_username'),
types.InlineKeyboardButton(
f'{EMOJI["update"]} Update Email',
callback_data=f'update_user_email'),
types.InlineKeyboardButton(
f'{EMOJI["delete"]} Delete Account',
callback_data=f'delete_account_{user["chat_id"]}')
)
bot.send_message(message.chat.id,
text=f"*Here is your account details:*\n\n"
f"*Name:* {user['username']}\n"
f"*Email:* {user['email']}\n",
reply_markup=keyboard,
parse_mode='Markdown')
def update_user(message, data):
if message.text in COMMANDS:
command_checker(message)
return
if data == 'email':
pattern = re.compile(r'^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,3})+$')
result = re.match(pattern, message.text)
if not result:
msg = bot.send_message(
message.chat.id,
'Email seems to be invalid. Please type email in format '
'[email protected], no spaces allowed.')
bot.register_next_step_handler(msg, update_user, data)
return
update_data = {data: message.text.lower()}
else:
update_data = {data: message.text}
req = handlers.update_user(message.chat.id, update_data)
if req == 204:
bot.send_message(message.chat.id, f'Your account has been updated.')
get_account_details(message)
return
bot.send_message(message.chat.id,
'Updating account was unsuccessful. '
'Please try again.')
get_account_details(message)
def delete_account(message, chat_id):
if message.text in COMMANDS:
command_checker(message)
return
if message.text == f'{EMOJI["yes"]} Yes':
req = handlers.delete_user(chat_id)
if req == 200:
bot.send_message(message.chat.id, 'Account has been deleted.',
reply_markup=build_keyboard(
f'{EMOJI["add"]} Create Account'))
return
bot.send_message(message.chat.id,
'Deleting account was unsuccessful. '
'Please try again.',
reply_markup=build_keyboard(
f'{EMOJI["account"]} My Account'))
return
get_account_details(message)
@bot.message_handler(
func=lambda x: x.text == f'{EMOJI["dashboard"]} Dashboards')
def get_dashboards(message):
dashboards = handlers.get_user_dashboards(message.chat.id)
if user_checker(message.chat.id, dashboards):
return
d_board = [d.get('name') for d in dashboards]
d_board_hidden = [d.get('id') for d in dashboards]
keyboard = build_inline_keyboard(d_board, d_board_hidden,
'dashboard_detailed')
keyboard.add(
types.InlineKeyboardButton(f'{EMOJI["add"]} Create Dashboard',
callback_data='create_dashboard'),
types.InlineKeyboardButton(f'{EMOJI["back"]} Back to Main Menu',
callback_data='main'))
bot.send_message(message.chat.id, 'Getting your dashboards...',
reply_markup=build_keyboard(
f'{EMOJI["add"]} Create Task',
f'{EMOJI["add"]} Add User to Dashboard',
f'{EMOJI["main"]}️ Main Menu')
)
bot.send_message(message.chat.id, 'Your dashboards:',
reply_markup=keyboard)
def update_dashboard(message, chat_id, d_id):
if message.text in COMMANDS:
command_checker(message)
return
req = handlers.update_dashboard(chat_id, d_id, message.text)
if req == 204:
bot.send_message(message.chat.id, f'Dashboard name has been changed to'
f' {message.text}.')
main_menu(message)
return
else:
bot.send_message(message.chat.id,
'Updating dashboard was unsuccessful. '
'Please note, only dashboard admins can update or '
'delete dashboards.')
main_menu(message)
def delete_dashboard(message, chat_id, d_id):
if message.text == f'{EMOJI["yes"]} Yes':
req = handlers.delete_dashboard(chat_id, d_id)
if req == 200:
bot.send_message(message.chat.id, 'Dashboard has been deleted.')
main_menu(message)
return
bot.send_message(message.chat.id,
'Deleting dashboard was unsuccessful. '
'Please note, only dashboard admins can delete or '
'update dashboards.')
main_menu(message)
return
main_menu(message)
@bot.message_handler(
func=lambda x: x.text == f'{EMOJI["add"]} Create Dashboard')
def initiate_dashboard_creation(message):
msg = bot.send_message(message.chat.id, 'OK. Send me your dashboard name:',
reply_markup=build_keyboard(
f'{EMOJI["back"]} Back to Main Menu'))
bot.register_next_step_handler(msg, process_dashboard_name_step)
def process_dashboard_name_step(message):
if message.text == f'{EMOJI["back"]} Back to Main Menu':
bot.send_message(message.chat.id, 'OK. Tell me when you are ready.',
reply_markup=build_keyboard(
f'{EMOJI["add"]} Create Dashboard'))
return
if message.text in COMMANDS:
command_checker(message)
return
dashboard = {'dashboard_name': message.text}
bot.send_message(
message.chat.id,
f'OK. Creating {dashboard["dashboard_name"]} dashboard...')
create_dashboard(message, dashboard)
def create_dashboard(message, data):
req = handlers.create_dashboard(message.chat.id, data)
sleep(1.0)
if req == 201:
bot.send_message(
message.chat.id,
f"Dashboard {data['dashboard_name']} has been created. You can "
f"create a new task or add users to your dashboard now.",
reply_markup=build_keyboard(
f'{EMOJI["add"]} Create Task',
f'{EMOJI["add"]} Add User',
f'{EMOJI["dashboard"]} Dashboards',
f'{EMOJI["back"]} Back to Main Menu'))
return
bot.send_message(message.chat.id,
f"Creating dashboard failed. Please try again.")
@bot.message_handler(
func=lambda x: x.text == f'{EMOJI["add"]} Add User to Dashboard'
or x.text == f'{EMOJI["add"]} Add User')
def initiate_adding_user(message):
dashboards = handlers.get_user_dashboards_as_admin(message.chat.id)
user_dashboards = [d.get('name') for d in dashboards]
msg = bot.send_message(message.chat.id,
"OK. Send me the dashboard to add new user to:",
reply_markup=build_keyboard(
*user_dashboards,
f'{EMOJI["back"]} Back to Main Menu')
)
bot.register_next_step_handler(msg, locate_user_dashboard_step, dashboards)
def locate_user_dashboard_step(message, dashboards):
if message.text in COMMANDS:
command_checker(message)
return
d_id = [d.get('id') for d in dashboards if
d.get('name') == message.text]
if not d_id:
msg = bot.send_message(message.chat.id,
'Dashboard does not exist. Choose a dashboard '
'from the list below:')
bot.register_next_step_handler(msg, locate_user_dashboard_step,
dashboards)
return
msg = bot.send_message(message.chat.id, "OK. Send me user's email:",
reply_markup=build_keyboard(
f'{EMOJI["back"]} Back to Main Menu')
)
bot.register_next_step_handler(msg, process_user_email_step, d_id[0])
def process_user_email_step(message, dashboard, email=None):
if message.text == f'{EMOJI["email"]} Send Email':
user = handlers.get_user(message.chat.id).get('username')
link = 'https://t.me/BestTaskManagerBot'
content = f"{user} was trying to add you to their dashboard in Best " \
f"Task Manager Bot but noticed that you are not registered " \
f"yet. If you still want to join you can click following " \
f"link:\n\n{link}\n\n--\nBest Task Manager Bot"
email_notification(user, email, content)
bot.send_message(message.chat.id,
f'OK. Sending an email to {email}...')
sleep(1.0)
bot.send_message(message.chat.id,
'Email has been sent.')
main_menu(message)
return
if message.text in COMMANDS:
command_checker(message)
return
pattern = re.compile(r'^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,3})+$')
result = re.match(pattern, message.text)
if not result:
msg = bot.send_message(
message.chat.id,
'Email seems to be invalid. Please type email in format '
bot.register_next_step_handler(msg, process_user_email_step, dashboard)
return
email = message.text
bot.send_message(message.chat.id,
'Thank you, checking if user is registered...')
user = handlers.get_user_by_email(email)
sleep(1.0)
if user == 'Not found':
msg = bot.send_message(
message.chat.id,
f"User with email {email} is not registered or email is wrong. "
f"If this is a correct email, we can send an email on your behalf "
f"to join Best Telegram Manager Bot. To confirm, press the button "
f"below:",
reply_markup=build_keyboard(
f'{EMOJI["email"]} Send Email',
f'{EMOJI["back"]} Back to Main Menu')
)
bot.register_next_step_handler(msg, process_user_email_step, dashboard,
email)
return
bot.send_message(message.chat.id,
'OK. User found. Adding user to dashboard...')
add_user_to_dashboard(message, user['chat_id'], dashboard)
def add_user_to_dashboard(message, user_id, dashboard):
req = handlers.add_user_to_dashboard(message.chat.id, user_id, dashboard)
sleep(1.0)
if req == 201:
bot.send_message(message.chat.id, "User has been added.",
reply_markup=build_keyboard(
f'{EMOJI["back"]} Back to Main Menu')
)
# sending notification to the added user
bot.send_message(user_id,
"You have been added to a new dashboard",
reply_markup=build_keyboard(
f'{EMOJI["dashboard"]} Dashboards',
f'{EMOJI["task"]} My Tasks',
f'{EMOJI["comment"]} My Comments',
f'{EMOJI["account"]} My Account')
)
main_menu(message)
return
bot.send_message(message.chat.id,
f"Adding user failed. Please try again")
@bot.message_handler(func=lambda x: x.text == f'{EMOJI["task"]} My Tasks')
def get_tasks(message):
tasks = handlers.get_user_stats(message.chat.id, 'tasks')
if user_checker(message.chat.id, tasks):
return
tasks_hidden = [
'_'.join([str(t.get('dashboard_id')), str(t.get('task_name')),
str(t.get('id'))])
for t in tasks]
tasks = [t.get('task_name') for t in tasks]
keyboard = build_inline_keyboard(tasks, tasks_hidden,
f'task_detailed')
keyboard.add(
types.InlineKeyboardButton(f'{EMOJI["back"]} Back to Main Menu',
callback_data='main')
)
bot.send_message(message.chat.id, 'Getting your tasks...',
reply_markup=build_keyboard(
f'{EMOJI["add"]} Create Task',
f'{EMOJI["add"]} Add User to Task',
f'{EMOJI["main"]}️ Main Menu'))
bot.send_message(message.chat.id, 'Your tasks:',
reply_markup=keyboard)
def update_task(message, task_data, status_buttons=None):
"""TO DO change task admins. Should have additional admin button"""
if task_data['update_instance'] == 'status' \
and message.text not in status_buttons:
msg = bot.send_message(message.chat.id,
'Wrong input. Please choose from the following '
'options:')
bot.register_next_step_handler(msg, update_task, task_data,
status_buttons)
return
if message.text in COMMANDS:
command_checker(message)
return
bot.send_message(message.chat.id, 'Updating your task...')
req = handlers.update_task(message.chat.id, task_data, message.text)
sleep(1.0)
if req == 204:
bot.send_message(message.chat.id,
f"Task has been successfully updated")
main_menu(message)
return
bot.send_message(message.chat.id,
f"Updating task was unsuccessful. Please try again.")
main_menu(message)
def delete_task(message, task_data):
if message.text == f'{EMOJI["yes"]} Yes':
req = handlers.delete_task(message.chat.id, task_data)
if req == 200:
bot.send_message(message.chat.id, 'Task has been deleted.')
main_menu(message)
return
bot.send_message(message.chat.id,
'Deleting task was unsuccessful. '
'Please try again')
main_menu(message)
def process_task_details_step(message, task_data, buttons):
if message.text not in buttons:
msg = bot.send_message(message.chat.id,
'Wrong input. Please choose from the following '
'options:')
bot.register_next_step_handler(msg, process_task_details_step,
task_data, buttons)
return
if message.text in COMMANDS:
command_checker(message)
return
if message.text == 'Name':
msg = bot.send_message(message.chat.id, 'OK. Send me a new name:')
task_data['update_instance'] = 'task_name'
bot.register_next_step_handler(msg, update_task, task_data)
if message.text == 'Description':
msg = bot.send_message(message.chat.id,
'OK. Send me a new description:')
task_data['update_instance'] = 'text'
bot.register_next_step_handler(msg, update_task, task_data)
if message.text == 'Current Status':
status_buttons = ('TO DO', 'IN PROCESS', 'DONE',
f'{EMOJI["back"]} Back to Main Menu')
msg = bot.send_message(message.chat.id, 'OK. Send me a new status:',
reply_markup=build_keyboard(*status_buttons))
task_data['update_instance'] = 'status'
bot.register_next_step_handler(msg, update_task, task_data,
status_buttons)
@bot.message_handler(func=lambda x: x.text == f'{EMOJI["add"]} Create Task')
def initiate_task_creation(message):
dashboards = handlers.get_user_dashboards(message.chat.id)
user_dashboards = [d.get('name') for d in dashboards]
msg = bot.send_message(message.chat.id,
'OK. Choose dashboard new task will belong to:',
reply_markup=build_keyboard(
*user_dashboards,
f'{EMOJI["back"]} Back to Main Menu'))
bot.register_next_step_handler(msg, locate_dashboard_step, dashboards)
def locate_dashboard_step(message, dashboards):
if message.text in COMMANDS:
command_checker(message)
return
d_id = [d.get('id') for d in dashboards if d.get('name') == message.text]
if not d_id:
msg = bot.send_message(message.chat.id,
'Dashboard does not exist. Choose a dashboard '
'from the list below:')
bot.register_next_step_handler(msg, locate_dashboard_step, dashboards)
return
msg = bot.send_message(message.chat.id, 'OK. Send me your task name:',
reply_markup=build_keyboard(
f'{EMOJI["back"]} Back to Main Menu')
)
task = {'dashboard_id': d_id[0]}
bot.register_next_step_handler(msg, process_task_name_step, task)
def process_task_name_step(message, task):
if message.text in COMMANDS:
command_checker(message)
return
task['task_name'] = message.text
msg = bot.send_message(message.chat.id,
'OK. Send me your task description. You can include'
' specific information about the task, instructions'
' and other details:',
reply_markup=build_keyboard(
f'{EMOJI["back"]} Back to Main Menu')
)
bot.register_next_step_handler(msg, process_task_description_step, task)
def process_task_description_step(message, task):
if message.text in COMMANDS:
command_checker(message)
return
task['text'] = message.text
bot.send_message(message.chat.id, 'OK. Creating your task...')
create_task(message, task)
def create_task(message, task):
req = handlers.create_task(message.chat.id, task)
sleep(1.0)
if req == 201:
bot.send_message(
message.chat.id,
f"Task {task['task_name']} has been created. You can "
f"change task status, add comments or users to your task.",
reply_markup=build_keyboard(
f'{EMOJI["back"]} Back to Main Menu'))
main_menu(message)
return
bot.send_message(message.chat.id,
f"Creating task failed. Please try again.")
@bot.message_handler(
func=lambda x: x.text == f'{EMOJI["add"]} Add User to Task')
def initiate_adding_user_to_task(message):
dashboards = handlers.get_user_dashboards(message.chat.id)
user_dashboards = [d.get('name') for d in dashboards]
msg = bot.send_message(message.chat.id,
"OK. Send me the dashboard first:",
reply_markup=build_keyboard(
*user_dashboards,
f'{EMOJI["back"]} Back to Main Menu')
)
bot.register_next_step_handler(msg, locate_user_task_step, dashboards)
def locate_user_task_step(message, dashboards):
if message.text in COMMANDS:
command_checker(message)
return
d_id = [d.get('id') for d in dashboards if
d.get('name') == message.text]
if not d_id:
msg = bot.send_message(message.chat.id,
'Dashboard does not exist. Choose a dashboard '
'from the list below:')
bot.register_next_step_handler(msg, locate_user_task_step,
dashboards)
return
dashboard_tasks = handlers.get_dashboard_tasks(d_id[0])
tasks = [t.get('task_name') for t in dashboard_tasks]
msg = bot.send_message(message.chat.id,
"OK. Send me the task to add user to:",
reply_markup=build_keyboard(
*tasks,
f'{EMOJI["back"]} Back to Main Menu')
)
bot.register_next_step_handler(msg, locate_task_users_step, d_id[0],
dashboard_tasks)
def locate_task_users_step(message, d_id, tasks=None, task=None):
if message.text in COMMANDS:
command_checker(message)
return
if task is None:
task = [t for t in tasks if t.get('task_name') == message.text][0]
if not task:
msg = bot.send_message(message.chat.id,
'Task does not exist. Choose a task from '
'the list below:')
bot.register_next_step_handler(msg, locate_task_users_step, tasks,
d_id)
return
dashboard_users = handlers.get_dashboard_users(d_id)
users = [u.get('username') for u in dashboard_users]
msg = bot.send_message(message.chat.id,
"OK. Send me a user to add to task:",
reply_markup=build_keyboard(
*users,
f'{EMOJI["back"]} Back to Main Menu')
)
bot.register_next_step_handler(msg, process_adding_user_step, task,
dashboard_users)
def process_adding_user_step(message, task, users):
if message.text in COMMANDS:
command_checker(message)
return
user_id = [u.get('chat_id') for u in users if
u.get('username') == message.text]
if not user_id:
msg = bot.send_message(message.chat.id,
'User does not exist. Choose a task from the '
'list below:')
bot.register_next_step_handler(msg, process_adding_user_step, task,
users)
return
bot.send_message(message.chat.id,
'OK. Adding user to task...')
add_user_to_task(message, user_id, task['dashboard_id'], task['id'])
def add_user_to_task(message, user_id, task_id, dashboard_id):
req = handlers.add_user_to_task(message.chat.id, user_id, task_id,
dashboard_id)
sleep(1.0)
if req == 201:
bot.send_message(message.chat.id, "User has been added.",
reply_markup=build_keyboard(
f'{EMOJI["back"]} Back to Main Menu')
)
main_menu(message)
return
bot.send_message(message.chat.id,
f"Adding user failed with status code. Please try again")
@bot.message_handler(
func=lambda x: x.text == f'{EMOJI["comment"]} Post Comment')
def initiate_posting_comment(message, comment_data):
if message.text in COMMANDS:
command_checker(message)
return
comment_data['title'] = message.text
msg = bot.send_message(message.chat.id,
"OK. Send me the comment text:",
reply_markup=build_keyboard(
f'{EMOJI["back"]} Back to Main Menu')
)
bot.register_next_step_handler(msg, process_comment_text_step,
comment_data)
def process_comment_text_step(message, comment_data):
if message.text in COMMANDS:
command_checker(message)
return
comment_data['text'] = message.text
comment_data['chat_id'] = message.chat.id
bot.send_message(message.chat.id, 'OK. Posting your comment...')
post_comment(message, comment_data)
def post_comment(message, comment_data):
req = handlers.post_comment(comment_data)
sleep(1.0)
if req == 201:
bot.send_message(message.chat.id, "Comment has been posted.",
reply_markup=build_keyboard(
f'{EMOJI["back"]} Back to Main Menu')
)
main_menu(message)
return
bot.send_message(message.chat.id,
f"Posting comment was unsuccessful. Please try again.")
@bot.message_handler(
func=lambda x: x.text == f'{EMOJI["comment"]} My Comments')
def get_user_comments(message):
# TODO add pagination
comments = handlers.get_user_comments(message.chat.id)
if user_checker(message.chat.id, comments):
return
sleep(1.0)
for c in comments:
bot.send_message(message.chat.id,
text=f"*Title*: {c.get('title')}\n"
f"*Comment*: {c.get('comment')}\n"
f"*Task*: {c.get('task')}\n"
f"*Posted*: {c.get('created_at')}",
parse_mode='Markdown')
@bot.message_handler(
func=lambda x: x.text == f'{EMOJI["main"]}️ Main Menu' or
x.text == f'{EMOJI["back"]} Back to Main Menu')
def main_menu(message):
bot.send_message(message.chat.id,
"You can navigate following sections:\n"
" - Dashboards\n"
" - Tasks\n"
" - Users\n"
" - Your account",
reply_markup=build_keyboard(
f'{EMOJI["dashboard"]} Dashboards',
f'{EMOJI["task"]} My Tasks',
f'{EMOJI["comment"]} My Comments',
f'{EMOJI["account"]} My Account'))
@bot.callback_query_handler(func=lambda call: True)
def process_callback_requests(call):
if call.data == 'main':
main_menu(call.message)
elif call.data == f'{EMOJI["add"]} Create Dashboard' or 'create_dashboard' \
in call.data:
initiate_dashboard_creation(call.message)
elif call.data == 'dashboard_main':
dashboards = handlers.get_user_dashboards(call.message.chat.id)
if user_checker(call.message.chat.id, dashboards):
return
d_board = [d.get('name') for d in dashboards]
d_board_hidden = [d.get('id') for d in dashboards]
keyboard = build_inline_keyboard(d_board, d_board_hidden,
'dashboard_detailed')
keyboard.add(
types.InlineKeyboardButton(
f'{EMOJI["add"]} Create Dashboard',
callback_data='create_dashboard'),
types.InlineKeyboardButton(f'{EMOJI["back"]} Back to Main Menu',
callback_data='main'))
bot.edit_message_text(text='Your dashboards:',
chat_id=call.message.chat.id,
message_id=call.message.message_id,
reply_markup=keyboard)
elif 'dashboard_detailed' in call.data:
d_board_id = call.data.split('_')[-1]
d_board_name = call.data.split('_')[-2]
keyboard = types.InlineKeyboardMarkup(row_width=2)
keyboard.add(types.InlineKeyboardButton(
f'{EMOJI["task"]} Tasks',
callback_data=f'dashboard_tasks_{d_board_id}'),
types.InlineKeyboardButton(
f'{EMOJI["user"]} Users',
callback_data=f'dashboard_users_{d_board_id}')
)
keyboard.add(types.InlineKeyboardButton(
f'{EMOJI["update"]} Update Dashboard',
callback_data=f'update_dashboard_{d_board_name}_{d_board_id}'),
types.InlineKeyboardButton(
f'{EMOJI["delete"]} Delete Dashboard',
callback_data=f'delete_dashboard_{d_board_name}_{d_board_id}')
)
keyboard.add(
types.InlineKeyboardButton(
f'{EMOJI["stats"]} Statistics',
callback_data=f'dashboard_stats_{d_board_name}_{d_board_id}'),
types.InlineKeyboardButton(
f'{EMOJI["back"]} Back to Dashboards',
callback_data='dashboard_main'))
bot.edit_message_text(text=f'*{d_board_name} details:*',
chat_id=call.message.chat.id,
message_id=call.message.message_id,
reply_markup=keyboard,
parse_mode='Markdown')
elif 'dashboard_stats' in call.data:
d_board_id = call.data.split('_')[-1]
d_board_name = call.data.split('_')[-2]
req = handlers.get_dashboard_stats(d_board_id)
if user_checker(call.message.chat.id, req):
return
if not req.get('status'):
bot.send_message(call.message.chat.id,
f'Dashboard {d_board_name} has no tasks yet.')
return
to_do = [i for i in req.get('status') if i == 'TO DO']
in_process = [i for i in req.get('status') if i == 'IN PROCESS']
done = [i for i in req.get('status') if i == 'DONE']
done_len = len(done) / len(req.get('status'))
to_do_len = (len(to_do) + len(in_process)) / len(req.get('status'))
done_tasks_percent = round(done_len * 100)
done_len = round(done_len * 16)
to_do_len = round(to_do_len * 16)
progress_bar = "".join(['✓' for _ in range(0, done_len)]) + "".join(
['×' for _ in range(0, to_do_len)])
text = f"*Dashboard {d_board_name} statistics:*\n\n" \
f"{EMOJI['TO DO']} *TO DO*: {len(to_do)} tasks\n" \
f"{EMOJI['IN PROCESS']} *IN PROCESS*: {len(in_process)} tasks\n" \
f"{EMOJI['DONE']} *DONE*: {len(done)} tasks\n\n" \
f"*Completed tasks:* {done_tasks_percent} %\n" \
f"{progress_bar}\n"
bot.send_message(call.message.chat.id, text, parse_mode='Markdown')
elif 'dashboard_users' in call.data:
d_board_id = call.data.split('_')[-1]
users = handlers.get_dashboard_users(d_board_id)
if user_checker(call.message.chat.id, users):
return
users_hidden = [u.get('chat_id') for u in users]
users = [u.get('username') for u in users]
keyboard = build_inline_keyboard(users, users_hidden,
f'user_detailed_{d_board_id}')
keyboard.add(
types.InlineKeyboardButton(
f'{EMOJI["add"]} Add User',
callback_data=f'add_user_dashboard_{d_board_id}'),
types.InlineKeyboardButton(
f'{EMOJI["back"]} Back to Dashboards',
callback_data='dashboard_main')
)
bot.edit_message_text(text=f'*Dashboard users:*',
chat_id=call.message.chat.id,
message_id=call.message.message_id,
reply_markup=keyboard,
parse_mode='Markdown')
elif 'add_user_dashboard' in call.data:
d_board_id = call.data[-1]
msg = bot.send_message(call.message.chat.id,
"OK. Send me user's email:",
reply_markup=build_keyboard(
f'{EMOJI["back"]} Back to Main Menu')
)
bot.register_next_step_handler(msg, process_user_email_step,
d_board_id)
elif 'dashboard_tasks' in call.data:
d_board_id = call.data.split('_')[-1]
tasks = handlers.get_dashboard_tasks(d_board_id)
if user_checker(call.message.chat.id, tasks):
return
task_hidden = [t.get('id') for t in tasks]
task = [t.get('task_name') for t in tasks]
keyboard = build_inline_keyboard(task, task_hidden,
f'task_detailed_{d_board_id}')
keyboard.add(
types.InlineKeyboardButton(
f'{EMOJI["add"]} Create Task',
callback_data=f'create_task_{d_board_id}'),
types.InlineKeyboardButton(
f'{EMOJI["back"]} Back to Dashboards',
callback_data='dashboard_main'))
bot.edit_message_text(text=f'*Dashboard tasks:*',
chat_id=call.message.chat.id,
message_id=call.message.message_id,
reply_markup=keyboard,
parse_mode='Markdown')
elif 'update_dashboard' in call.data:
d_board = call.data.split('_')
msg = bot.send_message(call.message.chat.id,
f'OK. Give me a new name for dashboard'
f' {d_board[-2]}:',
reply_markup=build_keyboard(
f'{EMOJI["back"]} Back to Main Menu')
)
bot.register_next_step_handler(msg, update_dashboard,
call.message.chat.id, d_board[-1])
elif 'delete_dashboard' in call.data:
d_board = call.data.split('_')
msg = bot.send_message(call.message.chat.id,
f'Are you sure you want to delete dashboard'
f' {d_board[-2]}?',
reply_markup=build_keyboard(
f'{EMOJI["yes"]} Yes',
f'{EMOJI["delete"]} No')
)
bot.register_next_step_handler(msg, delete_dashboard,
call.message.chat.id, d_board[-1])
elif 'user_detailed' in call.data:
user = call.data.split('_')[-1]
d_board_id = call.data.split('_')[-3]
user = handlers.get_user(user)
user_id = user.get('chat_id')
keyboard = types.InlineKeyboardMarkup()
d_board = handlers.get_dashboard(d_board_id)
d_board_name = d_board.get("name")
# adding a delete user button