From 86623346951ca04e93f8a4999bcb96946cf44df7 Mon Sep 17 00:00:00 2001 From: yshtcn Date: Fri, 8 Sep 2023 12:04:43 +0800 Subject: [PATCH 1/4] =?UTF-8?q?-=20=E5=A2=9E=E5=8A=A0=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Post_Test.bat | 6 ++++++ Post_Test.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 Post_Test.bat create mode 100644 Post_Test.py diff --git a/Post_Test.bat b/Post_Test.bat new file mode 100644 index 0000000..9fb059b --- /dev/null +++ b/Post_Test.bat @@ -0,0 +1,6 @@ +title 测试post +:start +@cd /d %~dp0 +py Post_Test.py +pause +goto start \ No newline at end of file diff --git a/Post_Test.py b/Post_Test.py new file mode 100644 index 0000000..aa488d2 --- /dev/null +++ b/Post_Test.py @@ -0,0 +1,31 @@ +import requests + +def send_post_request(): + url = "http://localhost:5000" # 请替换为实际API端点 + + # 构建表单数据 + payload = { + "bot_id": "", + "chat_id": "", + "title": "Test Title", + "desp": "Test Description---Test Description", + "url": "https://example.com" + } + + # 发送POST请求 + response = requests.post(url, data=payload) + + # 输出状态码 + print(f"Status Code: {response.status_code}") + + # 输出响应文本 + print(f"Response Text: {response.text}") + + # 在尝试解析JSON之前先检查响应是否包含JSON + try: + print(f"Response Data: {response.json()}") + except: + print("Failed to parse JSON from response.") + +if __name__ == "__main__": + send_post_request() From 54b959c7abb7e84d9469b8839a230168dd83579a Mon Sep 17 00:00:00 2001 From: yshtcn Date: Fri, 8 Sep 2023 12:05:49 +0800 Subject: [PATCH 2/4] =?UTF-8?q?-=20=E5=A2=9E=E5=8A=A0=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E8=8C=83=E4=BE=8B=20-=20=E6=B5=8B=E8=AF=95=E8=8C=83=E4=BE=8B?= =?UTF-8?q?=E5=8C=85=E5=90=AB=E6=96=B0=E5=8A=9F=E8=83=BD=E5=88=86=E9=9A=94?= =?UTF-8?q?=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot_config_example.json | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 bot_config_example.json diff --git a/bot_config_example.json b/bot_config_example.json new file mode 100644 index 0000000..2fbb292 --- /dev/null +++ b/bot_config_example.json @@ -0,0 +1,20 @@ + +[ + { + "main_bot_id": "YOUR_MAIN_BOT_ID_HERE", + "main_chat_id": "YOUR_MAIN_CHAT_ID_HERE", + "sub_bots": [ + { + "bot_id": "YOUR_SUB_BOT_ID_1", + "chat_id": "YOUR_SUB_CHAT_ID_1", + "keywords": ["test"], + "delimiter": "---split---" + }, + { + "bot_id": "YOUR_SUB_BOT_ID_2", + "chat_id": "YOUR_SUB_CHAT_ID_2", + "keywords": ["keyword3", "keyword4"] + } + ] + } +] From f410d5afd97d3d10cec0e696d8b03311c2588710 Mon Sep 17 00:00:00 2001 From: yshtcn Date: Fri, 8 Sep 2023 12:06:07 +0800 Subject: [PATCH 3/4] =?UTF-8?q?-=20=E6=96=B0=E5=A2=9E=E5=88=86=E9=9A=94?= =?UTF-8?q?=E7=AC=A6=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ServerChanPush2TelegramBot.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/ServerChanPush2TelegramBot.py b/ServerChanPush2TelegramBot.py index 4fb231d..7a81a93 100644 --- a/ServerChanPush2TelegramBot.py +++ b/ServerChanPush2TelegramBot.py @@ -11,7 +11,7 @@ from logging.handlers import TimedRotatingFileHandler current_date = datetime.now().strftime("%Y-%m-%d") # 创建一个处理器,该处理器每天午夜都会创建一个新的日志文件 -handler = TimedRotatingFileHandler(f"received_requests_{current_date}.log", when="midnight", interval=1, backupCount=10) +handler = TimedRotatingFileHandler(f"received_requests_{current_date}.log", when="midnight", interval=1, backupCount=10, encoding='utf-8') handler.suffix = "%Y-%m-%d" # 配置日志 @@ -83,6 +83,7 @@ def send_telegram_message(bot_id, chat_id, title, desp=None, url=None): all_bots_config = load_config() # 用于标记是否找到匹配的关键词 found = False + delimiter = None # 遍历所有主 bot_id 的配置 for config in all_bots_config: main_bot_id = config['main_bot_id'] @@ -97,15 +98,17 @@ def send_telegram_message(bot_id, chat_id, title, desp=None, url=None): title_decode = title.decode('utf-8') if isinstance(title, bytes) else title if keyword_decode.lower() in title_decode.lower(): bot_id = sub_bot['bot_id'] - chat_id = sub_bot['chat_id'] # 新增:替换 chat_id + chat_id = sub_bot['chat_id'] # 替换 chat_id + delimiter = sub_bot.get('delimiter') # 获取隔断符配置 found = True break - if found: - break - - # 一旦找到匹配的主 bot_id 和主 chat_id,就跳出循环 if found: - break + break + + # 一旦找到匹配的主 bot_id 和主 chat_id,就跳出循环 + if found: + break + api_url = f"https://api.telegram.org/bot{bot_id}/sendMessage" @@ -115,8 +118,8 @@ def send_telegram_message(bot_id, chat_id, title, desp=None, url=None): } text = title # 初始化 text 为 title - if desp: # 如果有 desp,添加到 text - text += f"\n\n{desp}" + text += f"\n\n{(desp.split(delimiter)[0] if delimiter and desp else desp) if desp else ''}" + # 使用正则表达式来识别受影响的链接 @@ -128,7 +131,7 @@ def send_telegram_message(bot_id, chat_id, title, desp=None, url=None): text = text.replace(affected_url, corrected_url) if url: # 如果有 url,添加到 text - text += f"\n\n详情:" + text += f"\n\n[详情]({url})" text += f"{url}" # 直接添加 URL,Telegram 会自动处理预览 text=unescape_url(text) @@ -137,7 +140,7 @@ def send_telegram_message(bot_id, chat_id, title, desp=None, url=None): payload = { 'chat_id': chat_id, 'text': text, - 'parse_mode': 'HTML', + 'parse_mode': 'markdown', 'disable_web_page_preview': False # 启用网页预览 } try: From 88b67285202809fd761386c47b5839d6c19db27a Mon Sep 17 00:00:00 2001 From: yshtcn Date: Fri, 8 Sep 2023 12:07:02 +0800 Subject: [PATCH 4/4] =?UTF-8?q?-=20=E5=A2=9E=E5=8A=A0=E5=BF=BD=E7=95=A5?= =?UTF-8?q?=E5=8F=AF=E8=83=BD=E5=90=AB=E6=9C=89=E6=95=8F=E6=84=9F=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E7=9A=84=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=20-=20?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=BF=BD=E7=95=A5=E6=97=A5=E5=BF=97=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E3=80=81=E5=BE=85=E5=8F=91=E6=B6=88=E6=81=AF=E9=98=9F?= =?UTF-8?q?=E5=88=97=E6=96=87=E4=BB=B6=E5=92=8C=E4=BF=9D=E5=AD=98=E7=9A=84?= =?UTF-8?q?=E6=94=B6=E5=8F=91=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.gitignore b/.gitignore index 3e7a4a6..3ce23a9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,9 @@ +received_requests_* +received_data_* +sent_data_* +bot_config.json +pending_messages.json + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod]