mirror of
https://github.com/yshtcn/ServerChanPush2TelegramBot.git
synced 2025-12-13 17:50:22 +08:00
增加base64的健壮性(部分为空的判断)
This commit is contained in:
parent
1a3c42ac1a
commit
180b06082b
@ -99,7 +99,10 @@ def read_pending_messages():
|
|||||||
encoded_messages = json.load(f)
|
encoded_messages = json.load(f)
|
||||||
decoded_messages = []
|
decoded_messages = []
|
||||||
for msg in encoded_messages:
|
for msg in encoded_messages:
|
||||||
decoded_msg = {key: base64.b64decode(value).decode('utf-8') if key in ['bot_id', 'chat_id', 'title', 'desp', 'url'] else value for key, value in msg.items()}
|
decoded_msg = {
|
||||||
|
key: base64.b64decode(value).decode('utf-8') if value is not None and key in ['bot_id', 'chat_id', 'title', 'desp', 'url'] else value
|
||||||
|
for key, value in msg.items()
|
||||||
|
}
|
||||||
decoded_messages.append(decoded_msg)
|
decoded_messages.append(decoded_msg)
|
||||||
return decoded_messages
|
return decoded_messages
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
@ -107,15 +110,20 @@ def read_pending_messages():
|
|||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
||||||
# 写入待发送的消息
|
# 写入待发送的消息
|
||||||
def write_pending_messages(messages):
|
def write_pending_messages(messages):
|
||||||
encoded_messages = []
|
encoded_messages = []
|
||||||
for msg in messages:
|
for msg in messages:
|
||||||
encoded_msg = {key: base64.b64encode(value.encode('utf-8')).decode('utf-8') if key in ['bot_id', 'chat_id', 'title', 'desp', 'url'] else value for key, value in msg.items()}
|
encoded_msg = {
|
||||||
|
key: base64.b64encode(value.encode('utf-8')).decode('utf-8') if value is not None and key in ['bot_id', 'chat_id', 'title', 'desp', 'url'] else value
|
||||||
|
for key, value in msg.items()
|
||||||
|
}
|
||||||
encoded_messages.append(encoded_msg)
|
encoded_messages.append(encoded_msg)
|
||||||
with open(os.path.join(data_dir, "pending_messages.json"), "w") as f:
|
with open(os.path.join(data_dir, "pending_messages.json"), "w") as f:
|
||||||
json.dump(encoded_messages, f, ensure_ascii=False)
|
json.dump(encoded_messages, f, ensure_ascii=False)
|
||||||
|
|
||||||
|
|
||||||
# 发送 Telegram 消息
|
# 发送 Telegram 消息
|
||||||
def send_telegram_message(bot_id, chat_id, title, desp=None, url=None):
|
def send_telegram_message(bot_id, chat_id, title, desp=None, url=None):
|
||||||
# 重新读取配置文件
|
# 重新读取配置文件
|
||||||
@ -234,6 +242,8 @@ def index():
|
|||||||
elif TestStatus == '2':
|
elif TestStatus == '2':
|
||||||
return jsonify({"ok": "the test passed", "pending_messages_count": pending_count}), 200
|
return jsonify({"ok": "the test passed", "pending_messages_count": pending_count}), 200
|
||||||
elif TestStatus == '3':
|
elif TestStatus == '3':
|
||||||
|
pending_messages = read_pending_messages()
|
||||||
|
pending_count = len(pending_messages)
|
||||||
if pending_messages:
|
if pending_messages:
|
||||||
new_pending_messages = []
|
new_pending_messages = []
|
||||||
for msg in pending_messages:
|
for msg in pending_messages:
|
||||||
@ -241,9 +251,11 @@ def index():
|
|||||||
if not success:
|
if not success:
|
||||||
new_pending_messages.append(msg)
|
new_pending_messages.append(msg)
|
||||||
write_pending_messages(new_pending_messages)
|
write_pending_messages(new_pending_messages)
|
||||||
return jsonify({"ok": "re-sent pending messages", "remaining_pending_messages_count": len(new_pending_messages)}), 200
|
return jsonify({"ok": "re-sent pending messages", "pending_messages_count": pending_count, "remaining_pending_messages_count": len(new_pending_messages)}), 200
|
||||||
else:
|
else:
|
||||||
return jsonify({"ok": "no pending messages to re-send"}), 200
|
return jsonify({"ok": "no pending messages to re-send", "pending_messages_count": pending_count}), 200
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# 原始的消息发送逻辑
|
# 原始的消息发送逻辑
|
||||||
pending_messages = read_pending_messages()
|
pending_messages = read_pending_messages()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user