forked from ecouus/Feed-Push
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelegram_rss_bot.py
More file actions
1009 lines (808 loc) · 41 KB
/
Copy pathtelegram_rss_bot.py
File metadata and controls
1009 lines (808 loc) · 41 KB
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
from telegram.ext import Application, CommandHandler, JobQueue
from telegram.helpers import escape_markdown
from telegram.error import BadRequest
from datetime import datetime
import feedparser
import requests
import os
import json
import re
import time
#v3.0 成功✌️
#重磅更新:
# 支持设置指定RSS源的刷新间隔,添加rss时:/add_rss <rss url> -r <刷新间隔的秒数> 或 已添加的rss:/refresh <rss 编号> <刷新间隔的秒数>;
# 如果某rss已添加,此时若使用/refresh,无论是否已设置刷新时间,都强制将更新该时间
# 同时为所有的订阅源增加秒数字段,在/list_rss输出中也增加相应的提示。
# 配置 - 从环境变量读取
CACHE_FILE = "./data/rss_cache3.txt" # 本地缓存文件
USER_DATA_FILE = "./data/user_data.json" # 存储用户规则和RSS源
ALLOWED_USERS_FILE = "./data/allowed_users.json" # 存储白名单的文件
WHITELIST_STATUS_FILE = "./data/whitelist_status.json" # 白名单模式状态文件
TELEGRAM_BOT_TOKEN = os.getenv('TELEGRAM_BOT_TOKEN')
ROOT_ID = int(os.getenv('ROOT_ID', 0))
WHITELIST_GROUP_ID = os.getenv('WHITELIST_GROUP_ID', '')
ENABLE_GROUP_VERIFY = os.getenv('ENABLE_GROUP_VERIFY', 'false').lower() == 'true'
# UPDATE_INTERVAL 现在作为默认刷新间隔
UPDATE_INTERVAL = int(os.getenv('UPDATE_INTERVAL', 300))
# 确保数据目录存在
os.makedirs('data', exist_ok=True)
# 加载白名单
def load_allowed_users():
# 默认将 ROOT_ID 加入白名单
allowed_users = {ROOT_ID}
if os.path.exists(ALLOWED_USERS_FILE):
with open(ALLOWED_USERS_FILE, "r") as f:
try:
# 从文件中加载用户并与默认的 ROOT_ID 合并
users_from_file = set(json.load(f))
allowed_users.update(users_from_file)
except json.JSONDecodeError:
# 如果文件损坏,则只使用 ROOT_ID
pass
return allowed_users
# 保存白名单
def save_allowed_users(users):
with open(ALLOWED_USERS_FILE, "w") as f:
json.dump(list(users), f)
def is_allowed_user(user_id):
if not is_whitelist_enabled():
return True
allowed_users = load_allowed_users()
return user_id in allowed_users
# 检查用户是否在特定群组中
async def is_user_in_group(user_id, context):
# 如果白名单已关闭(WHITELIST_GROUP_ID = false),直接返回 True
if WHITELIST_GROUP_ID == "false":
return True
# 如果进群验证关闭,直接返回 True
if not ENABLE_GROUP_VERIFY:
return True
try:
# 当 WHITELIST_GROUP_ID 为具体群组 ID 且开启进群验证时,检查用户是否在群组中
member = await context.bot.get_chat_member(WHITELIST_GROUP_ID, user_id)
return member.status in ["member", "administrator", "creator"]
except Exception as e:
print(f"Error checking if user {user_id} is in group: {e}")
return False
# 添加切换进群验证的命令处理函数
async def toggle_group_verify(update, context):
user_id = update.effective_user.id
if user_id != ROOT_ID:
await update.message.reply_text("只有管理员可以操作进群验证开关。")
return
if len(context.args) < 1 or context.args[0].lower() not in ["on", "off"]:
await update.message.reply_text("请提供有效参数:/group_verify on 或 /group_verify off")
return
global ENABLE_GROUP_VERIFY
ENABLE_GROUP_VERIFY = context.args[0].lower() == "on"
status_text = "开启" if ENABLE_GROUP_VERIFY else "关闭"
await update.message.reply_text(f"进群验证已{status_text}。")
# 白名单模式状态文件加载与保存
def load_whitelist_status():
# 检查文件是否存在
if os.path.exists(WHITELIST_STATUS_FILE):
with open(WHITELIST_STATUS_FILE, "r") as f:
try:
# 尝试解析 JSON 内容并返回白名单启用状态,默认为 False
return json.load(f).get("whitelist_enabled", False)
except json.JSONDecodeError:
# 如果文件内容有误,默认为 False(禁用)
return False
# 如果文件不存在,默认返回 True(启用白名单,并且将root id作为白名单)
return True
def save_whitelist_status(status):
# 将状态保存到文件
with open(WHITELIST_STATUS_FILE, "w") as f:
json.dump({"whitelist_enabled": status}, f)
def is_whitelist_enabled():
# 返回白名单启用状态
return load_whitelist_status()
# 加载用户数据
def load_user_data():
if os.path.exists(USER_DATA_FILE):
with open(USER_DATA_FILE, "r") as f:
return json.load(f)
return {}
def save_user_data(user_data):
with open(USER_DATA_FILE, "w") as f:
json.dump(user_data, f, indent=4)
# 用户注册
async def start(update, context):
user_id = update.effective_user.id
if not await is_user_in_group(user_id, context):
await update.message.reply_text("官方群组:https://t.me/youdaolis")
return
if not is_allowed_user(user_id):
await update.message.reply_text("抱歉,您没有权限使用此 Bot。")
return
chat_id = str(update.effective_chat.id)
user_data = load_user_data()
if chat_id not in user_data:
user_data[chat_id] = {"rss_sources": []}
save_user_data(user_data)
await update.message.reply_text("欢迎!您已成功注册。请使用 /add_rss 添加RSS源。使用 /help 获取帮助。")
else:
await update.message.reply_text("您已注册!可以继续添加或管理RSS源和相关规则。使用 /help 获取帮助。")
# 添加 RSS 订阅源 (支持自定义刷新间隔)
async def add_rss(update, context):
user_id = update.effective_user.id
if not await is_user_in_group(user_id, context):
await update.message.reply_text("官方群组:https://t.me/youdaolis")
return
if not is_allowed_user(user_id):
await update.message.reply_text("抱歉,您没有权限使用此 Bot。")
return
chat_id = str(update.effective_chat.id)
user_data = load_user_data()
if chat_id not in user_data:
await update.message.reply_text("请先使用 /start 注册。")
return
args = context.args
if len(args) < 1:
await update.message.reply_text("请提供一个 RSS URL。\n用法: /add_rss <url> [-r <秒数>]\n在话题内使用可自动绑定话题。")
return
# 解析参数以获取URL和刷新间隔
rss_url = ""
refresh_interval = UPDATE_INTERVAL # 默认刷新间隔
if "-r" in args:
try:
r_index = args.index("-r")
if r_index + 1 < len(args) and args[r_index + 1].isdigit():
refresh_interval = int(args[r_index + 1])
# 从参数列表中移除 -r 和对应的秒数
args.pop(r_index)
args.pop(r_index)
else:
await update.message.reply_text("错误:-r 参数后必须跟一个有效的数字(秒数)。")
return
except ValueError:
pass # -r 不存在
if not args:
await update.message.reply_text("请提供一个 RSS URL。")
return
rss_url = args[0].lower()
# 标准化URL,去除末尾的斜杠
if rss_url.endswith('/'):
rss_url = rss_url[:-1]
topic_id = None
if update.message.is_topic_message and update.message.message_thread_id:
topic_id = update.message.message_thread_id
if update.effective_chat.type == 'private':
await update.message.reply_text("错误:此功能必须在群组内使用,不能在私聊中使用。")
return
for rss in user_data[chat_id].get("rss_sources", []):
if topic_id is not None and rss["url"] == rss_url and rss.get("topic_id") == topic_id:
await update.message.reply_text(f"RSS 源 '{rss_url}' 已经订阅到当前话题,无需重复添加。")
return
elif topic_id is None and rss["url"] == rss_url and rss.get("topic_id") is None:
await update.message.reply_text(f"RSS 源 '{rss_url}' 已经订阅到主群组,无需重复添加。")
return
topic_name = None
if update.message.reply_to_message and update.message.reply_to_message.forum_topic_created:
topic_name = update.message.reply_to_message.forum_topic_created.name
rss_data = {
"url": rss_url,
"topic_id": topic_id,
"topic_name": topic_name,
"keywords": [],
"regex_patterns": [],
"regex_keywords": [],
"refresh_interval": refresh_interval, # 新增:刷新间隔
"last_checked": 0 # 新增:上次检查时间戳,0表示立即检查
}
user_data[chat_id]["rss_sources"].append(rss_data)
save_user_data(user_data)
clean_chat_id = chat_id.replace("-100", "")
other_topic_links = []
for rss in user_data[chat_id]["rss_sources"]:
if rss["url"] == rss_url and rss.get("topic_id") != topic_id:
other_topic_id = rss.get("topic_id")
if other_topic_id is not None:
display_name = rss.get("topic_name") or f"Topic: {other_topic_id}"
escaped_display_name = escape_markdown(display_name, version=2)
topic_link = f"https://t.me/c/{clean_chat_id}/{other_topic_id}"
other_topic_links.append(f"[{escaped_display_name}]({topic_link})")
else:
other_topic_links.append("`主群组`")
current_topic_sources = [
r for r in user_data[chat_id]["rss_sources"] if r.get("topic_id") == topic_id
]
sources_list_text = "\n".join(
f" {i + 1}、`{escape_markdown(r['url'], version=2)}` \(间隔: {r.get('refresh_interval', UPDATE_INTERVAL)}秒\)"
for i, r in enumerate(current_topic_sources)
)
escaped_rss_url = escape_markdown(rss_url, version=2)
response_parts = [
f"🟢 RSS 订阅源 '{escaped_rss_url}' 已成功添加到当前话题。",
f"⏱️ 刷新间隔设置为: `{refresh_interval}` 秒。",
f"🔘 当前话题已添加的RSS源:\n{sources_list_text}"
]
if other_topic_links:
other_topics_str = ", ".join(other_topic_links)
separator = escape_markdown("------------------------", version=2)
notification = (
f"{separator}\n"
"🟠 提示:\n"
f"此RSS源也已被添加到其他话题:{other_topics_str}\n"
"如非必要请前往删除。"
)
response_parts.append(notification)
final_response = "\n".join(response_parts)
await update.message.reply_text(final_response, parse_mode="MarkdownV2")
# 设置已添加的RSS源的刷新间隔
async def refresh(update, context):
user_id = update.effective_user.id
if not await is_user_in_group(user_id, context):
await update.message.reply_text("官方群组:https://t.me/youdaolis")
return
if not is_allowed_user(user_id):
await update.message.reply_text("抱歉,您没有权限使用此 Bot。")
return
chat_id = str(update.effective_chat.id)
user_data = load_user_data()
if len(context.args) != 2 or not context.args[0].isdigit() or not context.args[1].isdigit():
await update.message.reply_text("用法错误。\n请提供源编号和刷新间隔(秒),例如:/refresh 1 600")
return
rss_index = int(context.args[0]) - 1
new_interval = int(context.args[1])
if chat_id not in user_data or not (0 <= rss_index < len(user_data[chat_id]["rss_sources"])):
await update.message.reply_text("无效的源编号,请使用 /list_rss 查看。")
return
# 强制更新刷新间隔
user_data[chat_id]["rss_sources"][rss_index]["refresh_interval"] = new_interval
# 重置上次检查时间,使其在下一个检查周期内根据新间隔重新判断
user_data[chat_id]["rss_sources"][rss_index]["last_checked"] = 0
save_user_data(user_data)
rss_url = user_data[chat_id]["rss_sources"][rss_index]["url"]
await update.message.reply_text(f"✅ 源 {rss_index + 1} ({rss_url}) 的刷新间隔已更新为 {new_interval} 秒。")
# 查看所有RSS源
async def list_rss(update, context):
user_id = update.effective_user.id
if not await is_user_in_group(user_id, context):
await update.message.reply_text("官方群组:https://t.me/youdaolis ")
return
if not is_allowed_user(user_id):
await update.message.reply_text("抱歉,您没有权限使用此 Bot。")
return
chat_id = str(update.effective_chat.id)
user_data = load_user_data()
current_topic_id = None
if update.message.is_topic_message and update.message.message_thread_id:
current_topic_id = update.message.message_thread_id
if chat_id not in user_data or not user_data[chat_id]["rss_sources"]:
await update.message.reply_text("您还没有添加任何RSS源。")
return
response_lines = []
clean_chat_id = str(chat_id).replace("-100", "")
for i, rss in enumerate(user_data[chat_id]["rss_sources"]):
escaped_url = escape_markdown(rss['url'], version=2)
# 获取刷新间隔,如果未设置则使用默认值
refresh_interval = rss.get('refresh_interval', UPDATE_INTERVAL)
line = f"{i + 1}、`{escaped_url}`"
rss_topic_id = rss.get("topic_id")
if rss_topic_id is not None:
display_name = rss.get("topic_name") or f"Topic: {rss_topic_id}"
escaped_display_name = escape_markdown(display_name, version=2)
topic_link = f"https://t.me/c/{clean_chat_id}/{rss_topic_id}"
if current_topic_id is not None and int(rss_topic_id) == int(current_topic_id):
line += f" \(📌 *当前话题* [{escaped_display_name}]({topic_link})\)"
else:
line += f" \(👉 *所属话题* [{escaped_display_name}]({topic_link})\)"
else:
line += " \(主群组\)"
# 添加刷新间隔信息
line += f" \(刷新间隔: {refresh_interval}秒\)"
response_lines.append(line)
response = "已添加的RSS源:\n" + "\n".join(response_lines)
await update.message.reply_text(response, parse_mode="MarkdownV2")
# 查看特定RSS源的关键词
async def list_source(update, context):
user_id = update.effective_user.id
if not await is_user_in_group(user_id, context):
await update.message.reply_text("官方群组:https://t.me/youdaolis")
return
if not is_allowed_user(user_id):
await update.message.reply_text("抱歉,您没有权限使用此 Bot。")
return
chat_id = str(update.effective_chat.id)
user_data = load_user_data()
if len(context.args) < 1 or not context.args[0].isdigit():
await update.message.reply_text("请提供一个源编号,例如:/list 1")
return
rss_index = int(context.args[0]) - 1
if chat_id not in user_data or rss_index >= len(user_data[chat_id]["rss_sources"]):
await update.message.reply_text("无效的源编号,请检查已添加的RSS源。")
return
rss = user_data[chat_id]["rss_sources"][rss_index]
# --- 开始修改 ---
topic_info = ""
rss_topic_id = rss.get("topic_id")
if rss_topic_id is not None:
# 1. 创建用于链接的 clean_chat_id
clean_chat_id = str(chat_id).replace("-100", "")
# 2. 获取话题名称,如果不存在则使用ID作为备用
display_name = rss.get("topic_name") or f"Topic: {rss_topic_id}"
# 3. 对名称进行转义以防Markdown解析错误
escaped_display_name = escape_markdown(display_name, version=2)
# 4. 创建跳转链接
topic_link = f"https://t.me/c/{clean_chat_id}/{rss_topic_id}"
# 5. 组合成Markdown链接格式,注意对括号进行转义
topic_info = f" \(所属话题: [{escaped_display_name}]({topic_link})\)"
else:
# 如果没有绑定话题,则显示主群组
topic_info = " \(主群组\)"
# 对RSS URL也进行转义
escaped_url = escape_markdown(rss['url'], version=2)
keywords = rss.get("keywords", [])
if not keywords:
formatted_keywords = "无"
else:
formatted_keywords = "\n".join(f"{i + 1}\\. `{escape_markdown(kw, version=2)}`" for i, kw in enumerate(keywords))
regex_keywords = rss.get("regex_keywords", [])
if not regex_keywords:
formatted_regex = "无"
else:
formatted_regex = "\n".join(f"{i + 1}\\. `{escape_markdown(kw, version=2)}`" for i, kw in enumerate(regex_keywords))
response = f"源 {rss_index + 1} \(`{escaped_url}`\){topic_info} 的规则:\n\n*普通关键词*:\n{formatted_keywords}\n\n*正则表达式*:\n{formatted_regex}"
# 6. 发送消息时必须启用 MarkdownV2 模式
await update.message.reply_text(response, parse_mode="MarkdownV2")
# --- 结束修改 ---
def validate_regex(pattern):
try:
re.compile(pattern)
return True, None
except re.error as e:
return False, str(e)
def create_regex_pattern(pattern_str):
if not any(c in pattern_str for c in "+-"):
return f".*{re.escape(pattern_str)}.*"
parts = pattern_str.split("+")
positive_patterns = []
negative_patterns = []
for part in parts:
if not part:
continue
if "-" in part:
neg_parts = part.split("-")
if neg_parts[0]:
positive_patterns.append(f"(?=.*{re.escape(neg_parts[0])})")
for neg_part in neg_parts[1:]:
if neg_part:
negative_patterns.append(f"(?!.*{re.escape(neg_part)})")
else:
positive_patterns.append(f"(?=.*{re.escape(part)})")
return "^" + "".join(negative_patterns + positive_patterns) + ".*$"
# 添加关键词到特定RSS源
async def add(update, context):
user_id = update.effective_user.id
if not await is_user_in_group(user_id, context):
await update.message.reply_text("官方群组:https://t.me/youdaolis")
return
if not is_allowed_user(user_id):
await update.message.reply_text("抱歉,您没有权限使用此 Bot。")
return
chat_id = str(update.effective_chat.id)
user_data = load_user_data()
if len(context.args) < 2 or not context.args[0].isdigit():
await update.message.reply_text(
"请提供源编号和内容,例如:\n\n"
"📝 **添加智能关键词:**\n"
"/add 1 dmit 添加单个关键词\n"
"/add 1 dmit vps hosting 添加多个关键词\n"
"/add 1 +VPS+优惠-免费 复杂规则(包含VPS和优惠,但不包含免费)\n"
"/add 1 +A+B-C +X-Y 添加多个复杂规则\n\n"
"🔍 **添加正则表达式:**\n"
"/add 1 regex \\d+GB 添加正则表达式\n"
"/add 1 regex (VPS|服务器) 添加正则表达式\n\n"
"**格式说明:**\n"
"• 直接输入内容 = 智能关键词(支持 +A+B-C 语法)\n"
"• 使用 'regex' 前缀 = 正则表达式\n"
"• +A+B 表示必须同时包含A和B\n"
"• +A-B 表示必须包含A但不能包含B")
return
rss_index = int(context.args[0]) - 1
if chat_id not in user_data or rss_index >= len(user_data[chat_id]["rss_sources"]):
await update.message.reply_text("无效的源编号,请检查已添加的RSS源。")
return
if "keywords" not in user_data[chat_id]["rss_sources"][rss_index]:
user_data[chat_id]["rss_sources"][rss_index]["keywords"] = []
if "regex_patterns" not in user_data[chat_id]["rss_sources"][rss_index]:
user_data[chat_id]["rss_sources"][rss_index]["regex_patterns"] = []
if "regex_keywords" not in user_data[chat_id]["rss_sources"][rss_index]:
user_data[chat_id]["rss_sources"][rss_index]["regex_keywords"] = []
if context.args[1].lower() == 'regex':
if len(context.args) < 3:
await update.message.reply_text("请提供正则表达式内容,例如:/add 1 regex \\d+GB")
return
regex_pattern = " ".join(context.args[2:])
is_valid, error_msg = validate_regex(regex_pattern)
if not is_valid:
await update.message.reply_text(f"❌ 正则表达式语法错误:{error_msg}\n请检查您的正则表达式语法。")
return
user_data[chat_id]["rss_sources"][rss_index]["regex_keywords"].append(regex_pattern)
save_user_data(user_data)
regex_keywords = user_data[chat_id]["rss_sources"][rss_index]["regex_keywords"]
regex_list = "\n".join(f"{i + 1}. {regex}" for i, regex in enumerate(regex_keywords))
await update.message.reply_text(
f"✅ 已添加正则表达式到源 {rss_index + 1}:\n• {regex_pattern}\n\n"
f"🔍 当前的正则表达式列表:\n{regex_list}")
else:
patterns = context.args[1:]
added_keywords = []
for pattern in patterns:
pattern = pattern.lower().strip()
if pattern:
user_data[chat_id]["rss_sources"][rss_index]["keywords"].append(pattern)
regex_pattern = create_regex_pattern(pattern)
user_data[chat_id]["rss_sources"][rss_index]["regex_patterns"].append(regex_pattern)
added_keywords.append(pattern)
save_user_data(user_data)
keywords = user_data[chat_id]["rss_sources"][rss_index]["keywords"]
keyword_list = "\n".join(f"{i + 1}. {kw}" for i, kw in enumerate(keywords))
added_summary = "\n".join(f"• {kw}" for kw in added_keywords)
has_complex = any(any(c in kw for c in "+-") for kw in added_keywords)
if has_complex:
await update.message.reply_text(
f"✅ 已添加以下智能关键词到源 {rss_index + 1}:\n{added_summary}\n\n"
f"📝 当前的完整关键词列表:\n{keyword_list}\n\n"
f"💡 提示:使用了复杂匹配规则,系统将智能解析 +/- 语法")
else:
await update.message.reply_text(
f"✅ 已添加以下关键词到源 {rss_index + 1}:\n{added_summary}\n\n"
f"📝 当前的完整关键词列表:\n{keyword_list}")
# 删除特定RSS源的关键词
async def rm(update, context):
user_id = update.effective_user.id
if not await is_user_in_group(user_id, context):
await update.message.reply_text("官方群组:https://t.me/youdaolis")
return
if not is_allowed_user(user_id):
await update.message.reply_text("抱歉,您没有权限使用此 Bot。")
return
chat_id = str(update.effective_chat.id)
user_data = load_user_data()
if len(context.args) < 2 or not context.args[0].isdigit():
await update.message.reply_text(
"请提供源编号和要删除的序号,例如:\n"
"/rm 1 2 删除关键词\n"
"/rm 1 regex 1 删除正则表达式\n"
"/rm 1 1 2 3 删除多个关键词")
return
rss_index = int(context.args[0]) - 1
if chat_id not in user_data or rss_index >= len(user_data[chat_id]["rss_sources"]):
await update.message.reply_text("无效的源编号,请检查已添加的RSS源。")
return
rss_source = user_data[chat_id]["rss_sources"][rss_index]
if len(context.args) >= 3 and context.args[1].lower() == 'regex':
try:
indices = sorted([int(idx) - 1 for idx in context.args[2:]], reverse=True)
except ValueError:
await update.message.reply_text("请提供有效的正则表达式序号")
return
current_regex = rss_source.get("regex_keywords", [])
if not current_regex:
await update.message.reply_text("当前没有可删除的正则表达式")
return
if any(idx < 0 or idx >= len(current_regex) for idx in indices):
current_list = "\n".join(f"{i + 1}. {regex}" for i, regex in enumerate(current_regex))
await update.message.reply_text(
f"存在无效的正则表达式序号。当前的正则表达式列表:\n{current_list}")
return
removed_regex = [current_regex[i] for i in sorted(indices)]
for idx in indices:
current_regex.pop(idx)
save_user_data(user_data)
if not current_regex:
updated_list = "当前没有正则表达式"
else:
updated_list = "\n".join(f"{i + 1}. {regex}" for i, regex in enumerate(current_regex))
removed_summary = "\n".join(f"• {regex}" for regex in removed_regex)
await update.message.reply_text(
f"✅ 已删除以下正则表达式:\n{removed_summary}\n\n"
f"🔍 当前的正则表达式列表:\n{updated_list}")
else:
try:
indices = sorted([int(idx) - 1 for idx in context.args[1:]], reverse=True)
except ValueError:
await update.message.reply_text("请提供有效的关键词序号")
return
current_keywords = rss_source.get("keywords", [])
current_patterns = rss_source.get("regex_patterns", [])
while len(current_patterns) < len(current_keywords):
kw = current_keywords[len(current_patterns)]
current_patterns.append(create_regex_pattern(kw))
current_patterns = current_patterns[:len(current_keywords)]
if not current_keywords:
await update.message.reply_text("当前没有可删除的关键词")
return
if any(idx < 0 or idx >= len(current_keywords) for idx in indices):
current_list = "\n".join(f"{i + 1}. {kw}" for i, kw in enumerate(current_keywords))
await update.message.reply_text(
f"存在无效的关键词序号。当前的关键词列表:\n{current_list}")
return
removed_keywords = []
new_keywords = []
new_patterns = []
for i in range(len(current_keywords)):
if i in indices:
removed_keywords.append(current_keywords[i])
else:
new_keywords.append(current_keywords[i])
new_patterns.append(current_patterns[i])
rss_source["keywords"] = new_keywords
rss_source["regex_patterns"] = new_patterns
save_user_data(user_data)
if not new_keywords:
updated_list = "当前没有关键词"
else:
updated_list = "\n".join(f"{i + 1}. {kw}" for i, kw in enumerate(new_keywords))
removed_summary = "\n".join(f"• {kw}" for kw in removed_keywords)
await update.message.reply_text(
f"✅ 已删除以下关键词:\n{removed_summary}\n\n"
f"📝 当前的关键词列表:\n{updated_list}")
# 删除指定 RSS 订阅源 (支持批量删除)
async def rm_rss(update, context):
user_id = update.effective_user.id
if not await is_user_in_group(user_id, context):
await update.message.reply_text("官方群组:https://t.me/youdaolis")
return
if not is_allowed_user(user_id):
await update.message.reply_text("抱歉,您没有权限使用此 Bot。")
return
chat_id = str(update.effective_chat.id)
user_data = load_user_data()
if not context.args:
await update.message.reply_text("请提供一个或多个源编号,例如:/rm_rss 1 3 5")
return
try:
indices_to_remove = sorted([int(arg) - 1 for arg in set(context.args)], reverse=True)
except ValueError:
await update.message.reply_text("无效的输入,请确保所有编号都是数字。")
return
rss_sources = user_data[chat_id].get("rss_sources", [])
max_index = len(rss_sources) - 1
invalid_indices = [idx + 1 for idx in indices_to_remove if idx < 0 or idx > max_index]
if invalid_indices:
await update.message.reply_text(f"存在无效的源编号:{', '.join(map(str, invalid_indices))}\n请使用 /list_rss 查看有效的编号。")
return
removed_rss_urls = []
for index in indices_to_remove:
removed_item = rss_sources.pop(index)
removed_rss_urls.append(removed_item['url'])
removed_rss_urls.reverse()
save_user_data(user_data)
if not removed_rss_urls:
await update.message.reply_text("没有删除任何RSS源。")
else:
response_lines = ["✅ 已成功删除以下RSS源:"]
for i, url in enumerate(removed_rss_urls):
response_lines.append(f"{i + 1}、 {url}")
await update.message.reply_text("\n".join(response_lines))
# 检查 RSS 并推送新内容 (已更新为独立计时)
async def check_new_posts(context):
print("Running scheduled check for new posts...")
cached_guids = load_cache()
user_data = load_user_data()
current_timestamp = time.time()
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36",
"Accept": "application/rss+xml, application/xml, text/xml",
"Referer": "https://www.google.com",
"Accept-Language": "en-US,en;q=0.9",
}
for chat_id, data in user_data.items():
rss_sources = data.get("rss_sources", [])
for rss in rss_sources:
# 获取每个源的独立刷新配置
refresh_interval = rss.get("refresh_interval", UPDATE_INTERVAL)
last_checked = rss.get("last_checked", 0)
# 判断是否到达刷新时间
if current_timestamp - last_checked < refresh_interval:
continue # 未到时间,跳过
print(f"Checking feed (interval: {refresh_interval}s): {rss['url']}")
# 更新检查时间,无论成功与否都更新,防止因错误导致频繁重试
rss["last_checked"] = current_timestamp
rss_url = rss["url"]
topic_id = rss.get("topic_id")
try:
response = requests.get(rss_url, headers=headers, timeout=10)
response.raise_for_status()
feed = feedparser.parse(response.content)
except requests.RequestException as e:
print(f"Failed to fetch RSS: {rss_url}. Error: {e}")
continue
if not feed.entries:
print(f"No entries found in RSS feed: {rss_url}")
continue
for entry in feed.entries:
guid = entry.id if "id" in entry else entry.link
if guid in cached_guids:
continue
raw_title = entry.title.lower()
title = escape_markdown(entry.title, version=2)
link = escape_markdown(entry.link, version=2)
source_name = rss_url.replace('https://', '').replace('http://', '').split('/')[0]
current_time_str = datetime.now().strftime('%H:%M:%S')
message_sent = False
matched_keyword = None
keywords = rss.get("keywords", [])
regex_patterns = rss.get("regex_patterns", [])
for i, pattern in enumerate(regex_patterns):
try:
if re.search(pattern, raw_title, re.IGNORECASE):
if i < len(keywords):
matched_keyword = keywords[i]
message_text = (
f"📰 *{title}*\n\n"
f"匹配规则:`{escape_markdown(matched_keyword or '未知', version=2)}`\n"
f"🌐 {escape_markdown(source_name, version=2)}\n"
f"🕐 {current_time_str}\n\n"
f"{link}"
)
try:
await context.bot.send_message(
chat_id=chat_id,
text=message_text,
parse_mode="MarkdownV2",
message_thread_id=topic_id,
)
print(f"Message sent to {chat_id} (Topic: {topic_id}): {raw_title} (matched keyword: {matched_keyword})")
except BadRequest as e:
if "Message thread not found" in e.message:
print(f"ERROR: Failed to send to chat {chat_id}, topic {topic_id}. Topic may have been deleted. Error: {e.message}")
else:
print(f"ERROR: An unexpected BadRequest occurred when sending to {chat_id} (Topic: {topic_id}): {e.message}")
except Exception as e:
print(f"ERROR: An unexpected error occurred when sending message: {e}")
cached_guids.add(guid)
message_sent = True
break
except re.error as e:
print(f"Regex error: {e} for pattern: {pattern}")
if not message_sent:
regex_keywords = rss.get("regex_keywords", [])
for regex_pattern in regex_keywords:
try:
if re.search(regex_pattern, raw_title, re.IGNORECASE):
display_pattern = regex_pattern
if len(display_pattern) > 30:
display_pattern = display_pattern[:27] + "..."
message_text = (
f"📰 *{title}*\n\n"
f"匹配规则:`{escape_markdown(display_pattern, version=2)}`\n"
f"🌐 {escape_markdown(source_name, version=2)}\n"
f"🕐 {current_time_str}\n\n"
f"{link}"
)
try:
await context.bot.send_message(
chat_id=chat_id,
text=message_text,
parse_mode="MarkdownV2",
message_thread_id=topic_id,
)
print(f"Message sent to {chat_id} (Topic: {topic_id}): {raw_title} (matched regex: {regex_pattern})")
except BadRequest as e:
if "Message thread not found" in e.message:
print(f"ERROR: Failed to send to chat {chat_id}, topic {topic_id}. Topic may have been deleted. Error: {e.message}")
else:
print(f"ERROR: An unexpected BadRequest occurred when sending to {chat_id} (Topic: {topic_id}): {e.message}")
except Exception as e:
print(f"ERROR: An unexpected error occurred when sending message: {e}")
cached_guids.add(guid)
break
except re.error as e:
print(f"Regex error: {e} for pattern: {regex_pattern}")
# 在所有检查结束后,统一保存缓存和用户数据(包含更新后的last_checked时间戳)
save_cache(cached_guids)
save_user_data(user_data)
def load_cache():
if os.path.exists(CACHE_FILE):
with open(CACHE_FILE, "r") as f:
return set(line.strip() for line in f)
return set()
def save_cache(cache):
with open(CACHE_FILE, "w") as f:
for guid in cache:
f.write(f"{guid}\n")
# 添加用户到白名单
async def add_user(update, context):
user_id = update.effective_user.id
if user_id != ROOT_ID:
await update.message.reply_text("只有管理员可以操作白名单。")
return
if not context.args or not context.args[0].isdigit():
await update.message.reply_text("请提供要添加的用户 ID,例如:/add_user 123456789")
return
new_user_id = int(context.args[0])
allowed_users = load_allowed_users()
if new_user_id in allowed_users:
await update.message.reply_text(f"用户 ID {new_user_id} 已在白名单中。")
return
allowed_users.add(new_user_id)
save_allowed_users(allowed_users)
await update.message.reply_text(f"用户 ID {new_user_id} 已成功添加到白名单。")
# 白名单开关
async def toggle_whitelist(update, context):
user_id = update.effective_user.id
if user_id != ROOT_ID:
await update.message.reply_text("只有管理员可以操作白名单模式。")
return
if len(context.args) < 1 or context.args[0].lower() not in ["on", "off"]:
await update.message.reply_text("请提供有效参数:/whitelist on 或 /whitelist off")
return
status = context.args[0].lower() == "on"
save_whitelist_status(status)
status_text = "开启" if status else "关闭"
await update.message.reply_text(f"白名单模式已{status_text}。")
# 处理 /help 命令
async def help_command(update, context):
user_id = update.effective_user.id
if not await is_user_in_group(user_id, context):
await update.message.reply_text("官方群组:https://t.me/youdaolis")
return
if not is_allowed_user(user_id):
await update.message.reply_text("抱歉,您没有权限使用此 Bot。")
return
help_text = (
"👻Modified By @ZeldaYYDS 以下是可用命令的列表:\n"
"/start - 注册与启动服务\n"
"/help - 查看帮助信息\n"
"/add_rss <url> [-r <秒数>] - 添加RSS源,可选-r参数指定刷新间隔。\n"
"/list_rss - 列出所有已添加的RSS源(及其绑定的Topic和刷新间隔)\n"
"/refresh <编号> <秒数> - 强制更新指定RSS源的刷新间隔。\n"
"/list <编号> - 查看特定RSS源的详细信息\n"
"/add <编号> ... - 为指定编号的RSS源添加智能关键词或正则表达式\n"
" 📝 智能关键词示例:\n"
" /add 1 dmit - 添加关键词\n"
" /add 1 +VPS+优惠-免费 - 复杂规则\n"
" 🔍 正则表达式示例:\n"
" /add 1 regex \\d+GB - 匹配数字+GB\n"
"/rm <编号> ... - 删除关键词或正则表达式\n"
" /rm 1 2 - 删除关键词\n"
" /rm 1 regex 1 - 删除正则表达式\n"
"/rm_rss <编号> - 删除指定编号的RSS源\n"
" \n"
"管理员命令\n"
"/add_user <用户ID> - 将用户添加到白名单(仅管理员可用)\n"
"/group_verify <on/off> - 开启或关闭进群验证 (仅管理员可用)\n"
"/whitelist <on/off> - 开启或关闭白名单模式(仅管理员可用)\n"
"\n"
"💡 功能说明:\n"
"• 同一个RSS源可以被添加到多个不同的话题中。\n"
"• 每个RSS源可以设置独立的刷新间隔。\n"
"• 智能关键词支持 +A+B-C 复杂语法。\n"
"• 正则表达式用于高级匹配。\n"
"\n"
)
await update.message.reply_text(help_text)
# 主函数
def main():
if not TELEGRAM_BOT_TOKEN:
print("错误:未设置 TELEGRAM_BOT_TOKEN 环境变量")
return
if not ROOT_ID:
print("错误:未设置 ROOT_ID 环境变量")
return
application = Application.builder().token(TELEGRAM_BOT_TOKEN).job_queue(JobQueue()).build()
application.add_handler(CommandHandler("start", start))
application.add_handler(CommandHandler("add_rss", add_rss))
application.add_handler(CommandHandler("list_rss", list_rss))
application.add_handler(CommandHandler("refresh", refresh)) # 新增命令
application.add_handler(CommandHandler("list", list_source))
application.add_handler(CommandHandler("add", add))
application.add_handler(CommandHandler("rm", rm))
application.add_handler(CommandHandler("rm_rss", rm_rss))
application.add_handler(CommandHandler("add_user", add_user))
application.add_handler(CommandHandler("whitelist", toggle_whitelist))
application.add_handler(CommandHandler("group_verify", toggle_group_verify))
application.add_handler(CommandHandler("help", help_command))
# 调度器每60秒运行一次,以检查所有源是否到达其独立的刷新时间
# UPDATE_INTERVAL 环境变量现在用作新添加源的默认刷新间隔