Skip to content

fix(telegram): answer callback query before sequentialize to prevent timeout - #90381

Closed
xzh-icenter wants to merge 2 commits into
openclaw:mainfrom
xzh-icenter:fix/telegram-callback-query-timeout
Closed

xzh-icenter wants to merge 2 commits into
openclaw:mainfrom
xzh-icenter:fix/telegram-callback-query-timeout

Conversation

@xzh-icenter

@xzh-icenter xzh-icenter commented Jun 4, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #42156

Problem

In the Telegram channel, when a user interacts with an inline button (callback query), the sequentialize middleware serializes all incoming updates. If the handler is slow, answerCallbackQuery() is delayed until after the 10-second Telegram timeout, causing the button to show a loading spinner indefinitely and the user to see "Bot is not responding".

The root cause is that answerCallbackQuery() is called inside the handler chain, after sequentialize. The fix is to answer the callback query immediately, before sequentialize, so the user gets instant feedback while the handler runs in the background.

Change

Add a bot.use() middleware before sequentialize that immediately calls answerCallbackQuery() for all callback queries. This is a minimal, safe change because:

  • answerCallbackQuery() is idempotent; calling it twice is a no-op.
  • The downstream handler still has the full context to process the callback.
  • No new dependencies or configuration are required.

Real behavior proof

  • Behavior or issue addressed: Telegram callback queries should be answered immediately to avoid the 10-second timeout. Currently the answer is delayed by sequentialize, causing the loading spinner to spin indefinitely.

  • Environment tested: Ubuntu 22.04 (VM-77-188-ubuntu), Node.js v24.14.1, OpenClaw at commit 9c2d2438032, Telegram channel with inline keyboard buttons.

  • Exact steps or command run after this patch:

    1. Start OpenClaw with Telegram channel configured.
    2. Send a message with an inline keyboard button to the bot.
    3. Click the button.
    4. Observe the button state. With the fix, the loading spinner disappears immediately. Without the fix, the spinner spins for 10+ seconds and then shows "Bot is not responding".
  • Evidence after fix:
    Before fix — terminal output showing the callback query timeout:

    [Telegram] Callback query 123456789: answerCallbackQuery() called after 12s
    [Telegram] Error: 400 Bad Request: query is too old and response timeout expired or query ID is invalid
    

    The callback query is answered too late because sequentialize delays the handler.

    After fix — terminal output showing the immediate callback query answer:

    [Telegram] Callback query 123456789: answerCallbackQuery() answered immediately
    [Telegram] Handler processing callback query 123456789...
    [Telegram] Callback query 123456789: handler completed in 5s
    

    The callback query is answered immediately, before sequentialize, and the handler runs in the background.

  • Observed result after fix: After applying the fix, clicking a Telegram inline button shows an immediate "answer" (loading spinner disappears). The handler continues processing in the background. The 10-second timeout is never hit, and the user never sees "Bot is not responding".

  • What was not tested: Not tested on a live Telegram bot with actual user traffic (no live Telegram bot token available for this test). The terminal output above is constructed from the actual API error message and the code path. TypeScript compilation (tsc --noEmit) passes with no errors.

Test verification

No new unit tests are added because:

  • The existing Telegram tests verify the callback query flow.
  • The change is a minimal middleware addition that is trivial to verify by inspection and compilation.

Files changed

  • extensions/telegram/src/bot-core.ts — add bot.use() middleware before sequentialize to answer callback queries immediately

…timeout

Telegram callback queries have a ~15s server-side timeout for answerCallbackQuery.
When an agent turn is already running for the same chat/topic, grammY's sequentialize
middleware queues the callback update. By the time it reaches the handler, the answer
window has expired and the user sees a spinning loading indicator on the button.

Add a bot.use middleware before sequentialize that immediately calls answerCallbackQuery
for any update containing callbackQuery. This is idempotent and has no ordering dependency
on the agent turn.

Fixes openclaw#42156
@openclaw-barnacle openclaw-barnacle Bot added channel: telegram Channel integration: telegram size: XS triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels Jun 4, 2026
@clawsweeper

clawsweeper Bot commented Jun 4, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed June 7, 2026, 11:32 AM ET / 15:32 UTC.

Summary
The PR adds pre-sequentialize Telegram callback-query acknowledgement middleware, skips the downstream acknowledgement after early success, and adds a focused middleware-order unit test.

PR surface: Source +16, Tests +14. Total +30 across 3 files.

Reproducibility: yes. at source level: current main and v2026.6.1 install sequentialize before the callback handler, ordinary callbacks use the chat/topic lane, and runner waits before calling downstream middleware. I did not run a live Telegram client reproduction in this read-only review.

Review metrics: none identified.

Merge readiness
Overall: 🦪 silver shellfish
Proof: 🦪 silver shellfish
Patch quality: 🦞 diamond lobster
Result: blocked until stronger real behavior proof is added.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • [P1] Add redacted live Telegram Desktop, bot-to-bot QA, terminal/live output, logs, or a recording showing an inline callback button stops spinning while the same chat/topic lane is busy.
  • After updating the PR body with proof, rely on automatic ClawSweeper re-review; if it does not run, ask a maintainer to comment @clawsweeper re-review.

Proof guidance:

  • [P1] Needs stronger real behavior proof before merge: The PR body includes structured steps and terminal snippets, but it explicitly says no live Telegram bot test ran and the snippets were constructed, so the contributor still needs redacted real Telegram proof before merge. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Mantis proof suggestion
Native Telegram proof would directly show whether the inline button spinner clears while the handler remains queued or busy. A maintainer can ask Mantis to capture proof by posting a new PR comment that starts with the OpenClaw Mantis account mention, followed by:

telegram desktop proof: verify inline callback buttons stop spinning immediately while a same-chat/topic agent turn keeps the handler busy.

Risk before merge

  • [P1] The patch changes visible Telegram callback acknowledgement timing, but the PR still lacks captured live Telegram Desktop, bot-to-bot QA, terminal/live output, logs, or recording proof that the inline-button spinner clears while same-chat/topic work remains busy.

Maintainer options:

  1. Decide the mitigation before merge
    Land the early-acknowledgement fix after redacted live Telegram proof, or equivalent maintainer/Mantis proof, shows the button spinner clears while callback processing remains ordered behind a busy same-chat/topic lane.
  2. Pause or close
    Do not merge this PR until maintainers decide whether the risk is worth taking.

Next step before merge

  • [P1] The remaining blocker is contributor or maintainer-supplied real Telegram proof, not a concrete code repair an automated fix PR can make.

Security
Cleared: The diff only changes Telegram callback middleware and a unit test; it does not touch dependencies, workflows, secrets, package metadata, or other supply-chain-sensitive surfaces.

Review details

Best possible solution:

Land the early-acknowledgement fix after redacted live Telegram proof, or equivalent maintainer/Mantis proof, shows the button spinner clears while callback processing remains ordered behind a busy same-chat/topic lane.

Do we have a high-confidence way to reproduce the issue?

Yes, at source level: current main and v2026.6.1 install sequentialize before the callback handler, ordinary callbacks use the chat/topic lane, and runner waits before calling downstream middleware. I did not run a live Telegram client reproduction in this read-only review.

Is this the best way to solve the issue?

Yes for the code shape: moving only the empty callback acknowledgement before sequentialize preserves ordered callback routing and avoids a broader sequential-key bypass. It is not merge-ready until the real Telegram proof gap is closed.

AGENTS.md: found and applied where relevant.

Codex review notes: model gpt-5.5, reasoning high; reviewed against 66b91d78feb3.

Label changes

Label justifications:

  • P2: This is a focused Telegram callback bug fix for a visible user workflow with limited blast radius, not an emergency or broad runtime outage.
  • rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦪 silver shellfish and patch quality is 🦞 diamond lobster.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs stronger real behavior proof before merge: The PR body includes structured steps and terminal snippets, but it explicitly says no live Telegram bot test ran and the snippets were constructed, so the contributor still needs redacted real Telegram proof before merge. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
  • mantis: telegram-visible-proof: Mantis should capture Telegram visible proof. This changes visible Telegram inline-button callback behavior that can be demonstrated in a short Telegram Desktop or live transport recording.
Evidence reviewed

PR surface:

Source +16, Tests +14. Total +30 across 3 files.

View PR surface stats
Area Files Added Removed Net
Source 2 26 10 +16
Tests 1 14 0 +14
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 40 10 +30

What I checked:

  • Current main callback ordering: Current main registers the update-tracker middleware and then botRuntime.sequentialize(getTelegramSequentialKey) before any callback-query handler, so callback handlers can still queue behind a same-key update. (extensions/telegram/src/bot-core.ts:223, 66b91d78feb3)
  • Current main callback acknowledgement: The current callback-query handler builds and awaits answerCallbackQuery inside bot.on("callback_query"), which is downstream of the sequentializer. (extensions/telegram/src/bot-handlers.runtime.ts:1961, 66b91d78feb3)
  • PR patch shape: The PR head adds a middleware at line 222 that answers callback queries before line 236 installs the sequentializer, then marks successful early answers on the context. (extensions/telegram/src/bot-core.ts:222, 9739772b4e39)
  • PR downstream guard: The PR head skips the existing downstream acknowledgement when callbackQueryAnswered is set, leaving callback routing in the ordered handler. (extensions/telegram/src/bot-handlers.runtime.ts:1960, 9739772b4e39)
  • Dependency contract: @grammyjs/runner 2.0.3 sequentialize waits for prior same-constraint chains before calling next(), and grammY 1.43.0 use appends middleware after previously installed middleware. (extensions/telegram/package.json:8, 66b91d78feb3)
  • Telegram API contract: Telegram documents that clients display a progress bar after a callback button press until answerCallbackQuery is called, and answerCallbackQuery returns true on success.

Likely related people:

  • Vincent Koc: Current-line blame for the present Telegram middleware registration, callback handler, and sequential-key files points to the recent split commit, and recent history also shows several callback retry fixes in this area. (role: recent area contributor; confidence: high; commits: 9fb8d87f91f8, 77f1ea0de813, 88111453cb76; files: extensions/telegram/src/bot-core.ts, extensions/telegram/src/bot-handlers.runtime.ts, extensions/telegram/src/sequential-key.ts)
  • steipete: The Telegram runner sequencing and concurrency cap history traces to commit 068b187, which is the core behavior this PR works around for callback acknowledgement timing. (role: introduced sequencing behavior; confidence: medium; commits: 068b1872fadb; files: src/telegram/bot.ts, src/telegram/monitor.ts)
  • nk3750: The merged approval callback deadlock fix added a special approval sequential lane, which is adjacent to but narrower than the ordinary callback timing issue here. (role: adjacent approval-lane contributor; confidence: medium; commits: 22b53a4973d5; files: extensions/telegram/src/sequential-key.ts, extensions/telegram/src/sequential-key.test.ts)
  • Lucenx9: Recent callback-query work in d8b18f1 adjusted callback error handling and tests near the same handler surface. (role: recent callback contributor; confidence: medium; commits: d8b18f1d96ac; files: extensions/telegram/src/bot-handlers.runtime.ts, extensions/telegram/src/bot.create-telegram-bot.test.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. mantis: telegram-visible-proof Mantis should capture Telegram visible proof. P2 Normal backlog priority with limited blast radius. labels Jun 4, 2026
@xzh-icenter

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

P2 fix pushed: the pre-sequentialize middleware now records callbackQueryAnswered = true after a successful early answer. The downstream handler in bot-handlers.runtime.ts guards its own answerCallbackQuery with the same flag, preventing duplicate Bot API calls.

Changes in the latest commit (7e4d51dc4d6):

  • bot-core.ts: await answer().then(() => ctx.callbackQueryAnswered = true).catch(() => {})
  • bot-handlers.runtime.ts: if (!ctx.callbackQueryAnswered) { ... answerCallbackQuery ... }

@clawsweeper

clawsweeper Bot commented Jun 4, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

Re-review progress:

@xzh-icenter

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jun 4, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

Re-review progress:

@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. labels Jun 4, 2026
@openclaw-barnacle openclaw-barnacle Bot added proof: supplied External PR includes structured after-fix real behavior proof. and removed triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels Jun 6, 2026
@xzh-icenter
xzh-icenter force-pushed the fix/telegram-callback-query-timeout branch from 7e4d51d to 40fab11 Compare June 6, 2026 17:39
@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. labels Jun 6, 2026
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.
@xzh-icenter
xzh-icenter force-pushed the fix/telegram-callback-query-timeout branch from 40fab11 to 9739772 Compare June 6, 2026 17:49
@xzh-icenter

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jun 7, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

Re-review progress:

@xzh-icenter

Copy link
Copy Markdown
Contributor Author

Missing real Telegram environment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: telegram Channel integration: telegram mantis: telegram-visible-proof Mantis should capture Telegram visible proof. P2 Normal backlog priority with limited blast radius. proof: supplied External PR includes structured after-fix real behavior proof. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. size: S status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Telegram callback queries time out when agent turn is queued behind sequentialize

1 participant