forked from bsimjoo/Telegram-RSS-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Handlers.py
895 lines (816 loc) · 38.3 KB
/
Handlers.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
import html
import json
import logging
import pickle
import random
import string
from threading import Timer
import BugReporter
from datetime import datetime, timedelta
from dateutil.parser import parse
from telegram import (Chat, ChatMember, InlineKeyboardButton,
InlineKeyboardMarkup, InputMediaPhoto, ParseMode,
ReplyKeyboardMarkup, ReplyKeyboardRemove, Update)
from telegram.bot import Bot
from telegram.error import BadRequest, NetworkError, Unauthorized
from telegram.ext import (BaseFilter, CallbackContext, CallbackQueryHandler,
ChatMemberHandler, CommandHandler,
ConversationHandler, Filters, MessageHandler,
Updater)
from telegram.utils.helpers import DEFAULT_NONE
from decorators import (CommandHandlerDecorator, ConversationDecorator,
DispatcherDecorators, HandlerDecorator, auth, MessageHandlerDecorator)
from main import BotHandler
# pylint: disable=unused-variable
def add_owner_handlers(server: BotHandler):
def unknown_query(u: Update, c: CallbackContext):
query = u.callback_query
logging.debug('unknown query, query data:'+query.data)
query.answer("❌ ERROR\nUnknown answer", show_alert = True,)
def unknown_command(u: Update, c: CallbackContext):
u.message.reply_text(server.get_string('unknown'))
dispatcher_decorators = DispatcherDecorators(server.dispatcher)
@dispatcher_decorators.commandHandler
@auth(server.ownerID, unknown_command)
def gentoken(u: Update, c: CallbackContext):
admin_token = ''.join(
[random.choice(string.ascii_letters+string.digits) for x in range(32)])
server.admin_token.append(admin_token)
u.message.reply_html((
'one-time admin token:\n<pre>\n'
f'{admin_token}'
'\n</pre>\n<i>Send this token to anyone you want to promote as admin</i>'
))
# TODO:availability of removing admins feature
# labels: enhancement
@dispatcher_decorators.addHandler
@HandlerDecorator(CallbackQueryHandler,pattern = 'accept-.*')
@auth(server.ownerID, unknown_query)
def confirm_admin(u: Update, c: CallbackContext):
query = u.callback_query
if u.effective_user.id == server.ownerID:
new_admin_id = int(query.data[7:])
server.bot.send_message(
new_admin_id,
f'✅ Accepted, From now on, I know you as my admin')
server.adminID.append(new_admin_id)
server.__set_data__('adminID', server.adminID, DB = server.data_db)
server.admin_token.remove(server.admins_pendding[new_admin_id])
del(server.admins_pendding[new_admin_id])
query.answer('✅ Accepted')
query.message.edit_text(query.message.text+'\n\n✅ Accepted')
else:
query.answer()
@dispatcher_decorators.addHandler
@HandlerDecorator(CallbackQueryHandler,pattern = 'decline-.*')
@auth(server.ownerID, unknown_query)
def decline_admin(u: Update, c: CallbackContext):
query = u.callback_query
if u.effective_user.id == server.ownerID:
new_admin_id = int(query.data[8:])
server.bot.send_message(
new_admin_id,
f"❌ Declined, Owner didn't accepted your request")
server.admin_token.remove(server.admins_pendding[new_admin_id])
del(server.admins_pendding[new_admin_id])
query.answer('❌ Declined')
query.message.edit_text(query.message.text+'\n\n❌ Declined')
else:
query.answer()
def add_debuging_handlers(server: BotHandler):
def unknown_command(u: Update, c: CallbackContext):
u.message.reply_text(server.get_string('unknown'))
dispatcher_decorators = DispatcherDecorators(server.dispatcher)
@dispatcher_decorators.messageHandler(Filters.update, group=0)
def log_update(u: Update, c: CallbackContext):
message = (
'Received a new update event from telegram\n'
f'update = {json.dumps(u.to_dict(), indent = 2, ensure_ascii = False, default=str)}\n'
f'user_data = {json.dumps(c.user_data, indent = 2, ensure_ascii = False, default=str)}\n'
f'chat_data = {json.dumps(c.chat_data, indent = 2, ensure_ascii = False, default=str)}'
)
logging.info(message)
if server.debug:
try:
server.bot.send_message(server.ownerID, html.escape(
message), parse_mode=ParseMode.HTML)
except BaseException as e:
server.log_bug(e, 'Exception while sending update log to owner',
ownerID=server.ownerID, message=html.escape(message))
@dispatcher_decorators.commandHandler
@auth(server.ownerID, unknown_command)
def log_updates(u: Update, c: CallbackContext):
server.debug = not server.debug
if server.debug:
u.message.reply_text(
'Debug enabled. now bot sends all updates for you')
else:
u.message.reply_text('Debug disabled.')
def add_admin_handlers(server: BotHandler):
def unknown_msg(u: Update, c: CallbackContext):
u.message.reply_text(server.get_string('unknown-msg'))
def unknown_command(u: Update, c: CallbackContext):
u.message.reply_text(server.get_string('unknown'))
dispatcher_decorators = DispatcherDecorators(server.dispatcher)
admin_auth = auth(server.adminID, unknown_command)
@dispatcher_decorators.commandHandler
@admin_auth
def my_level(u: Update, c: CallbackContext):
if u.effective_user.id == server.ownerID:
u.message.reply_text(
'Oh, my lord. I respect you.')
elif u.effective_user.id in server.adminID:
u.message.reply_text('Oh, my admin. Hi, How are you?')
@dispatcher_decorators.commandHandler
@admin_auth
def state(u: Update, c: CallbackContext):
members, chats = 0, 0
msg = u.message.reply_text('⏳ Please wait, counting members...')
with server.env.begin(server.chats_db) as txn:
chats = int(txn.stat()["entries"])
for key, value in txn.cursor():
v = pickle.loads(value)
members += v['members-count']
msg.edit_text(
f'👥chats:\t{chats}\n' +
f'👤members:\t{members}\n' +
f'🤵admins:\t{len(server.adminID)}'
)
@dispatcher_decorators.commandHandler
@admin_auth
def listchats(u: Update, c: CallbackContext):
res = ''
with server.env.begin(server.chats_db) as txn:
res = 'total: '+str(txn.stat()["entries"])+'\n'
for key, value in txn.cursor():
chat = pickle.loads(value)
if type(chat) is not type(dict()):
res += html.escape(
f'\n bad data type; type:{type(chat)}, value:{chat}\n')
continue
if 'username' in chat:
chat['username'] = '@'+chat['username']
res += html.escape(json.dumps(chat,
indent=2, ensure_ascii=False))
u.message.reply_html(res)
@dispatcher_decorators.commandHandler
@admin_auth
def send_feed_toall(u: Update, c: CallbackContext):
server.send_feed(
server.render_feed(
next(server.read_feed()),
server.get_string('last-feed')
),
server.iter_all_chats())
@dispatcher_decorators.commandHandler
@admin_auth
def set_interval(u: Update, c: CallbackContext):
if len(c.args) == 1:
if c.args[0].isdigit():
server.interval = int(c.args[0])
u.message.reply_text(
'✅ Interval changed to '+str(server.interval))
server.check_thread.cancel()
if server.check_thread.is_alive():
server.check_thread.join()
server.check_thread = Timer(server.interval, server.check_new_feed)
server.check_thread.start()
server.logger.info('Interval changed to '+str(server.interval))
server.set_data(
'interval', server.interval, DB = server.data_db)
else:
u.message.reply_markdown_v2(
'❌ Bad command, use `/set_interval {new interval in seconds}`')
def add_keyboard(c: CallbackContext):
'A function that create keyboard that needed in send_all conversation'
keys = ['❌Cancel']
if len(c.user_data['messages']):
keys = ['✅Send', '👁Preview', '❌Cancel']
markdown = c.user_data['parser'] == ParseMode.HTML
return ReplyKeyboardMarkup(
[
[('✅ HTML Enabled' if markdown else '◻️ HTML Disabled')],
keys
],
resize_keyboard=True
)
STATE_ADD, STATE_EDIT, STATE_DELETE, STATE_CONFIRM = range(4)
@CommandHandlerDecorator
@admin_auth
def sendall(u: Update, c: CallbackContext):
if u.effective_chat.type != Chat.PRIVATE:
u.message.reply_text(
'❌ ERROR\nthis command only is available in private')
return ConversationHandler.END
c.user_data['last-message'] = u.message.reply_text(
'You can send text or photo.', disable_notification=True)
c.user_data['messages'] = []
c.user_data['parser'] = DEFAULT_NONE
u.message.reply_text(
'OK, Send a message to forward it to all users',
reply_markup=add_keyboard(c))
return STATE_ADD
sendall_conv_handler = ConversationDecorator([sendall], per_user=True)
@sendall_conv_handler.state(STATE_ADD)
@MessageHandlerDecorator(Filters.regex("^✅Send$"))
def confirm(u: Update, c: CallbackContext):
c.user_data['last-message'].delete()
c.user_data['last-message'] = server.bot.send_message(
u.effective_chat.id,
'Are you sure, you want to send message' +
('s' if len(c.user_data['messages']) > 1 else '') +
'to all users, groups and channels?',
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton(
"👍Yes, that's OK!", callback_data='yes'),
InlineKeyboardButton(
"✋No, stop!", callback_data='no')
]
]
)
)
return STATE_CONFIRM
text_markup = InlineKeyboardMarkup([
[
InlineKeyboardButton('✏️Edit', callback_data='edit'),
InlineKeyboardButton('❌Delete', callback_data='delete')
]
])
photo_markup = InlineKeyboardMarkup([
[
InlineKeyboardButton('✏️Edit', callback_data='edit'),
InlineKeyboardButton('📝Edit caption', callback_data='edit-cap'),
InlineKeyboardButton('❌Delete', callback_data='delete')
]
])
def cleanup_last_preview(chat_id, c: CallbackContext):
if 'prev-dict' in c.user_data:
for msg_id in c.user_data['prev-dict']:
c.bot.edit_message_reply_markup(
chat_id,
msg_id,
)
def cancel(state) -> callable:
_cancel = None
if state in (STATE_ADD, STATE_CONFIRM):
def _cancel(u: Update, c: CallbackContext):
for key in ('messages', 'prev-dict', 'had-error', 'edit-cap', 'editing-prev-id'):
if key in c.user_data:
del(c.user_data[key])
c.user_data['last-message'].delete()
c.user_data['last-message'] = server.bot.send_message(u.effective_chat.id,
'Canceled', reply_markup=ReplyKeyboardRemove())
return ConversationHandler.END
elif state == STATE_EDIT:
def _cancel(u: Update, c: CallbackContext):
for key in ('edit-cap', 'editing-prev-id'):
if key in c.user_data:
del(c.user_data[key])
return STATE_ADD
elif state == STATE_DELETE:
def _cancel(u: Update, c: CallbackContext):
query = u.callback_query
query.answer('❌ Canceled')
query.edit_message_reply_markup(
InlineKeyboardMarkup([
[InlineKeyboardButton('✏️Edit', callback_data='edit'), InlineKeyboardButton(
'❌Delete', callback_data='delete')]
]))
c.user_data['last-message'].delete()
c.user_data['last-message'] = server.bot.send_message(
u.effective_chat.id,
'OK, now what? (send a message to add)',
reply_markup=add_keyboard(c))
return STATE_ADD
return _cancel
@sendall_conv_handler.state(STATE_ADD)
@MessageHandlerDecorator(Filters.regex("^👁Preview$"))
def preview(u: Update, c: CallbackContext):
cleanup_last_preview(u.effective_chat.id, c)
c.user_data['prev-dict'] = dict()
chat = u.effective_chat
for msg in c.user_data['messages']:
if msg['type'] == 'text':
try:
msg_id = chat.send_message(
msg['text'],
parse_mode=msg['parser'],
reply_markup=text_markup
).message_id
except BadRequest as ex:
msg_id = chat.send_message(
msg['text']+'\n\n⚠️ CAN NOT PARSE.\n'+ex.message,
reply_markup=text_markup
).message_id
c.user_data['had-error'] = True
c.user_data['prev-dict'][msg_id] = msg
elif msg['type'] == 'photo':
try:
msg_id = chat.send_photo(
msg['photo'],
msg['caption'],
parse_mode=msg['parser'],
reply_markup=photo_markup
).message_id
except BadRequest as ex:
msg_id = chat.send_photo(
msg['photo'],
caption=msg['caption'] +
'\n\n⚠️ CAN NOT PARSE.\n'+ex.message,
reply_markup=photo_markup
).message_id
c.user_data['had-error'] = True
c.user_data['prev-dict'][msg_id] = msg
else:
logging.error('UNKNOWN MSG TYPE FOUND\n'+str(msg))
c.bot.send_message(
server.ownerID, 'UNKNOWN MSG TYPE FOUND\n'+str(msg))
BugReporter.bug('unknown type message in preview',
'UNKNOWN MSG TYPE FOUND\n'+str(msg))
if c.user_data.get('had-error'):
c.user_data['last-message'] = u.message.reply_text(
'🛑 there is a problem with your messages, please fix them.',
reply_markup=add_keyboard(c))
else:
c.user_data['last-message'] = u.message.reply_text('OK, now what? (send a message to add)',
reply_markup=add_keyboard(c))
return STATE_ADD
sendall_conv_handler.state(STATE_ADD)(
MessageHandler(Filters.regex("^❌Cancel$"), cancel(STATE_ADD))
)
@sendall_conv_handler.state(STATE_ADD, STATE_EDIT)
@MessageHandlerDecorator(Filters.regex("^✅ HTML Enabled$") | Filters.regex("^◻️ HTML Disabled$"))
def toggle_markdown(u: Update, c: CallbackContext):
if c.user_data['parser'] == ParseMode.HTML:
c.user_data['parser'] = DEFAULT_NONE
u.message.reply_text('◻️ HTML Disabled',
reply_markup=add_keyboard(c))
else:
c.user_data['parser'] = ParseMode.HTML
u.message.reply_text(
'✅ HTML Enabled', reply_markup=add_keyboard(c))
@sendall_conv_handler.state(STATE_ADD)
@MessageHandlerDecorator(Filters.text)
def add_text(u: Update, c: CallbackContext):
text = u.message.text
if c.user_data['parser'] == ParseMode.HTML:
text = str(server.purge(text, False))
c.user_data['messages'].append(
{
'type': 'text',
'text': text,
'parser': c.user_data['parser']
}
)
c.user_data['last-message'].delete()
c.user_data['last-message'] = u.message.reply_text('OK, I received your message now what? (send a message to add)',
reply_markup=add_keyboard(c))
return STATE_ADD
@sendall_conv_handler.state(STATE_ADD)
@MessageHandlerDecorator(Filters.photo)
def add_photo(u: Update, c: CallbackContext):
text = u.message.caption
if c.user_data['parser'] == ParseMode.HTML:
text = str(server.purge(text, False))
c.user_data['messages'].append(
{
'type': 'photo',
'photo': u.message.photo[-1],
'caption': text,
'parser': c.user_data['parser']
}
)
c.user_data['last-message'].delete()
c.user_data['last-message'] = u.message.reply_text(
'OK, I received photo%s now what? (send a message to add)' % (
's' if len(u.message.photo) > 1 else ''),
reply_markup=add_keyboard(c))
return STATE_ADD
sendall_conv_handler.state(STATE_EDIT)(
MessageHandler(Filters.regex("^❌Cancel$"), cancel(STATE_EDIT))
)
@sendall_conv_handler.fallback
@HandlerDecorator(CallbackQueryHandler, pattern='^edit(-cap)?$')
def edit(u: Update, c: CallbackContext):
query = u.callback_query
edit_cap = query.data == 'edit-cap'
query.answer()
c.user_data['last-message'].delete()
c.user_data['last-message'] = server.bot.send_message(u.effective_chat.id,
'✏️ EDITING CAPTION\nSend new caption.' if edit_cap else '✏️ EDITING\nSend new edition.',
reply_markup=ReplyKeyboardMarkup([['❌Cancel']], resize_keyboard=True))
c.user_data['editing-prev-id'] = query.message.message_id
c.user_data['edit-cap'] = edit_cap
return STATE_EDIT
@sendall_conv_handler.state(STATE_EDIT)
@MessageHandlerDecorator(Filters.text)
def text_edited(u: Update, c: CallbackContext):
if not u.message:
return STATE_EDIT
# id of the message that bot sent as preview
preview_msg_id = c.user_data['editing-prev-id']
# get msg by searching preview message id in prev-dict
msg = c.user_data['prev-dict'][preview_msg_id]
edited_txt = u.message.text
if c.user_data['parser'] == ParseMode.HTML:
edited_txt = str(server.purge(edited_txt, False))
if msg.get('had-error'):
del(msg['had-error'])
if c.user_data.get('had-error'):
del(c.user_data['had-error'])
msg['parser'] = c.user_data['parser']
if msg['type'] == 'text':
msg['text'] = edited_txt # change message text
try:
c.bot.edit_message_text( # edit preview message text
edited_txt,
u.effective_chat.id,
preview_msg_id,
parse_mode=msg['parser'],
reply_markup=text_markup
)
except BadRequest as ex:
c.bot.edit_message_text(
edited_txt+'\n\n⚠️ CAN NOT PARSE.\n'+ex.message,
u.effective_chat.id,
preview_msg_id,
reply_markup=text_markup
)
msg['had-error'] = True
c.user_data['had-error'] = True
elif msg['type'] == 'photo':
if c.user_data['edit-cap']:
msg['caption'] = u.message.text
try:
c.bot.edit_message_caption(
u.effective_chat.id,
preview_msg_id,
caption=edited_txt,
parse_mode=msg['parser'],
reply_markup=photo_markup
)
except BadRequest as ex:
c.bot.edit_message_caption(
u.effective_chat.id,
preview_msg_id,
caption=edited_txt+'\n\n⚠️ CAN NOT PARSE.\n'+ex.message,
reply_markup=photo_markup
)
msg['had-error'] = True
c.user_data['had-error'] = True
else:
# change message type from photo to text
msg['type'] = 'text'
del(msg['photo'], msg['caption'])
msg['text'] = edited_txt
c.bot.edit_message_caption(
caption='⚠️ This message type had been changed from photo to text. ' +
'You can request for a new preview to see this message.',
chat_id=u.effective_chat.id,
message_id=preview_msg_id
)
else:
# Log this bug
logging.error('UNKNOWN MSG TYPE FOUND\n'+str(msg))
c.bot.send_message(
server.ownerID, 'UNKNOWN MSG TYPE FOUND\n'+str(msg))
if c.user_data.get('had-error'):
c.user_data['last-message'] = u.message.reply_text(
'🛑 there is a problem with your messages, please fix them.',
parse_mode=ParseMode.MARKDOWN_V2,
reply_markup=add_keyboard(c))
else:
c.user_data['last-message'] = u.message.reply_text('✅ Message edited; now you can add more messages or send it',
reply_markup=add_keyboard(c))
return STATE_ADD
@sendall_conv_handler.state(STATE_EDIT)
@MessageHandlerDecorator(Filters.photo)
def photo_edited(u: Update, c: CallbackContext):
# id of the message that bot sent as preview
preview_msg_id = c.user_data['editing-prev-id']
# get msg by searching preview message id in prev-dict
msg = c.user_data['prev-dict'][preview_msg_id]
if msg.get('had-error'):
del(msg['had-error'])
if c.user_data.get('had-error'):
del(c.user_data['had-error'])
msg['parser'] = c.user_data['parser']
msg['photo'] = u.message.photo[-1]
msg['caption'] = u.message.caption
if c.user_data['parser'] == ParseMode.HTML:
msg['caption'] = str(server.purge(msg['caption'], False))
if c.user_data['parser'] == ParseMode.MARKDOWN_V2:
msg['caption'] = u.message.caption_markdown_v2
if msg['type'] == 'photo':
try:
c.bot.edit_message_media(
u.effective_chat.id,
preview_msg_id,
media=InputMediaPhoto(
media=msg['photo'],
caption=msg['caption'],
parse_mode=msg['parser'])
)
except BadRequest as ex:
c.bot.edit_message_media(
u.effective_chat.id,
preview_msg_id,
media=InputMediaPhoto(
media=msg['photo'],
caption=msg['caption']+'\n\n⚠️ CAN NOT PARSE.\n'+ex.message)
)
msg['had-error'] = True
c.user_data['had-error'] = True
elif msg['type'] == 'text':
# change message type to photo
msg['type'] = 'photo'
del(msg['text'])
c.bot.edit_message_text(
'⚠️ This message type had been changed from text to photo. ' +
'You can request for a new preview to see this message.',
u.effective_chat.id,
preview_msg_id,
)
else:
# report bug
logging.error('UNKNOWN MSG TYPE FOUND\n'+str(msg))
c.bot.send_message(
server.ownerID, 'UNKNOWN MSG TYPE FOUND\n'+str(msg))
if c.user_data.get('had-error'):
c.user_data['last-message'] = u.message.reply_text(
'🛑 there is a problem with your messages, please fix them.',
parse_mode=ParseMode.MARKDOWN_V2,
reply_markup=add_keyboard(c))
else:
c.user_data['last-message'] = u.message.reply_text('✅ Message edited; now you can add more messages or send it',
reply_markup=add_keyboard(c))
return STATE_ADD
@sendall_conv_handler.fallback
@HandlerDecorator(CallbackQueryHandler, pattern='^delete$')
def deleting(u: Update, c: CallbackContext):
query = u.callback_query
query.answer()
query.edit_message_reply_markup(
InlineKeyboardMarkup([
[InlineKeyboardButton(
'🛑 Are you sure?', callback_data='None')],
[InlineKeyboardButton('🔴 Yes', callback_data='yes'), InlineKeyboardButton(
'🟢 No', callback_data='no')]
])
)
c.user_data['last-message'].delete()
c.user_data['last-message'] = server.bot.send_message(
u.effective_chat.id, '⏳ Deleting a message...', reply_markup=ReplyKeyboardRemove())
return STATE_DELETE
sendall_conv_handler.state(STATE_DELETE)(
CallbackQueryHandler(cancel(STATE_DELETE), pattern='^no$')
)
@sendall_conv_handler.state(STATE_DELETE)
@HandlerDecorator(CallbackQueryHandler, pattern='^yes$')
def delete(u: Update, c: CallbackContext):
query = u.callback_query
query.answer('✅ Deleted')
preview_msg_id = query.message.message_id
msg = c.user_data['prev-dict'][preview_msg_id]
c.user_data['messages'].remove(msg)
del(c.user_data['prev-dict'][preview_msg_id])
query.edit_message_text('❌')
c.user_data['last-message'].delete()
c.user_data['last-message'] = server.bot.send_message(
u.effective_chat.id, 'OK, now you can send message to add', reply_markup=add_keyboard(c))
return STATE_ADD
def send_message(chat_id, c: CallbackContext):
chat = server.bot.get_chat(chat_id)
for msg in c.user_data['messages']:
# send message to admin for a debug!
if msg['type'] == 'text':
try:
chat.send_message(
msg['text'],
parse_mode=msg['parser']
).message_id
except BadRequest as ex:
chat.send_message(
msg['text']+'\n\n⚠️ CAN NOT PARSE.\n'+ex.message,
reply_markup=text_markup
)
c.user_data['had-error'] = True
msg['had-error'] = True
return STATE_ADD
elif msg['type'] == 'photo':
try:
chat.send_photo(
msg['photo'],
msg['caption'],
parse_mode=msg['parser']
).message_id
except BadRequest as ex:
chat.send_photo(
msg['photo'],
caption=msg['caption'] +
'\n\n⚠️ CAN NOT PARSE.\n'+ex.message
).message_id
c.user_data['had-error'] = True
msg['had-error'] = True
return STATE_ADD
@sendall_conv_handler.state(STATE_CONFIRM)
@HandlerDecorator(CallbackQueryHandler, pattern='^yes$')
def send(u: Update, c: CallbackContext):
query = u.callback_query
if c.user_data.get('had-error'):
query.answer()
u.effective_chat.send_message(
'🛑 there is a problem with your messages, please fix them.',
parse_mode=ParseMode.MARKDOWN_V2,
reply_markup=add_keyboard(c)
)
return STATE_ADD
query.answer(
'✅ Done\nSending message to all users, groups and channels', show_alert=True)
logging.info('Sending message to chats')
c.user_data['last-message'].delete()
c.user_data['last-message'] = server.bot.send_message(u.effective_chat.id,
'✅ Done\nSending message to all users, groups and channels')
res = send_message(u.effective_chat.id, c)
if res:
u.effective_chat.send_message(
'🛑 there is a problem with your messages, please fix them.',
parse_mode=ParseMode.MARKDOWN_V2,
reply_markup=add_keyboard(c)
)
return res
remove_ids = []
for chat_id, chat_data in server.iter_all_chats():
if chat_id != u.effective_chat.id:
try:
send_message(chat_id, c)
except Unauthorized as e:
server.log_bug(e, 'handled an exception while trying to send message to a chat. removing chat',
report=False, chat_id=chat_id, chat_data=chat_data)
try:
with server.env.begin(server.chats_db, write=True) as txn:
txn.delete(str(chat_id).encode())
except Exception as e2:
server.log_bug(
e2, 'exception while trying to remove chat')
remove_ids.append(chat_id)
except Exception as e:
server.log_bug(
e, 'exception while trying to send message to a chat', chat_id=chat_id, chat_data=chat_data)
for chat_id in remove_ids:
with server.env.begin(server.chats_db, write=True) as txn:
txn.delete(str(chat_id).encode())
cleanup_last_preview(u.effective_chat.id, c)
for key in ('messages', 'prev-dict', 'had-error', 'edit-cap', 'editing-prev-id'):
if key in c.user_data:
del(c.user_data[key])
return ConversationHandler.END
sendall_conv_handler.state(STATE_CONFIRM)(
CallbackQueryHandler(cancel(STATE_CONFIRM), pattern='^no$')
)
server.dispatcher.add_handler(sendall_conv_handler.get_handler(), group = 1)
def add_users_handlers(server: BotHandler):
dispatcher_decorators = DispatcherDecorators(server.dispatcher)
@dispatcher_decorators.commandHandler
def start(u: Update, c: CallbackContext):
chat = u.effective_chat
message = u.message
user = u.effective_user
data = chat.to_dict()
data['members-count'] = chat.get_members_count()-1
if chat.type == Chat.PRIVATE:
data.update(user.to_dict())
message.reply_markdown_v2(server.get_string('welcome'))
if len(c.args) == 1:
if c.args[0] == server.token:
if user.id in server.adminID:
message.reply_text(
f'My dear {user.full_name}, I already know you as my lord!')
else:
message.reply_text(
f'Hi my dear {user.full_name}\nFrom now on, I know you as my lord\nyour id is: "{user.id}"')
server.adminID.append(user.id)
server.set_data(
'adminID', server.adminID, DB=server.data_db)
server.ownerID = user.id
server.set_data(
'ownerID', server.ownerID, DB=server.data_db)
elif c.args[0] in server.admin_token:
if user.id in server.adminID:
message.reply_text(
f'My dear {user.full_name}, I already know you as my admin!')
else:
message.reply_text(
'Owner must accept your request.\n⏳ please wait...')
server.admins_pendding[user.id] = c.args[0]
server.bot.send_message(
server.ownerID,
'Hi, A user wants to be admin:\n' +
f'tel-id:\t{user.id}\n' +
f'user-id:\t{user.username}\n' +
f'name:\t{user.full_name}',
reply_markup=InlineKeyboardMarkup(
[[
InlineKeyboardButton(
'✅ Accept', callback_data=f'accept-{user.id}'),
InlineKeyboardButton(
'❌ Decline', callback_data=f'decline-{user.id}')
]])
)
else:
u.message.reply_markdown_v2(
server.get_string('group-intro'))
server.set_data(key=str(chat.id), value=data)
@dispatcher_decorators.commandHandler
def last_feed(u: Update, c: CallbackContext):
if u.effective_user.id not in server.adminID and 'time' in c.user_data:
if c.user_data['time'] > datetime.now():
u.message.reply_text(server.get_string('time-limit-error'))
return
wait_msg = u.message.reply_animation(open("wait animation.tgs", 'rb'))
server.send_feed(
server.render_feed(
next(server.read_feed(0)),
server.get_string('last-feed')
),
chats = [(u.effective_chat.id, c.chat_data)])
wait_msg.delete()
c.user_data['time'] = datetime.now() + timedelta(minutes = 2) #The next request is available 2 minutes later
@dispatcher_decorators.commandHandler(command = 'help')
def help_(u: Update, c: CallbackContext):
if u.effective_chat.id == server.ownerID:
u.message.reply_text(server.get_string('owner-help'))
if u.effective_chat.id in server.adminID:
u.message.reply_text(server.get_string('admin-help'))
u.message.reply_text(server.get_string('help'))
@dispatcher_decorators.messageHandler(Filters.update.edited_message)
def handle_edited_msg(u: Update, c:CallbackContext):
#TODO: Handle editing messages
# Handle messages editing in /send_all could be usefull
# labels: enhancement
u.edited_message.reply_text(server.strings['edited-message'])
def add_other_handlers(server: BotHandler):
dispatcher_decorators = DispatcherDecorators(server.dispatcher)
@dispatcher_decorators.addHandler
@HandlerDecorator(ChatMemberHandler)
def onBotBlocked(u: Update, c:CallbackContext):
if (u.my_chat_member.new_chat_member.user.id == server.bot.id):
status = u.my_chat_member.new_chat_member.status
if status in (ChatMember.KICKED, ChatMember.LEFT, ChatMember.RESTRICTED):
logging.info('Bot had been kicked or blocked by a user')
with server.env.begin(server.chats_db, write = True) as txn:
txn.delete(str(u.my_chat_member.chat.id).encode())
@dispatcher_decorators.messageHandler(Filters.status_update.new_chat_members)
def onjoin(u: Update, c: CallbackContext):
for member in u.message.new_chat_members:
if member.username == server.bot.username:
data = u.effective_chat.to_dict()
data['members-count'] = u.effective_chat.get_members_count()-1
server.set_data(key = str(u.effective_chat.id), value = data)
server.bot.send_message(
server.ownerID,
'<i>Joined to a chat:</i>\n' +
html.escape(json.dumps(
data, indent = 2, ensure_ascii = False)),
ParseMode.HTML,
disable_notification = True)
if u.effective_chat.type != Chat.CHANNEL:
u.message.reply_markdown_v2(
server.get_string('group-intro'))
@dispatcher_decorators.messageHandler(Filters.status_update.left_chat_member)
def onkick(u: Update, c: CallbackContext):
if u.message.left_chat_member['username'] == server.bot.username:
data = server.get_data(str(u.effective_chat.id))
if data:
server.bot.send_message(
server.ownerID,
'<i>Kicked from a chat:</i>\n' +
html.escape(json.dumps(
data, indent = 2, ensure_ascii = False)),
ParseMode.HTML,
disable_notification = True)
with server.env.begin(server.chats_db, write = True) as txn:
txn.delete(str(u.effective_chat.id).encode())
@dispatcher_decorators.errorHandler
def error_handler(update: object, context: CallbackContext) -> None:
"""Log the error and send a telegram message to notify the developer."""
server.log_bug(
context.error,
'Exception while handling an update',
not isinstance(context.error, NetworkError),
update = update.to_dict() if isinstance(update, Update) else str(update),
user_data = context.user_data,
chat_data = context.chat_data
)
def add_unknown_handlers(server: BotHandler):
"this must be the last method you call while adding handlers"
dispatcher_decorators = DispatcherDecorators(server.dispatcher)
@dispatcher_decorators.messageHandler(Filters.command)
def unknown_command(u: Update, c: CallbackContext):
u.message.reply_text(server.get_string('unknown'))
@dispatcher_decorators.messageHandler
def unknown_msg(u: Update, c: CallbackContext):
if u._effective_chat.type == Chat.PRIVATE:
u.message.reply_text(server.get_string('unknown-msg'))