-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmain.py
1389 lines (1295 loc) · 75.3 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 argparse
import gettext
import importlib
import json
import logging
import os
import queue
import random
import re
import signal
import sqlite3
import threading
from datetime import datetime
from traceback import print_exc
import pytz
from diskcache import Cache
from telebot import types, TeleBot
from telebot.apihelper import create_forum_topic, close_forum_topic, ApiTelegramException, delete_forum_topic, \
reopen_forum_topic
from telebot.types import Message, MessageReactionUpdated
parser = argparse.ArgumentParser(description="")
parser.add_argument("-token", type=str, required=True, help="Telegram bot token")
parser.add_argument("-group_id", type=str, required=True, help="Group ID")
parser.add_argument("-language", type=str, default="en_US", help="Language", choices=["en_US", "zh_CN", "ja_JP"])
args = parser.parse_args()
logger = logging.getLogger()
logger.setLevel("INFO")
BASIC_FORMAT = "%(asctime)s [%(levelname)s] %(message)s"
DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
formatter = logging.Formatter(BASIC_FORMAT, DATE_FORMAT)
chlr = logging.StreamHandler()
chlr.setFormatter(formatter)
logger.addHandler(chlr)
project_root = os.path.dirname(os.path.abspath(__file__))
locale_dir = os.path.join(project_root, "locale")
gettext.bindtextdomain("BetterForward", locale_dir)
gettext.textdomain("BetterForward")
try:
_ = gettext.translation("BetterForward", locale_dir, languages=[args.language]).gettext
except FileNotFoundError:
_ = gettext.gettext
stop = False
def handle_sigterm(*args):
global stop
stop = True
raise KeyboardInterrupt()
def escape_markdown(text):
escape_chars = r'\*_`\[\]()'
return re.sub(f'([{escape_chars}])', r'\\\1', text)
signal.signal(signal.SIGTERM, handle_sigterm)
signal.signal(signal.SIGINT, handle_sigterm)
class TGBot:
def __init__(self, bot_token: str, group_id: str, db_path: str = "./data/storage.db"):
logger.info(_("Starting BetterForward..."))
self.group_id = int(group_id)
self.bot = TeleBot(token=bot_token)
self.bot.edited_message_handler(func=lambda m: True)(self.handle_edit)
self.bot.message_handler(commands=["start", "help"])(self.help)
self.bot.message_handler(commands=["ban"])(self.ban_user)
self.bot.message_handler(commands=["unban"])(self.unban_user)
self.bot.message_handler(commands=["terminate"])(self.handle_terminate)
self.bot.message_handler(commands=["delete"])(self.delete_message)
self.bot.message_handler(commands=["verify"])(self.handle_verify)
self.bot.message_handler(func=lambda m: True, content_types=["photo", "text", "sticker", "video", "document"])(
self.push_messages)
self.bot.message_reaction_handler(func=lambda message: True)(self.handle_reaction)
self.bot.callback_query_handler(func=lambda call: True)(self.callback_query)
self.db_path = db_path
# Check path exists
if not os.path.exists(os.path.dirname(db_path)):
os.makedirs(os.path.dirname(db_path))
self.upgrade_db()
self.bot.set_my_commands([
types.BotCommand("delete", _("Delete a message")),
types.BotCommand("help", _("Show help")),
], scope=types.BotCommandScopeAllPrivateChats())
self.bot.set_my_commands([
types.BotCommand("help", _("Show help")),
types.BotCommand("ban", _("Ban a user")),
types.BotCommand("unban", _("Unban a user")),
types.BotCommand("delete", _("Delete a message")),
types.BotCommand("terminate", _("Terminate a thread")),
types.BotCommand("verify", _("Set verified status")),
], scope=types.BotCommandScopeChat(self.group_id))
self.cache = Cache()
self.load_settings()
self.time_zone = None
self.update_self_time_zone()
self.check_permission()
self.message_queue = queue.Queue()
self.message_processor = threading.Thread(target=self.process_messages)
self.message_processor.start()
self.bot.infinity_polling(skip_pending=True, timeout=30,
allowed_updates=['message', 'edited_message', 'callback_query', 'my_chat_member',
'message_reaction', 'message_reaction_count', ])
def check_valid_chat(self, message: Message):
return message.chat.id == self.group_id and message.message_thread_id is None
def update_self_time_zone(self):
self.time_zone = pytz.timezone(self.cache.get("setting_time_zone"))
def upgrade_db(self):
try:
with sqlite3.connect(self.db_path) as db:
db_cursor = db.cursor()
db_cursor.execute("SELECT value FROM settings WHERE key = 'db_version'")
current_version = int(db_cursor.fetchone()[0])
except sqlite3.OperationalError:
current_version = 0
db_migrate_dir = "./db_migrate"
files = [f for f in os.listdir(db_migrate_dir) if f.endswith('.py')]
files.sort(key=lambda x: int(x.split('_')[0]))
for file in files:
try:
if (version := int(file.split('_')[0])) > current_version:
logger.info(_("Upgrading database to version {}").format(version))
module = importlib.import_module(f"db_migrate.{file[:-3]}")
module.upgrade(self.db_path)
with sqlite3.connect(self.db_path) as db:
db_cursor = db.cursor()
db_cursor.execute("UPDATE settings SET value = ? WHERE key = 'db_version'", (str(version),))
db.commit()
except Exception:
logger.error(_("Failed to upgrade database"))
print_exc()
exit(1)
def load_settings(self):
# Load settings
with sqlite3.connect(self.db_path) as db:
db_cursor = db.cursor()
db_cursor.execute("SELECT key, value FROM settings")
for key, value in db_cursor.fetchall():
self.cache.set(f"setting_{key}", value)
def generate_captcha(self, user_id: int, type="math"):
match type:
case "math":
num1 = random.randint(1, 10)
num2 = random.randint(1, 10)
answer = num1 + num2
self.cache.set(f"captcha_{user_id}", answer, 300)
return f"{num1} + {num2} = ?"
case "button":
markup = types.InlineKeyboardMarkup()
markup.add(types.InlineKeyboardButton("Click to verify", callback_data=json.dumps(
{"action": "verify_button", "user_id": user_id})))
self.bot.send_message(user_id, _("Please click the button to verify."), reply_markup=markup)
case _:
raise ValueError(_("Invalid captcha setting"))
def handle_button_captcha(self, call: types.CallbackQuery):
data = json.loads(call.data)
if data.get("action") == "verify_button":
user_id = data.get("user_id")
if user_id:
self.cache.set(f"verified_{user_id}", True, 1800) # 设置用户为已验证
self.bot.answer_callback_query(call.id)
self.bot.send_message(user_id, _("Verification successful, you can now send messages"))
self.bot.delete_message(call.message.chat.id, call.message.message_id)
with sqlite3.connect(self.db_path) as db:
db_cursor = db.cursor()
db_cursor.execute("INSERT INTO verified_users (user_id) VALUES (?)", (user_id,))
db.commit()
self.cache.set(f"verified_{user_id}", True, 1800)
else:
self.bot.answer_callback_query(call.id)
self.bot.send_message(call.message.chat.id, _("Invalid user ID"))
# Get thread_id to terminate when needed
def terminate_thread(self, thread_id=None, user_id=None):
with sqlite3.connect(self.db_path) as db:
db_cursor = db.cursor()
if thread_id is not None:
result = db_cursor.execute("SELECT user_id FROM topics WHERE thread_id = ? LIMIT 1", (thread_id,))
if (user_id := result.fetchone()) is not None:
user_id = user_id[0]
db_cursor.execute("DELETE FROM topics WHERE thread_id = ?", (thread_id,))
db.commit()
elif user_id is not None:
result = db_cursor.execute("SELECT thread_id FROM topics WHERE user_id = ? LIMIT 1", (user_id,))
if (thread_id := result.fetchone()[0]) is not None:
db_cursor.execute("DELETE FROM topics WHERE user_id = ?", (user_id,))
db.commit()
self.cache.delete(f"chat_{user_id}_threadid")
self.cache.delete(f"threadid_{thread_id}_userid")
try:
delete_forum_topic(chat_id=self.group_id, message_thread_id=thread_id, token=self.bot.token)
except ApiTelegramException:
pass
db_cursor.execute("DELETE FROM messages WHERE topic_id = ?", (thread_id,))
logger.info(_("Terminating thread") + str(thread_id))
# To terminate and totally delete the topic
def handle_terminate(self, message: Message):
if message.chat.id == self.group_id:
user_id = None
thread_id = None
if message.message_thread_id is None:
if len((msg_split := message.text.split(" "))) != 2:
self.bot.reply_to(message, "Invalid command\n"
"Correct usage:```\n"
"/terminate <user ID>```", parse_mode="Markdown")
return
user_id = int(msg_split[1])
else:
thread_id = message.message_thread_id
if thread_id == 1:
self.bot.reply_to(message, _("Cannot terminate main thread"))
return
try:
self.terminate_thread(thread_id=thread_id, user_id=user_id)
if message.message_thread_id is None:
self.bot.reply_to(message, _("Thread terminated"))
except Exception:
logger.error(_("Failed to terminate the thread") + str(thread_id))
self.bot.reply_to(message, _("Failed to terminate the thread"))
else:
self.bot.send_message(message.chat.id, _("This command is only available to admin users."))
def match_auto_response(self, text):
if text is None:
return None
current_time = datetime.now(self.time_zone).time()
with sqlite3.connect(self.db_path) as db:
db.row_factory = sqlite3.Row
db_cursor = db.cursor()
# Check for exact match
db_cursor.execute(
"SELECT value, type, start_time, end_time FROM auto_response WHERE key = ? AND is_regex = 0",
(text,))
results = db_cursor.fetchall()
for result in results:
if self.is_within_time_range(current_time, result['start_time'], result['end_time']):
return {"response": result['value'], "type": result['type']}
# Check for regex match
db_cursor.execute(
"SELECT key, value, type, start_time, end_time FROM auto_response WHERE is_regex = 1")
results = db_cursor.fetchall()
for row in results:
try:
if re.match(row['key'], text) and self.is_within_time_range(current_time, row['start_time'],
row['end_time']):
return {"response": row['value'], "type": row['type']}
except re.error:
logger.error(_("Invalid regular expression: {}").format(row['key']))
return None
return None
def is_within_time_range(self, current_time, start_time, end_time):
if start_time is None or end_time is None:
return True
start_time = datetime.strptime(start_time, "%H:%M").time()
end_time = datetime.strptime(end_time, "%H:%M").time()
if start_time <= end_time:
return start_time <= current_time <= end_time
else:
return current_time >= start_time or current_time <= end_time
# Push messages to the queue
def push_messages(self, message: Message):
self.message_queue.put(message)
# Main message handler
def handle_message(self, message: Message, retry=False):
# Not responding in General topic
if self.check_valid_chat(message):
return
with sqlite3.connect(self.db_path) as db:
curser = db.cursor()
if message.chat.id != self.group_id:
logger.info(
_("Received message from {}, content: {}, type: {}").format(message.from_user.id, message.text,
message.content_type))
if self.cache.get("setting_captcha") != "disable":
# Captcha Handler
if (captcha := self.cache.get(f"captcha_{message.from_user.id}")) is not None:
if message.text != str(captcha):
logger.info(_("User {} entered an incorrect answer").format(message.from_user.id))
self.bot.send_message(message.chat.id, _("The answer is incorrect, please try again"))
return
logger.info(_("User {} passed the captcha").format(message.from_user.id))
self.bot.send_message(message.chat.id, _("Verification successful, you can now send messages"))
curser.execute("INSERT INTO verified_users (user_id) VALUES (?)",
(message.from_user.id,))
self.cache.delete(f"captcha_{message.from_user.id}")
self.cache.set(f"verified_{message.from_user.id}", True, 1800)
return
# Check if the user is verified
verified = self.cache.get(f"verified_{message.from_user.id}")
if verified is None:
result = curser.execute("SELECT 1 FROM verified_users WHERE user_id = ? LIMIT 1",
(message.from_user.id,))
if result.fetchone() is not None:
verified = True
else:
verified = False
if not verified:
logger.info(_("User {} is not verified").format(message.from_user.id))
match self.cache.get("setting_captcha"):
case "button":
self.generate_captcha(message.from_user.id, self.cache.get("setting_captcha"))
return
case "math":
captcha = self.generate_captcha(message.from_user.id, self.cache.get("setting_captcha"))
self.bot.send_message(message.chat.id,
_("Captcha is enabled. Please solve the following question and send the result directly\n") + captcha)
return
case _:
logger.error(_("Invalid captcha setting"))
self.bot.send_message(self.group_id,
_("Invalid captcha setting") + f": {self.cache.get('setting_captcha')}")
return
else:
self.cache.set(f"verified_{message.from_user.id}", verified, 1800)
# Check if the user is banned
if (result := curser.execute("SELECT ban FROM topics WHERE user_id = ? LIMIT 1",
(message.from_user.id,)).fetchone()) and result[0] == 1:
logger.info(_("User {} is banned").format(message.from_user.id))
return
# Auto response
auto_response = None
if (auto_response_result := self.match_auto_response(message.text)) is not None:
if not retry:
match auto_response_result["type"]:
case "text":
self.bot.send_message(message.chat.id,
auto_response_result["response"])
case "photo":
self.bot.send_photo(message.chat.id,
photo=auto_response_result["response"])
case "sticker":
self.bot.send_sticker(message.chat.id,
sticker=auto_response_result["response"])
case "video":
self.bot.send_video(message.chat.id,
video=auto_response_result["response"])
case "document":
self.bot.send_document(message.chat.id,
document=auto_response_result["response"])
case _:
logger.error(_("Unsupported message type") + auto_response_result["type"])
auto_response = auto_response_result["response"]
# Forward message to group
userid = message.from_user.id
if (thread_id := self.cache.get(f"chat_{userid}_threadid")) is None:
result = curser.execute("SELECT thread_id FROM topics WHERE user_id = ? LIMIT 1", (userid,))
thread_id = result.fetchone()
if thread_id is None:
# Create a new thread
logger.info(_("Creating a new thread for user {}").format(userid))
try:
topic = create_forum_topic(chat_id=self.group_id,
name=f"{message.from_user.first_name} | {userid}",
token=self.bot.token)
except Exception as e:
logger.error(e)
return
curser.execute("INSERT INTO topics (user_id, thread_id) VALUES (?, ?)",
(userid, topic["message_thread_id"]))
db.commit()
thread_id = topic["message_thread_id"]
username = _(
"Not set") if message.from_user.username is None else f"@{message.from_user.username}"
last_name = "" if message.from_user.last_name is None else f" {message.from_user.last_name}"
pin_message = self.bot.send_message(self.group_id,
f"User ID: [{userid}](tg://openmessage?user_id={userid})\n"
f"Full Name: {escape_markdown(f"{message.from_user.first_name}{last_name}")}\n"
f"Username: {escape_markdown(username)}\n",
message_thread_id=thread_id, parse_mode='markdown')
self.bot.pin_chat_message(self.group_id, pin_message.message_id)
else:
thread_id = thread_id[0]
self.cache.set(f"chat_{userid}_threadid", thread_id)
try:
reply_id = None
if message.reply_to_message is not None:
if message.reply_to_message.from_user.id == message.from_user.id:
curser.execute(
"SELECT forwarded_id FROM messages WHERE received_id = ? AND topic_id = ? AND in_group = ? LIMIT 1",
(message.reply_to_message.message_id, thread_id, False,))
else:
curser.execute(
"SELECT received_id FROM messages WHERE forwarded_id = ? AND topic_id = ? AND in_group = ? LIMIT 1",
(message.reply_to_message.message_id, thread_id, True,))
if (result := curser.fetchone()) is not None:
reply_id = int(result[0])
match message.content_type:
case "photo":
fwd_msg = self.bot.send_photo(chat_id=self.group_id,
photo=message.photo[-1].file_id,
caption=message.caption,
message_thread_id=thread_id,
reply_to_message_id=reply_id)
case "text":
fwd_msg = self.bot.send_message(chat_id=self.group_id,
text=message.text,
message_thread_id=thread_id,
reply_to_message_id=reply_id)
case "sticker":
fwd_msg = self.bot.send_sticker(chat_id=self.group_id,
sticker=message.sticker.file_id,
message_thread_id=thread_id,
reply_to_message_id=reply_id)
case "video":
fwd_msg = self.bot.send_video(chat_id=self.group_id,
video=message.video.file_id,
caption=message.caption,
message_thread_id=thread_id,
reply_to_message_id=reply_id)
case "document":
fwd_msg = self.bot.send_document(chat_id=self.group_id,
document=message.document.file_id,
caption=message.caption,
message_thread_id=thread_id,
reply_to_message_id=reply_id)
case _:
logger.error(_("Unsupported message type") + message.content_type)
return
curser.execute(
"INSERT INTO messages (received_id, forwarded_id, topic_id, in_group) VALUES (?, ?, ?, ?)",
(message.message_id, fwd_msg.message_id, thread_id, False,))
except ApiTelegramException as e:
if not retry:
self.terminate_thread(thread_id=thread_id)
return self.handle_message(message, retry=True)
else:
logger.error(_("Failed to forward message from user {}".format(message.from_user.id)))
logger.error(e)
self.bot.send_message(self.group_id,
_("Failed to forward message from user {}".format(message.from_user.id)),
message_thread_id=None)
self.bot.forward_message(self.group_id, message.chat.id, message_id=message.message_id)
return
self.bot.send_message(self.group_id, _("[Auto Response]") + auto_response,
message_thread_id=thread_id)
else:
# Forward message to user
if (user_id := self.cache.get(f"threadid_{message.message_thread_id}_userid")) is None:
result = curser.execute("SELECT user_id FROM topics WHERE thread_id = ? LIMIT 1",
(message.message_thread_id,))
user_id = result.fetchone()[0]
self.cache.set(f"threadid_{message.message_thread_id}_userid", user_id)
if user_id is not None:
reply_id = None
if message.reply_to_message is not None:
if message.reply_to_message.from_user.id == message.from_user.id:
curser.execute(
"SELECT forwarded_id FROM messages WHERE received_id = ? AND topic_id = ? AND in_group = ? LIMIT 1",
(message.reply_to_message.message_id, message.message_thread_id, True,))
else:
curser.execute(
"SELECT received_id FROM messages WHERE forwarded_id = ? AND topic_id = ? AND in_group = ? LIMIT 1",
(message.reply_to_message.message_id, message.message_thread_id, False,))
if (result := curser.fetchone()) is not None:
reply_id = int(result[0])
match message.content_type:
case "photo":
fwd_msg = self.bot.send_photo(chat_id=user_id,
photo=message.photo[-1].file_id,
caption=message.caption,
reply_to_message_id=reply_id)
case "text":
fwd_msg = self.bot.send_message(chat_id=user_id,
text=message.text,
reply_to_message_id=reply_id)
case "sticker":
fwd_msg = self.bot.send_sticker(chat_id=user_id,
sticker=message.sticker.file_id,
reply_to_message_id=reply_id)
case "video":
fwd_msg = self.bot.send_video(chat_id=user_id,
video=message.video.file_id,
caption=message.caption,
reply_to_message_id=reply_id)
case "document":
fwd_msg = self.bot.send_document(chat_id=user_id,
document=message.document.file_id,
caption=message.caption,
reply_to_message_id=reply_id)
case _:
logger.error(_("Unsupported message type") + message.content_type)
return
curser.execute(
"INSERT INTO messages (received_id, forwarded_id, topic_id, in_group) VALUES (?, ?, ?, ?)",
(message.message_id, fwd_msg.message_id, message.message_thread_id, True,))
else:
self.bot.send_message(self.group_id, _("Chat not found, please remove this topic manually"),
message_thread_id=message.message_thread_id)
close_forum_topic(chat_id=self.group_id, message_thread_id=message.message_thread_id,
token=self.bot.token)
# Process messages in the queue
def process_messages(self):
while stop is False:
try:
message = self.message_queue.get(timeout=1)
self.handle_message(message)
except queue.Empty:
continue # Skip to the next iteration if the queue is empty
except Exception as e:
logger.error(_("Failed to process message: {}").format(e))
print_exc()
self.bot.stop_bot()
def check_permission(self):
if not self.bot.get_chat(self.group_id).is_forum:
logger.error(_("Topic function is not enabled in this group"))
self.bot.send_message(self.group_id, _("Topic function is not enabled in this group"))
chat_member = self.bot.get_chat_member(self.group_id, self.bot.get_me().id)
permissions = {
_("Manage Topics"): chat_member.can_manage_topics,
_("Delete Messages"): chat_member.can_delete_messages
}
for key, value in permissions.items():
if value is False:
logger.error(_("Bot doesn't have {} permission").format(key))
self.bot.send_message(self.group_id, _("Bot doesn't have {} permission").format(key))
self.bot.send_message(self.group_id, _("Bot started successfully"))
def add_auto_response(self, message: Message):
if not self.check_valid_chat(message):
return
msg = self.bot.edit_message_text(text=_(
"Let's set up an automatic response.\nSend /cancel to cancel this operation.\n\n"
"Please send the keywords or regular expression that should trigger this response."),
chat_id=self.group_id, message_id=message.message_id)
self.bot.register_next_step_handler(msg, self.add_auto_response_type)
def add_auto_response_type(self, message: Message):
if not self.check_valid_chat(message):
return
if isinstance(message.text, str) and message.text == "/cancel":
self.bot.send_message(self.group_id, _("Operation cancelled"))
return
if message.content_type != "text":
self.bot.send_message(self.group_id, _("Invalid input"))
return
self.cache.set("auto_response_key", message.text, 300)
try:
re.compile(message.text)
is_regex = True
except re.error:
is_regex = False
self.cache.set("auto_response_regex", is_regex, 300)
self.add_auto_response_value(message)
def add_auto_response_value(self, message: Message):
if not self.check_valid_chat(message):
return
if isinstance(message.text, str) and message.text == "/cancel":
self.bot.send_message(self.group_id, _("Operation cancelled"))
return
if self.cache.get("auto_response_regex") is True:
try:
re.compile(message.text)
except re.error:
markup = types.InlineKeyboardMarkup()
markup.add(types.InlineKeyboardButton("⬅️" + _("Back"),
callback_data=json.dumps({"action": "auto_reply"})))
self.bot.send_message(
text=_("Invalid regular expression"),
chat_id=self.group_id,
message_thread_id=None,
reply_markup=markup)
return
msg = self.bot.send_message(text=_("Please send the response content. It can be text, stickers, photos "
"and so on."),
chat_id=self.group_id,
message_thread_id=None)
self.bot.register_next_step_handler(msg, self.add_auto_response_time)
def add_auto_response_time(self, message: Message):
if not self.check_valid_chat(message):
return
if isinstance(message.text, str) and message.text == "/cancel":
self.bot.send_message(self.group_id, _("Operation cancelled"))
return
if self.cache.get("auto_response_key") is None:
self.bot.send_message(self.group_id, _("The operation has timed out. Please initiate the process again."))
return
self.cache.set("auto_response_key", self.cache.get("auto_response_key"), 300)
match message.content_type:
case "photo":
self.cache.set("auto_response_value", message.photo[-1].file_id, 300)
self.cache.set("auto_response_type", "photo", 300)
case "text":
self.cache.set("auto_response_value", message.text, 300)
self.cache.set("auto_response_type", "text", 300)
case "sticker":
self.cache.set("auto_response_value", message.sticker.file_id, 300)
self.cache.set("auto_response_type", "sticker", 300)
case "video":
self.cache.set("auto_response_value", message.video.file_id, 300)
self.cache.set("auto_response_type", "video", 300)
case "document":
self.cache.set("auto_response_value", message.document.file_id, 300)
self.cache.set("auto_response_type", "document", 300)
case _:
self.bot.send_message(self.group_id, _("Unsupported message type"))
return
self.cache.set("auto_response_regex", self.cache.get("auto_response_regex"), 300)
markup = types.InlineKeyboardMarkup()
markup.add(types.InlineKeyboardButton("✅" + _("Yes"), callback_data=json.dumps(
{"action": "set_auto_response_time", "value": "yes"})))
markup.add(types.InlineKeyboardButton("❌" + _("No"), callback_data=json.dumps(
{"action": "set_auto_response_time", "value": "no"})))
self.bot.send_message(self.group_id, _("Do you want to set a start and end time for this auto response?"),
reply_markup=markup)
def handle_auto_response_time(self, message: Message):
if not self.check_valid_chat(message):
return
if message.text.lower() == "no":
self.cache.set("auto_response_start_time", None, 300)
self.cache.set("auto_response_end_time", None, 300)
self.process_add_auto_reply(message)
elif message.text.lower() == "yes":
msg = self.bot.send_message(self.group_id,
_("Please enter the start time in HH:MM format (24-hour clock):"))
self.bot.register_next_step_handler(msg, self.set_auto_response_start_time)
else:
msg = self.bot.send_message(self.group_id, _("Invalid input. Please enter 'yes' or 'no':"))
self.bot.register_next_step_handler(msg, self.handle_auto_response_time)
def set_auto_response_start_time(self, message: Message):
if not self.check_valid_chat(message):
return
try:
start_time = datetime.strptime(message.text, "%H:%M").time()
self.cache.set("auto_response_start_time", start_time, 300)
msg = self.bot.send_message(self.group_id, _("Please enter the end time in HH:MM format (24-hour clock):"))
self.bot.register_next_step_handler(msg, self.set_auto_response_end_time)
except ValueError:
msg = self.bot.send_message(self.group_id,
_("Invalid time format.") + "\n" + _(
"Please enter the start time in HH:MM format (24-hour clock):"))
self.bot.register_next_step_handler(msg, self.set_auto_response_start_time)
def set_auto_response_end_time(self, message: Message):
if not self.check_valid_chat(message):
return
try:
end_time = datetime.strptime(message.text, "%H:%M").time()
self.cache.set("auto_response_end_time", end_time, 300)
self.process_add_auto_reply(message)
except ValueError:
msg = self.bot.send_message(self.group_id,
_("Invalid time format.") + "\n" + _(
"Please enter the end time in HH:MM format (24-hour clock):"))
self.bot.register_next_step_handler(msg, self.set_auto_response_end_time)
def process_add_auto_reply(self, message: Message):
key = self.cache.pop("auto_response_key")
value = self.cache.pop("auto_response_value")
is_regex = self.cache.pop("auto_response_regex")
type = self.cache.pop("auto_response_type")
start_time = self.cache.pop("auto_response_start_time")
end_time = self.cache.pop("auto_response_end_time")
if start_time is not None and end_time is not None:
start_time = start_time.strftime("%H:%M")
end_time = end_time.strftime("%H:%M")
markup = types.InlineKeyboardMarkup()
back_button = types.InlineKeyboardButton("⬅️" + _("Back"), callback_data=json.dumps({"action": "menu"}))
if None in [key, value, is_regex, type]:
self.bot.delete_message(self.group_id, message.id)
self.bot.send_message(self.group_id, _("Invalid action"), reply_markup=markup)
return
with sqlite3.connect(self.db_path) as db:
db_cursor = db.cursor()
db_cursor.execute(
"INSERT INTO auto_response (key, value, is_regex, type, start_time, end_time) VALUES (?, ?, ?, ?, ?, ?)",
(key, value, is_regex, type, start_time, end_time))
db.commit()
markup.add(back_button)
self.bot.send_message(text=_("Auto reply added"),
chat_id=message.chat.id,
message_thread_id=None,
reply_markup=markup)
def menu(self, message, edit=False):
if not self.check_valid_chat(message):
return
markup = types.InlineKeyboardMarkup()
buttons = [
types.InlineKeyboardButton("💬" + _("Auto Reply"), callback_data=json.dumps({"action": "auto_reply"})),
types.InlineKeyboardButton("📙" + _("Default Message"), callback_data=json.dumps({"action": "default_msg"})),
types.InlineKeyboardButton("⛔" + _("Banned Users"), callback_data=json.dumps({"action": "ban_user"})),
types.InlineKeyboardButton("🔒" + _("Captcha Settings"),
callback_data=json.dumps({"action": "captcha_settings"})),
types.InlineKeyboardButton("🌍" + _("Time Zone Settings"),
callback_data=json.dumps({"action": "time_zone_settings"})),
types.InlineKeyboardButton("📢" + _("Broadcast Message"),
callback_data=json.dumps({"action": "broadcast_message"}))
]
for i in range(0, len(buttons), 2):
markup.row(*buttons[i:i + 2])
if edit:
self.bot.edit_message_text(_("Menu"), message.chat.id, message.message_id, reply_markup=markup)
else:
self.bot.send_message(self.group_id, _("Menu"), reply_markup=markup, message_thread_id=None)
def help(self, message: Message):
if self.check_valid_chat(message):
self.menu(message)
else:
if (response := self.get_setting('default_message')) is None:
self.bot.send_message(message.chat.id, _("I'm a bot that forwards messages, so please just tell me "
"what you want to say.") + "\n" +
"Powered by [BetterForward](https://github.com/SideCloudGroup/BetterForward)",
parse_mode="Markdown",
disable_web_page_preview=True)
else:
self.bot.send_message(message.chat.id, response)
def get_setting(self, key):
with sqlite3.connect(self.db_path) as db:
db_cursor = db.cursor()
db_cursor.execute("SELECT value FROM settings WHERE key = ? LIMIT 1", (key,))
result = db_cursor.fetchone()
return result[0] if result else None
def validate_time_zone(self, message: Message):
time_zone = message.text
if time_zone == "/cancel":
self.bot.send_message(message.chat.id, _("Operation cancelled"))
return
if time_zone in pytz.all_timezones:
self.set_time_zone(message, time_zone)
else:
msg = self.bot.send_message(message.chat.id, _("Invalid time zone. Please try again:"))
self.bot.register_next_step_handler(msg, self.validate_time_zone)
def time_zone_settings_menu(self, message: Message):
if not self.check_valid_chat(message):
return
current_time_zone = self.get_setting('time_zone')
msg = self.bot.send_message(
text=_(
"Current time zone: {}.\nPlease enter the new time zone (e.g., Europe/London):\n\nSend /cancel to cancel this operation.").format(
current_time_zone),
chat_id=message.chat.id,
message_thread_id=None
)
self.bot.register_next_step_handler(msg, self.validate_time_zone)
def set_time_zone(self, message: Message, value: str):
with sqlite3.connect(self.db_path) as db:
db_cursor = db.cursor()
db_cursor.execute("UPDATE settings SET value = ? WHERE key = 'time_zone'", (value,))
db.commit()
self.cache.set("setting_time_zone", value)
self.update_self_time_zone()
markup = types.InlineKeyboardMarkup()
markup.add(types.InlineKeyboardButton("⬅️" + _("Back"), callback_data=json.dumps({"action": "menu"})))
self.bot.send_message(message.chat.id, _("Time zone updated to {}").format(value), reply_markup=markup)
def manage_auto_reply(self, message: Message, page: int = 1, page_size: int = 5):
with sqlite3.connect(self.db_path) as db:
db.row_factory = sqlite3.Row
db_cursor = db.cursor()
markup = types.InlineKeyboardMarkup()
back_button = types.InlineKeyboardButton("⬅️" + _("Back"),
callback_data=json.dumps({"action": "auto_reply"}))
# Calculate pagination
offset = (page - 1) * page_size
db_cursor.execute("SELECT COUNT(*) FROM auto_response")
total_responses = db_cursor.fetchone()[0]
total_pages = (total_responses + page_size - 1) // page_size
# Fetch data with limits
db_cursor.execute(
"SELECT id, key, value, is_regex, type, start_time, end_time FROM auto_response LIMIT ? OFFSET ?",
(page_size, offset))
auto_responses = db_cursor.fetchall()
text = _("Auto Reply List:") + "\n" + _("Total: {}").format(total_responses) + "\n" + _("Page: {}").format(
page) + "/" + str(total_pages) + "\n\n"
id_buttons = []
for auto_response in auto_responses:
text += "-" * 20 + "\n"
text += f"ID: {auto_response['id']}\n"
text += _("Trigger: {}").format(auto_response['key']) + "\n"
text += _("Response: {}").format(
auto_response['value'] if auto_response['type'] == "text" else auto_response['type']) + "\n"
text += _("Is regex: {}").format("✅" if auto_response['is_regex'] else "❌") + "\n"
text += _("Active time: ")
if auto_response['start_time'] and auto_response['end_time']:
text += "{}~{}".format(auto_response['start_time'],
auto_response['end_time']) + "\n\n"
else:
text += _("Disabled") + "\n\n"
id_buttons.append(types.InlineKeyboardButton(text=f"#{auto_response['id']}",
callback_data=json.dumps({"action": "select_auto_reply",
"id": auto_response['id']})))
# Add ID buttons in a single row
if id_buttons:
markup.row(*id_buttons)
# Add pagination buttons
if 1 < page < total_pages:
markup.row(
types.InlineKeyboardButton("⬅️" + _("Previous Page"),
callback_data=json.dumps(
{"action": "manage_auto_reply", "page": page - 1})),
types.InlineKeyboardButton("➡️" + _("Next Page"),
callback_data=json.dumps(
{"action": "manage_auto_reply", "page": page + 1}))
)
elif page > 1:
markup.add(types.InlineKeyboardButton("⬅️" + _("Previous Page"),
callback_data=json.dumps(
{"action": "manage_auto_reply", "page": page - 1})))
elif page < total_pages:
markup.add(types.InlineKeyboardButton("➡️" + _("Next Page"),
callback_data=json.dumps(
{"action": "manage_auto_reply", "page": page + 1})))
# Add back button in a separate row
markup.add(back_button)
self.bot.edit_message_text(text, message.chat.id, message.message_id, reply_markup=markup)
def select_auto_reply(self, message: Message, id: int):
with sqlite3.connect(self.db_path) as db:
db.row_factory = sqlite3.Row
db_cursor = db.cursor()
db_cursor.execute(
"SELECT key, value, is_regex, type, start_time, end_time FROM auto_response WHERE id = ? LIMIT 1",
(id,))
auto_response = db_cursor.fetchone()
if auto_response is None:
self.bot.send_message(self.group_id, _("Auto reply not found"))
return
markup = types.InlineKeyboardMarkup()
markup.add(types.InlineKeyboardButton("❌" + _("Delete"),
callback_data=json.dumps({"action": "delete_auto_reply", "id": id})))
markup.add(
types.InlineKeyboardButton("⬅️" + _("Back"), callback_data=json.dumps({"action": "manage_auto_reply"})))
text = _("Trigger: {}").format(auto_response["key"]) + "\n"
text += _("Response: {}").format(
auto_response["value"] if auto_response["type"] == "text" else auto_response["type"]) + "\n"
text += _("Is regex: {}").format("✅" if auto_response["is_regex"] else "❌") + "\n"
text += _("Active time: ")
if auto_response['start_time'] and auto_response['end_time']:
text += "{}~{}".format(auto_response['start_time'],
auto_response['end_time']) + "\n\n"
else:
text += _("Disabled") + "\n\n"
self.bot.edit_message_text(text, message.chat.id, message.message_id, reply_markup=markup)
def delete_auto_reply(self, message: Message, id: int):
with sqlite3.connect(self.db_path) as db:
db_cursor = db.cursor()
db_cursor.execute("DELETE FROM auto_response WHERE id = ?", (id,))
db.commit()
markup = types.InlineKeyboardMarkup()
markup.add(
types.InlineKeyboardButton("⬅️" + _("Back"), callback_data=json.dumps({"action": "manage_auto_reply"})))
self.bot.edit_message_text(_("Auto reply deleted"), chat_id=message.chat.id, message_id=message.id,
reply_markup=markup)
def ban_user(self, message: Message):
if message.chat.id == self.group_id and message.message_thread_id is None:
self.bot.send_message(self.group_id, _("This command is not available in the main chat."))
return
if message.chat.id != self.group_id:
self.bot.send_message(message.chat.id, _("This command is only available to admin users."))
return
with sqlite3.connect(self.db_path) as db:
db_cursor = db.cursor()
db_cursor.execute("UPDATE topics SET ban = 1 WHERE thread_id = ?", (message.message_thread_id,))
# Remove user from verified list
db_cursor.execute("DELETE FROM verified_users WHERE user_id = ?", (message.from_user.id,))
db.commit()
self.bot.send_message(self.group_id, _("User banned"), message_thread_id=message.message_thread_id)
close_forum_topic(chat_id=self.group_id, message_thread_id=message.message_thread_id, token=self.bot.token)
def unban_user(self, message: Message, user_id: int = None):
if message.chat.id != self.group_id:
self.bot.send_message(message.chat.id, _("This command is only available to admin users."))
return
if user_id is None:
if self.check_valid_chat(message):
if len((msg_split := message.text.split(" "))) != 2:
self.bot.reply_to(message, "Invalid command\n"
"Correct usage:```\n"
"/unban <user ID>```", parse_mode="Markdown")
return
user_id = int(msg_split[1])
if user_id is None:
with sqlite3.connect(self.db_path) as db:
db_cursor = db.cursor()
db_cursor.execute("UPDATE topics SET ban = 0 WHERE thread_id = ?", (message.message_thread_id,))
db.commit()
self.bot.send_message(self.group_id, _("User unbanned"), message_thread_id=message.message_thread_id)
try:
reopen_forum_topic(chat_id=self.group_id, message_thread_id=message.message_thread_id,
token=self.bot.token)
except ApiTelegramException:
pass
else:
with sqlite3.connect(self.db_path) as db:
db_cursor = db.cursor()
# Check user exists
db_cursor.execute("SELECT thread_id FROM topics WHERE user_id = ? LIMIT 1", (user_id,))
thread_id = db_cursor.fetchone()
if thread_id is None:
self.bot.send_message(self.group_id, _("User not found"))
return
db_cursor.execute("UPDATE topics SET ban = 0 WHERE user_id = ?", (user_id,))
db.commit()
try:
reopen_forum_topic(chat_id=self.group_id, message_thread_id=thread_id,
token=self.bot.token)
except ApiTelegramException:
pass
markup = types.InlineKeyboardMarkup()
markup.add(types.InlineKeyboardButton("⬅️" + _("Back"), callback_data=json.dumps({"action": "menu"})))
if message.from_user.id == self.bot.get_me().id:
self.bot.edit_message_text(_("User unbanned"), message.chat.id, message.message_id, reply_markup=markup)
else:
self.bot.send_message(self.group_id, _("User unbanned"), reply_markup=markup)
def manage_ban_user(self, message: Message):
with sqlite3.connect(self.db_path) as db:
db_cursor = db.cursor()
markup = types.InlineKeyboardMarkup()
back_button = types.InlineKeyboardButton("⬅️" + _("Back"), callback_data=json.dumps({"action": "menu"}))
db_cursor.execute("SELECT user_id FROM topics WHERE ban = 1")
banned_users = db_cursor.fetchall()
text = _("Banned User List:") + "\n"
for user in banned_users:
text += "-" * 20 + "\n"
text += f"User ID: {user[0]}\n"
markup.add(types.InlineKeyboardButton(text=user[0],
callback_data=json.dumps(
{"action": "select_ban_user", "id": user[0]})))
markup.add(back_button)
self.bot.send_message(text=text,
chat_id=message.chat.id,
message_thread_id=None,
reply_markup=markup)
def select_ban_user(self, message: Message, id: int):
with sqlite3.connect(self.db_path) as db:
db_cursor = db.cursor()
db_cursor.execute("SELECT thread_id FROM topics WHERE user_id = ? LIMIT 1", (id,))
thread_id = db_cursor.fetchone()
if thread_id is None:
self.bot.send_message(self.group_id, _("User not found"))
return
markup = types.InlineKeyboardMarkup()
markup.add(types.InlineKeyboardButton("❌" + _("Unban"),
callback_data=json.dumps({"action": "unban_user", "id": id}))
)
markup.add(types.InlineKeyboardButton("⬅️" + _("Back"),
callback_data=json.dumps({"action": "ban_user"})))
self.bot.edit_message_text(f"User ID: {id}", message.chat.id, message.message_id, reply_markup=markup)
def default_msg_menu(self, message: Message):
if not self.check_valid_chat(message):
return
markup = types.InlineKeyboardMarkup()
markup.add(types.InlineKeyboardButton("✏️" + _("Edit Message"),
callback_data=json.dumps({"action": "edit_default_msg"})))
markup.add(types.InlineKeyboardButton("🔄️" + _("Set to Default"),
callback_data=json.dumps({"action": "empty_default_msg"})))
markup.add(types.InlineKeyboardButton("⬅️" + _("Back"), callback_data=json.dumps({"action": "menu"})))
self.bot.send_message(text=_("Default Message") +
"\n" +
_("The default message is an auto-reply to the commands /help and /start"),
chat_id=message.chat.id,
message_thread_id=None, reply_markup=markup)