fix: handle 400 errors from Telegram callback interactions
- Remove redundant editMessageText in CALLBACK_REPLY (text unchanged) - Wrap answerCallbackQuery in try/except (non-critical) - Wrap each update in its own try/except to ensure last_update_id always advances, preventing infinite retry loops
This commit is contained in:
@@ -61,8 +61,12 @@ def poll_telegram(tg_cfg: TelegramConfig, cfg: Config, last_update_id: int) -> i
|
||||
if not data.get("ok"):
|
||||
return last_update_id
|
||||
for update in data.get("result", []):
|
||||
_handle_update(tg_cfg, cfg, update)
|
||||
last_update_id = update["update_id"] + 1
|
||||
uid = update["update_id"]
|
||||
try:
|
||||
_handle_update(tg_cfg, cfg, update)
|
||||
except Exception as e:
|
||||
logger.warning("处理 update %d 失败: %s", uid, e)
|
||||
last_update_id = uid + 1
|
||||
except Exception as e:
|
||||
logger.warning("Telegram polling error: %s", e)
|
||||
return last_update_id
|
||||
@@ -181,7 +185,10 @@ def _handle_callback(tg_cfg: TelegramConfig, cfg: Config, cb: dict):
|
||||
data = cb["data"]
|
||||
cb_id = cb["id"]
|
||||
|
||||
_tg_req(tg_cfg, "answerCallbackQuery", {"callback_query_id": cb_id})
|
||||
try:
|
||||
_tg_req(tg_cfg, "answerCallbackQuery", {"callback_query_id": cb_id})
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
parts = data.split("|")
|
||||
action = parts[0]
|
||||
@@ -207,12 +214,6 @@ def _handle_callback(tg_cfg: TelegramConfig, cfg: Config, cb: dict):
|
||||
|
||||
elif action == CALLBACK_REPLY and ctx:
|
||||
_conversations[chat_id] = {"state": "awaiting_reply", "msg_id": msg_id}
|
||||
can_reply = ctx.get("can_reply", True)
|
||||
_tg_req(tg_cfg, "editMessageText", {
|
||||
"chat_id": chat_id, "message_id": msg_id,
|
||||
"text": ctx["summary_text"], "parse_mode": "MarkdownV2",
|
||||
"reply_markup": _summary_keyboard(can_reply),
|
||||
})
|
||||
send_text(tg_cfg, str(chat_id), "请输入你的回复内容:")
|
||||
|
||||
elif action == CALLBACK_AI_REPLY and ctx:
|
||||
|
||||
Reference in New Issue
Block a user