From e8e3ea949f9354f70eda83930085a15002d1b6fa Mon Sep 17 00:00:00 2001 From: yshtcn Date: Fri, 22 Sep 2023 00:11:50 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BC=98=E5=8C=96=E6=9A=82=E5=81=9C?= =?UTF-8?q?=E6=97=B6=E9=97=B4=EF=BC=8C=E6=94=B9=E4=B8=BA=E5=8F=AA=E6=9C=89?= =?UTF-8?q?send=20message=E6=9A=82=E5=81=9C=EF=BC=8C=E5=87=8F=E5=B0=91?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E6=A6=82=E7=8E=87=E3=80=82=E5=A6=82=E6=9E=9C?= =?UTF-8?q?=E5=87=BA=E9=94=99=E5=A2=9E=E5=A4=A7=E6=9A=82=E5=81=9C=E5=88=B0?= =?UTF-8?q?10=E7=A7=92=E3=80=82=20-=20=E4=BC=98=E5=8C=96print=E6=8F=90?= =?UTF-8?q?=E7=A4=BA=E5=86=85=E5=AE=B9=20-=20=E6=B8=85=E7=90=86=E4=BA=86?= =?UTF-8?q?=E4=B8=80=E4=BA=9B=E8=BF=87=E6=9C=9F=E7=9A=84=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Diary Assistant.py | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/Diary Assistant.py b/Diary Assistant.py index 17e78c6..eb13932 100644 --- a/Diary Assistant.py +++ b/Diary Assistant.py @@ -1,6 +1,3 @@ -# 修改代码以记录黑名单而非删除消息 - -# 示例代码(请在本地环境中运行) import json import requests import time @@ -124,20 +121,26 @@ def process_message_queue(): # 遍历消息队列,尝试发送消息 remaining_messages = [] - for message in message_queue: + for message in message_queue: chat_id = message['chat_id'] text = message['text'] try: url = URL + "sendMessage" payload = {'chat_id': chat_id, 'text': text,'parse_mode': 'Markdown'} - r = requests.post(url, json=payload, proxies=proxies) + print(f"send to {url}:\n-----message text-----\n{text}\n-----message end-----") + r = requests.post(url, json=payload, proxies=proxies) if r.status_code == 200: + print("Message sent; nexttime in 2 seconds...") + time.sleep(2) continue else: remaining_messages.append(message) + print(f"send faild;append to remaining messages:{r.status_code}") except Exception as e: - print(f"Error sending message: {e}") + print(f"Error sending message,append to remaining messages: {e}") remaining_messages.append(message) + print(f"pauses for 10 seconds") + time.sleep(10) # 将更新后(或未成功发送的)消息队列保存回文件 with open('message_queue.json', 'w') as f: @@ -149,8 +152,9 @@ def process_message_queue(): def main(): print("Program started") # Debugging line last_update_id = None - while True: - + while True: + result = set_bot_commands() + print(f"set menu:{result}") print("Checking for updates...") # Debugging line updates = get_updates(last_update_id) if updates and "result" in updates: @@ -179,9 +183,7 @@ def main(): chat_id_str = str(chat_id) if unique_id not in blacklist: - # 处理“done”命令 - result = set_bot_commands() - print(f"set menu:{result}") + # 处理特殊命令 if message_text.lower() == "/start": send_message(chat_id_str, f"欢迎使日记助手,你可以直接开始发送要记录的内容.随时可以发送: \n/check 查看已记录的内容,并继续记录。\n /done 记录完毕,把记录发送给您,并开启新的记录。\n /removelast 删除最后一条信息。注意:直接编辑信息并不会修改错误的记录!") blacklist.append(unique_id) @@ -198,7 +200,6 @@ def main(): main_text="\n\n".join(user_data[chat_id_str]) send_message(chat_id_str, f"#查看记录\n以下是已记录的全部内容:\n\n```\n{main_text}\n```\n\n发送消息将继续当前的记录.") blacklist.append(unique_id) - # 新增:处理 "removelast" 命令 elif message_text.lower() == "/removelast": if user_data[chat_id_str]: user_data[chat_id_str].pop() @@ -210,7 +211,7 @@ def main(): if chat_id_str not in user_data: user_data[chat_id_str] = [] user_data[chat_id_str].append(message_text) - blacklist.append(unique_id) # 添加到黑名单 + blacklist.append(unique_id) @@ -227,18 +228,17 @@ def main(): with open('blacklist.json', 'w') as f: json.dump(blacklist, f) - print("Process message queue...") - process_message_queue() + + else: print(f"{URL} Received updates: {updates}") print("Error or no updates; retrying in 10 seconds...") time.sleep(10) # 等待5秒再重试 + print("Process message queue...") process_message_queue() - # 每30s检查一次 - print("Nexttime in 3 seconds...") - time.sleep(3) + # 运行主函数 if __name__ == '__main__':