Skip to content

Commit 9739772

Browse files
committed
fix(telegram): guard downstream callback answer after early success
The pre-sequentialize middleware now records when answerCallbackQuery succeeds. The downstream handler skips its own acknowledgement when that flag is set, preventing duplicate Bot API calls. Addresses ClawSweeper P2 review: skip the fallback answer after early success.
1 parent d03ac90 commit 9739772

2 files changed

Lines changed: 17 additions & 11 deletions

File tree

‎extensions/telegram/src/bot-core.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,11 @@ export function createTelegramBotCore(
224224
const answer = typeof (ctx as { answerCallbackQuery?: unknown }).answerCallbackQuery === "function"
225225
? () => ctx.answerCallbackQuery()
226226
: () => bot.api.answerCallbackQuery(ctx.callbackQuery.id);
227-
await answer().catch(() => {});
227+
await answer()
228+
.then(() => {
229+
(ctx as Record<string, unknown>).callbackQueryAnswered = true;
230+
})
231+
.catch(() => {});
228232
}
229233
return next();
230234
});

‎extensions/telegram/src/bot-handlers.runtime.ts‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,16 +1957,18 @@ export const registerTelegramHandlers = ({
19571957
if (shouldSkipUpdate(ctx)) {
19581958
return;
19591959
}
1960-
const answerCallbackQuery =
1961-
typeof (ctx as { answerCallbackQuery?: unknown }).answerCallbackQuery === "function"
1962-
? () => ctx.answerCallbackQuery()
1963-
: () => bot.api.answerCallbackQuery(callback.id);
1964-
// Answer immediately to prevent Telegram from retrying while we process
1965-
await withTelegramApiErrorLogging({
1966-
operation: "answerCallbackQuery",
1967-
runtime,
1968-
fn: answerCallbackQuery,
1969-
}).catch(() => {});
1960+
if (!(ctx as Record<string, unknown>).callbackQueryAnswered) {
1961+
const answerCallbackQuery =
1962+
typeof (ctx as { answerCallbackQuery?: unknown }).answerCallbackQuery === "function"
1963+
? () => ctx.answerCallbackQuery()
1964+
: () => bot.api.answerCallbackQuery(callback.id);
1965+
// Answer immediately to prevent Telegram from retrying while we process
1966+
await withTelegramApiErrorLogging({
1967+
operation: "answerCallbackQuery",
1968+
runtime,
1969+
fn: answerCallbackQuery,
1970+
}).catch(() => {});
1971+
}
19701972
try {
19711973
const data = (callback.data ?? "").trim();
19721974
const callbackMessage = callback.message;

0 commit comments

Comments
 (0)