Skip to content

fix(telegram): skip empty messages instead of throwing error - #37294

Closed
yelog wants to merge 2 commits into
openclaw:mainfrom
yelog:fix/telegram-empty-reply-37278
Closed

yelog wants to merge 2 commits into
openclaw:mainfrom
yelog:fix/telegram-empty-reply-37278

Conversation

@yelog

@yelog yelog commented Mar 6, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Problem: Telegram channel crashes when agent emits empty reply (GrammyError 400: text must be non-empty)
  • Why it matters: Gateway restarts on each occurrence, causing disruption to users
  • What changed: Silently skip empty messages instead of throwing error
  • What did NOT change: Normal message delivery behavior

Change Type

  • Bug fix

Scope

  • Gateway / orchestration

Linked Issue

User-visible / Behavior Changes

Empty agent replies (e.g., NO_REPLY, heartbeat ack) are now silently skipped instead of causing gateway crashes.

Security Impact

  • New permissions/capabilities? (No)
  • Secrets/tokens handling changed? (No)
  • New/changed network calls? (No)
  • Command/tool execution surface changed? (No)
  • Data access scope changed? (No)

Repro + Verification

Steps

  1. Configure Telegram channel with heartbeat enabled
  2. Trigger an empty reply scenario
  3. Observe gateway no longer crashes

Expected

  • Empty messages are skipped silently
  • No Telegram API 400 errors
  • Gateway remains stable

Actual

  • Verified with unit tests: skips when formatted and plain fallback text are both empty

Evidence

  • ✅ Updated test passes
  • ✅ All 26 delivery tests pass

Human Verification

  • Verified scenarios: Empty text, whitespace-only text, normal text delivery
  • Edge cases checked: Threaded mode, plain text fallback
  • What you did not verify: Integration with real Telegram API

Compatibility / Migration

  • Backward compatible? (Yes)
  • Config/env changes? (No)
  • Migration needed? (No)

Failure Recovery

  • How to disable/revert this change quickly: Revert commit
  • Files/config to restore: None
  • Known bad symptoms reviewers should watch for: Missing legitimate messages

Risks and Mitigations

  • Risk: Legitimate empty responses might be silently dropped
    • Mitigation: This matches expected behavior for NO_REPLY and heartbeat responses

@openclaw-barnacle openclaw-barnacle Bot added channel: telegram Channel integration: telegram size: XS labels Mar 6, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b2da493c82

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

throw new Error("telegram sendMessage failed: empty formatted text and empty plain fallback");
// Silently skip empty messages to avoid Telegram API 400 error
runtime.log?.("telegram sendMessage skipped: empty content");
return 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid reporting skipped empty Telegram sends as delivered

Returning 0 here makes downstream code treat a skipped message as a successful delivery: deliverTextReply in src/telegram/bot/delivery.replies.ts stores this numeric ID and calls markDelivered, and pinning logic later attempts pinChatMessage with message ID 0 when pin is enabled. In practice, whitespace-only replies are now counted as sent (message_sent success becomes true) even though nothing was sent, and can trigger an invalid pin API call; skip should be represented as a non-delivery state instead of a fake numeric message ID.

Useful? React with 👍 / 👎.

@greptile-apps

greptile-apps Bot commented Mar 6, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a gateway crash caused by Telegram's 400 "text must be non-empty" error when agents emit empty replies (e.g. NO_REPLY, heartbeat ack). Instead of throwing, sendTelegramText now silently returns 0 and logs a message.

Issue: The return 0 sentinel is not handled by callers in delivery.replies.ts. Both deliverTextReply (line 143) and sendPendingFollowUpText (line 178) call markDelivered() unconditionally after every sendTelegramText invocation, regardless of the return value. This causes:

  • progress.deliveredCount to be incremented for skipped messages
  • deliverReplies to return { delivered: true } for replies that were fully skipped
  • message_sent hooks to fire with success: true for skipped messages
  • firstDeliveredMessageId to be set to 0, which could cause invalid API calls if pinning is requested

The test verifies that no API call is made and the log message appears, but does not assert that delivery state remains unaffected.

Confidence Score: 3/5

  • The crash fix is sound, but the implementation has an unhandled side-effect where delivery state is incorrectly marked as successful for skipped messages.
  • The change correctly prevents the GrammyError 400 gateway crash. However, the chosen implementation (returning 0 as a skip signal) is not properly handled by callers. This silently corrupts delivery-progress state and can cause downstream side effects (incorrect hook signals with success: true, wrong delivered return value, potential invalid pinning attempts). The fix is technically sound but structurally incomplete.
  • src/telegram/bot/delivery.send.ts and src/telegram/bot/delivery.replies.ts need alignment on the return 0 sentinel handling.

Last reviewed commit: b2da493

throw new Error("telegram sendMessage failed: empty formatted text and empty plain fallback");
// Silently skip empty messages to avoid Telegram API 400 error
runtime.log?.("telegram sendMessage skipped: empty content");
return 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning 0 here propagates back through deliverTextReply in delivery.replies.ts, which calls markDelivered(progress) unconditionally after every sendTelegramText call regardless of the return value. This causes:

  1. progress.hasDelivered is set to true even though nothing was sent
  2. progress.deliveredCount is incremented for skipped messages
  3. deliverReplies returns { delivered: true } for a fully skipped reply
  4. Any message_sent hooks fire with success: true for the skipped message (line 631 checks progress.deliveredCount > deliveredCountBeforeReply)
  5. firstDeliveredMessageId is set to 0; if shouldPin is ever true on such a reply, maybePinFirstDeliveredMessage will attempt pinChatMessage(..., 0) (line 482-486)

The test at line 477 verifies the happy-path (no API call, correct log line) but does not assert the return value of deliverReplies or that deliveredCount remains unchanged, leaving this side-effect invisible to CI.

Suggested fix: Change the return type of sendTelegramText to Promise<number | undefined> and return undefined for the skip case, then guard the markDelivered calls in deliverTextReply / sendPendingFollowUpText with if (messageId) checks.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/telegram/bot/delivery.send.ts
Line: 139

Comment:
Returning `0` here propagates back through `deliverTextReply` in `delivery.replies.ts`, which calls `markDelivered(progress)` unconditionally after every `sendTelegramText` call regardless of the return value. This causes:

1. `progress.hasDelivered` is set to `true` even though nothing was sent
2. `progress.deliveredCount` is incremented for skipped messages
3. `deliverReplies` returns `{ delivered: true }` for a fully skipped reply
4. Any `message_sent` hooks fire with `success: true` for the skipped message (line 631 checks `progress.deliveredCount > deliveredCountBeforeReply`)
5. `firstDeliveredMessageId` is set to `0`; if `shouldPin` is ever `true` on such a reply, `maybePinFirstDeliveredMessage` will attempt `pinChatMessage(..., 0)` (line 482-486)

The test at line 477 verifies the happy-path (no API call, correct log line) but does not assert the return value of `deliverReplies` or that `deliveredCount` remains unchanged, leaving this side-effect invisible to CI.

**Suggested fix:** Change the return type of `sendTelegramText` to `Promise<number | undefined>` and return `undefined` for the skip case, then guard the `markDelivered` calls in `deliverTextReply` / `sendPendingFollowUpText` with `if (messageId)` checks.

How can I resolve this? If you propose a fix, please make it concise.

yelog pushed a commit to yelog/openclaw-1 that referenced this pull request Mar 8, 2026
- sendTelegramText returns 0 for skipped empty messages (NO_REPLY, heartbeat ack)
- Check return value before updating delivery state (markDelivered, markReplyApplied)
- Prevents incorrect deliveredCount increment for skipped messages
- Fixes message_sent hooks firing with success=true for skipped messages
- Fixes firstDeliveredMessageId being set to 0 (invalid for pinning)

Fixes openclaw#37294
When sendTelegramText returns 0 (empty content skipped), callers in
delivery.replies.ts were still calling markDelivered() unconditionally,
causing:
- progress.deliveredCount to be incremented for skipped messages
- deliverReplies to return { delivered: true } for skipped replies
- message_sent hooks to fire with success: true for skipped messages
- firstDeliveredMessageId to be set to 0

Now we check messageId !== 0 before tracking delivery.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 19e93b3f42

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +137 to +139
// Silently skip empty messages to avoid Telegram API 400 error
runtime.log?.("telegram sendMessage skipped: empty content");
return 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Handle skipped sends in voice fallback before marking delivery

Returning 0 here introduces a sentinel message ID, but only deliverTextReply/sendPendingFollowUpText were updated to treat it as “not delivered”; the voice-forbidden fallback path still treats any numeric ID as a real send (sendTelegramVoiceFallbackText stores messageId directly, and deliverMediaReply then marks delivery and can pin that ID). In the VOICE_MESSAGES_FORBIDDEN path, if a fallback chunk is skipped (for example when chunking yields an empty-content chunk), this can report success and attempt pinning with message 0 even though no text fallback message was actually sent.

Useful? React with 👍 / 👎.

amittell added a commit to amittell/openclaw that referenced this pull request Mar 11, 2026
Skip empty chunks before sendTelegramText instead of throwing.
Handles all 3 call sites in delivery.replies.ts. Fixes first-chunk
button attachment when chunk 0 is empty (use sentAnyChunk flag
instead of i === 0). Safety net in sendTelegramText returns undefined
instead of throwing, with guarded markDelivered/markReplyApplied.

Closes openclaw#37278. More complete than openclaw#37294.
amittell added a commit to amittell/openclaw that referenced this pull request Mar 15, 2026
Skip empty chunks before sendTelegramText instead of throwing.
Handles all 3 call sites in delivery.replies.ts. Fixes first-chunk
button attachment when chunk 0 is empty (use sentAnyChunk flag
instead of i === 0). Safety net in sendTelegramText returns undefined
instead of throwing, with guarded markDelivered/markReplyApplied.

Closes openclaw#37278. More complete than openclaw#37294.
@yelog

yelog commented Mar 15, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review/comments here.

Closing this PR as stale.

The Telegram delivery path has changed enough since this was opened that this patch is no longer a good fit for the current code structure.

The original idea may still be useful, but it should be reintroduced as a fresh PR against current main if the issue still exists.

@yelog yelog closed this Mar 15, 2026
amittell added a commit to amittell/openclaw that referenced this pull request Mar 18, 2026
Skip empty chunks before sendTelegramText instead of throwing.
Handles all 3 call sites in delivery.replies.ts. Fixes first-chunk
button attachment when chunk 0 is empty (use sentAnyChunk flag
instead of i === 0). Safety net in sendTelegramText returns undefined
instead of throwing, with guarded markDelivered/markReplyApplied.

Closes openclaw#37278. More complete than openclaw#37294.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: telegram Channel integration: telegram size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Telegram channel crashes when agent emits empty reply (GrammyError 400: text must be non-empty)

1 participant