mirror of
https://github.com/yshtcn/ServerChanPush2TelegramBot.git
synced 2025-12-14 02:00:26 +08:00
commit
5b31b3a6b1
6
.gitignore
vendored
6
.gitignore
vendored
@ -1,3 +1,9 @@
|
|||||||
|
received_requests_*
|
||||||
|
received_data_*
|
||||||
|
sent_data_*
|
||||||
|
bot_config.json
|
||||||
|
pending_messages.json
|
||||||
|
|
||||||
# Byte-compiled / optimized / DLL files
|
# Byte-compiled / optimized / DLL files
|
||||||
__pycache__/
|
__pycache__/
|
||||||
*.py[cod]
|
*.py[cod]
|
||||||
|
|||||||
6
Post_Test.bat
Normal file
6
Post_Test.bat
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
title 测试post
|
||||||
|
:start
|
||||||
|
@cd /d %~dp0
|
||||||
|
py Post_Test.py
|
||||||
|
pause
|
||||||
|
goto start
|
||||||
31
Post_Test.py
Normal file
31
Post_Test.py
Normal file
@ -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()
|
||||||
@ -83,6 +83,7 @@ def send_telegram_message(bot_id, chat_id, title, desp=None, url=None):
|
|||||||
all_bots_config = load_config()
|
all_bots_config = load_config()
|
||||||
# 用于标记是否找到匹配的关键词
|
# 用于标记是否找到匹配的关键词
|
||||||
found = False
|
found = False
|
||||||
|
delimiter = None
|
||||||
# 遍历所有主 bot_id 的配置
|
# 遍历所有主 bot_id 的配置
|
||||||
for config in all_bots_config:
|
for config in all_bots_config:
|
||||||
main_bot_id = config['main_bot_id']
|
main_bot_id = config['main_bot_id']
|
||||||
@ -97,7 +98,8 @@ 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
|
title_decode = title.decode('utf-8') if isinstance(title, bytes) else title
|
||||||
if keyword_decode.lower() in title_decode.lower():
|
if keyword_decode.lower() in title_decode.lower():
|
||||||
bot_id = sub_bot['bot_id']
|
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
|
found = True
|
||||||
break
|
break
|
||||||
if found:
|
if found:
|
||||||
@ -108,6 +110,7 @@ def send_telegram_message(bot_id, chat_id, title, desp=None, url=None):
|
|||||||
break
|
break
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
api_url = f"https://api.telegram.org/bot{bot_id}/sendMessage"
|
api_url = f"https://api.telegram.org/bot{bot_id}/sendMessage"
|
||||||
proxies = {
|
proxies = {
|
||||||
'http': 'http://127.0.0.1:7890',
|
'http': 'http://127.0.0.1:7890',
|
||||||
@ -115,8 +118,8 @@ def send_telegram_message(bot_id, chat_id, title, desp=None, url=None):
|
|||||||
}
|
}
|
||||||
text = title # 初始化 text 为 title
|
text = title # 初始化 text 为 title
|
||||||
|
|
||||||
if desp: # 如果有 desp,添加到 text
|
text += f"\n\n{(desp.split(delimiter)[0] if delimiter and desp else desp) if desp else ''}"
|
||||||
text += f"\n\n{desp}"
|
|
||||||
|
|
||||||
|
|
||||||
# 使用正则表达式来识别受影响的链接
|
# 使用正则表达式来识别受影响的链接
|
||||||
@ -128,7 +131,7 @@ def send_telegram_message(bot_id, chat_id, title, desp=None, url=None):
|
|||||||
text = text.replace(affected_url, corrected_url)
|
text = text.replace(affected_url, corrected_url)
|
||||||
|
|
||||||
if url: # 如果有 url,添加到 text
|
if url: # 如果有 url,添加到 text
|
||||||
text += f"\n\n<a href=\"{url}\">详情:</a>"
|
text += f"\n\n[详情]({url})"
|
||||||
text += f"{url}" # 直接添加 URL,Telegram 会自动处理预览
|
text += f"{url}" # 直接添加 URL,Telegram 会自动处理预览
|
||||||
|
|
||||||
text=unescape_url(text)
|
text=unescape_url(text)
|
||||||
@ -137,7 +140,7 @@ def send_telegram_message(bot_id, chat_id, title, desp=None, url=None):
|
|||||||
payload = {
|
payload = {
|
||||||
'chat_id': chat_id,
|
'chat_id': chat_id,
|
||||||
'text': text,
|
'text': text,
|
||||||
'parse_mode': 'HTML',
|
'parse_mode': 'markdown',
|
||||||
'disable_web_page_preview': False # 启用网页预览
|
'disable_web_page_preview': False # 启用网页预览
|
||||||
}
|
}
|
||||||
try:
|
try:
|
||||||
|
|||||||
20
bot_config_example.json
Normal file
20
bot_config_example.json
Normal file
@ -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"]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
Loading…
x
Reference in New Issue
Block a user