Telegram Bot API调用报错400:Chat Not Found问题排查求助
Telegram Bot 400错误:Bad Request: chat not found 排查
错误信息
raise ApiTelegramException(method_name, result, result_json) telebot.apihelper.ApiTelegramException: A request to the Telegram API was unsuccessful. Error code: 400. Description: Bad Request: chat not found
问题场景
用户点击“👍 Like”按钮后,代码需将点赞记录存入数据库,并向被点赞用户发送消息,但触发上述400错误。
相关代码
点赞按钮生成代码
if user: name, age, gender, photo_id = user markup = types.InlineKeyboardMarkup() markup.add(types.InlineKeyboardButton("👍 Like", callback_data=f"like_{random_user_id}"))
回调处理函数
def handle_like_callback(call): user_id = call.from_user.id liked_user_id = int(call.data.split('_')[1]) with conn: cursor = conn.cursor() cursor.execute("INSERT INTO likes (liker_id, liked_id) VALUES (?, ?)", (user_id, liked_user_id)) cursor.execute("SELECT telegram_id FROM users WHERE telegram_id = ?", (liked_user_id,)) liked_user = cursor.fetchone() if liked_user: liked_user_telegram_id = liked_user[0] cursor.execute("SELECT name, age, gender, photo_id FROM users WHERE telegram_id = ?", (user_id,)) user = cursor.fetchone() if user: name, age, gender, photo_id = user markup = types.InlineKeyboardMarkup() markup.add(types.InlineKeyboardButton("Reply", url=f"tg://user?id={user_id}")) if liked_user_telegram_id: bot.send_photo(liked_user_telegram_id, photo_id, caption=f"Name: {name}\nAge: {age}\nGender: {gender}", reply_markup=markup) else: bot.send_message(....)
已尝试修改
if liked_user_telegram_id: bot.send_photo(liked_user_telegram_id, photo_id, caption=f"Name: {name}\nAge: {age}\nGender: {gender}", reply_markup=markup)
错误原因分析
- 数据库连接生命周期问题:
with conn:块执行完毕后连接会自动关闭,后续cursor.execute使用已关闭的连接,可能导致查询结果异常(如liked_user为空或拿到错误的Telegram ID)。 - 查询条件错误:如果
random_user_id是users表的主键ID(而非Telegram用户ID),SELECT telegram_id FROM users WHERE telegram_id = ?的查询条件完全错误,无法获取正确的被点赞用户Telegram ID。 - Telegram API限制:即使拿到正确的Telegram ID,若用户未主动与Bot发起对话、或已屏蔽Bot,Bot无法向该用户发送消息,同样会触发
chat not found错误。
解决方案
1. 统一数据库操作的连接生命周期
将所有数据库查询放入with conn:块内,确保连接有效:
def handle_like_callback(call): user_id = call.from_user.id liked_user_id = int(call.data.split('_')[1]) with conn: cursor = conn.cursor() # 插入点赞记录 cursor.execute("INSERT INTO likes (liker_id, liked_id) VALUES (?, ?)", (user_id, liked_user_id)) # 修正查询条件:如果liked_user_id是users表主键,改为WHERE id = ? cursor.execute("SELECT telegram_id FROM users WHERE id = ?", (liked_user_id,)) liked_user = cursor.fetchone() if liked_user: liked_user_telegram_id = liked_user[0] cursor.execute("SELECT name, age, gender, photo_id FROM users WHERE telegram_id = ?", (user_id,)) user = cursor.fetchone() if user: name, age, gender, photo_id = user markup = types.InlineKeyboardMarkup() markup.add(types.InlineKeyboardButton("Reply", url=f"tg://user?id={user_id}")) if liked_user_telegram_id: try: bot.send_photo(liked_user_telegram_id, photo_id, caption=f"Name: {name}\nAge: {age}\nGender: {gender}", reply_markup=markup) except telebot.apihelper.ApiTelegramException as e: # 处理无法发送消息的情况 bot.send_message(user_id, "无法向对方发送消息,可能对方未开启与Bot的对话或已屏蔽Bot") else: bot.send_message(user_id, "未找到被点赞用户的有效Telegram ID")
2. 确认random_user_id的含义
- 如果
random_user_id是users表主键ID,修改查询条件为WHERE id = ?; - 如果是Telegram用户ID,确保数据库
telegram_id字段为整数类型,存入数据格式正确。
3. 增加API调用异常处理
在发送消息代码外层添加try-except块,捕获Telegram API异常,避免程序崩溃并给用户明确反馈。
内容的提问来源于stack exchange,提问作者Denys Alexeev
相关产品推荐
相关产品推荐

