From 020fecef5c96e68670a416eaef2b6ab354696702 Mon Sep 17 00:00:00 2001 From: Siddhant Jain Date: Wed, 21 Jan 2026 03:04:28 +0000 Subject: [PATCH] fix(telegram): answer callback queries immediately to prevent retries Telegram retries callback queries if they aren't acknowledged quickly. Previously, answerCallbackQuery was called in a finally block AFTER processing, which could take several seconds for agent responses. This change moves answerCallbackQuery to immediately after basic validation, before any processing begins. This prevents Telegram from sending duplicate callbacks while the agent is thinking. Fixes duplicate callback handling when agent processing is slow. --- src/telegram/bot-handlers.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/telegram/bot-handlers.ts b/src/telegram/bot-handlers.ts index a17a65b3e..8dfcc5ac1 100644 --- a/src/telegram/bot-handlers.ts +++ b/src/telegram/bot-handlers.ts @@ -179,6 +179,8 @@ export const registerTelegramHandlers = ({ const callback = ctx.callbackQuery; if (!callback) return; if (shouldSkipUpdate(ctx)) return; + // Answer immediately to prevent Telegram from retrying while we process + await bot.api.answerCallbackQuery(callback.id).catch(() => {}); try { const data = (callback.data ?? "").trim(); const callbackMessage = callback.message; @@ -323,8 +325,6 @@ export const registerTelegramHandlers = ({ }); } catch (err) { runtime.error?.(danger(`callback handler failed: ${String(err)}`)); - } finally { - await bot.api.answerCallbackQuery(callback.id).catch(() => {}); } });