Telegram Bot无法删除含Escrow Wallet的消息,求代码排查
问题排查与修复方案
1. 核心权限问题(最常见原因)
Bot必须拥有删除消息的权限才能执行操作:
- 将Bot添加为群组/频道的管理员
- 在管理员权限设置中,勾选删除消息权限,否则所有删除请求都会被Telegram服务器拒绝
2. 修复消息处理器逻辑
当前代码存在两个潜在问题:
- 未处理
message.text为空的情况(如图片、语音消息),会导致正则匹配报错 - 默认情况下,pyTelegramBotAPI可能无法捕获Bot自身发送的消息(需确保处理器覆盖所有消息来源)
修改删除消息的处理器代码:
@bot.message_handler(func=lambda message: message.text is not None and re.search(r'escrow wallet', message.text, re.IGNORECASE)) def delete_escrow_wallet_message(message): try: bot.delete_message(message.chat.id, message.message_id) except telebot.apihelper.ApiTelegramException as e: print(f"删除消息失败:{e.description}")
3. 解决Bot阻塞问题
你的auth函数中使用time.sleep(2*60)会完全阻塞Bot进程,导致2分钟内无法处理任何消息(包括删除请求)。改用线程实现延迟发送:
import threading def send_auth_message(chat_id): time.sleep(2 * 60) bot.send_message(chat_id, "💬 @log2, the funds have been deposited in the escrow wallet. You can go ahead and deliver the product(s) to the buyer. When @Audio is satisfied, he can paste /release into the chat or /contact in case of a dispute\n\n 💡 The seller can also /refund the buyer!") @bot.message_handler(commands=["auth"]) def auth(message): threading.Thread(target=send_auth_message, args=(message.chat.id,)).start()
4. 简化冗余代码
你代码中API_KEY变量未实际使用,可简化为:
API_KEY = os.getenv('API_KEY') bot = telebot.TeleBot(API_KEY)
完整修复后代码
import os import time import telebot import re import threading API_KEY = os.getenv('API_KEY') bot = telebot.TeleBot(API_KEY) def send_auth_message(chat_id): time.sleep(2 * 60) bot.send_message(chat_id, "💬 @log2, the funds have been deposited in the escrow wallet. You can go ahead and deliver the product(s) to the buyer. When @Audio is satisfied, he can paste /release into the chat or /contact in case of a dispute\n\n 💡 The seller can also /refund the buyer!") @bot.message_handler(commands=["auth"]) def auth(message): threading.Thread(target=send_auth_message, args=(message.chat.id,)).start() @bot.message_handler(commands=["balance"]) def balance(message): bot.send_message(message.chat.id, "📍 ESCROW WALLET\n\n💬 Wait for the balance to show up here, then continue with the deal. The funds will show up after 1 blockchain confirmation.\n\n💰 BALANCE: 0.00775835 BTC [$1730.94]") @bot.message_handler(func=lambda message: message.text is not None and re.search(r'escrow wallet', message.text, re.IGNORECASE)) def delete_escrow_wallet_message(message): try: bot.delete_message(message.chat.id, message.message_id) except telebot.apihelper.ApiTelegramException as e: print(f"删除失败:{e}") # 启动Bot,确保监听所有更新类型 bot.polling(none_stop=True, allowed_updates=telebot.util.update_types)
内容的提问来源于stack exchange,提问作者yxngkxng 480
相关产品推荐
相关产品推荐

