Skip to content

fix(telegram): silently skip empty-text sends instead of failing - #40384

Closed
amittell wants to merge 5 commits into
openclaw:mainfrom
amittell:fix/empty-text-silent-skip
Closed

amittell wants to merge 5 commits into
openclaw:mainfrom
amittell:fix/empty-text-silent-skip

Conversation

@amittell

@amittell amittell commented Mar 9, 2026

Copy link
Copy Markdown
Contributor

Problem

When an agent reply normalizes to empty or whitespace-only text, the following chain fires and sends a spurious fallback message to the user:

  1. sendTelegramText throws (pre-send check) OR Telegram rejects the API call with 400: text must be non-empty / 400: message text is empty
  2. The throw propagates to onError in bot-message-dispatch.ts
  3. markNonSilentFailure() fires → sends "No response generated. Please try again."

This produces a confusing user-visible error for something that should silently no-op.

Fixes #37278. Supersedes #37294 (incomplete fix).

Root causes fixed

Mode A — pre-send empty check threw instead of skipping

sendTelegramText threw "empty formatted text and empty plain fallback" when htmlText and plainText were both empty. Changed to logVerbose + return undefined.

Mode B — empty chunks reached sendTelegramText in delivery functions

deliverTextReply, sendPendingFollowUpText, sendTelegramVoiceFallbackText passed chunks with empty html AND empty text to sendTelegramText. Added pre-send guards. Also fixed replyToMode="first" threading to attach to the first actually sent chunk via sentAnyChunk flag (was using chunk index 0).

Mode C — EMPTY_TEXT_ERR_RE regex didn't match Telegram's newer error wording

Telegram has two variants: "message text is empty" (old) and "text must be non-empty" (new). The catch block only matched the old form, causing it to fall through to throw err instead of attempting the plain-text fallback. Updated regex: /message text is empty|text must be non-empty/i

Mode D — catch block threw instead of returning undefined when no fallback

When the empty-text error was caught (post-Mode C fix) but no fallback text was available, the code threw instead of returning undefined — inconsistent with the Mode A pre-send path. Changed to logVerbose + return undefined.

Mode E — send.ts HTML-parse fallback didn't handle empty-text errors

withTelegramHtmlParseFallback in send.ts (programmatic message tool path) only caught HTML parse errors. Added empty-text error handling alongside it.

Files changed

  • src/telegram/bot/delivery.send.ts — Modes A, C, D
  • src/telegram/bot/delivery.replies.ts — Mode B + sentAnyChunk fix
  • src/telegram/send.ts — Mode E
  • src/telegram/bot/delivery.test.ts — new tests for A, B, C, D
  • src/telegram/send.test.ts — new test for Mode E

Test results

Related

#40383 fixes the pre-existing retry regressions noted in the test results above.

Copilot AI review requested due to automatic review settings March 9, 2026 00:19
@openclaw-barnacle openclaw-barnacle Bot added channel: telegram Channel integration: telegram size: M labels Mar 9, 2026
@greptile-apps

greptile-apps Bot commented Mar 9, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a multi-mode bug where empty or whitespace-only agent replies caused a spurious "No response generated. Please try again." error in Telegram chats, covering five distinct failure paths (Modes A–E) across the bot delivery and programmatic send layers.

Key changes:

  • Mode A (delivery.send.ts): sendTelegramText pre-send empty check now returns undefined instead of throwing, preventing the error chain that reached onError/markNonSilentFailure.
  • Mode B (delivery.replies.ts): Three delivery functions (deliverTextReply, sendPendingFollowUpText, sendTelegramVoiceFallbackText) gain empty-chunk guards; sentAnyChunk correctly gates button and reply-threading attachment to the first actually sent chunk rather than chunk index 0.
  • Mode C (delivery.send.ts): EMPTY_TEXT_ERR_RE updated to also match Telegram's newer "text must be non-empty" wording.
  • Mode D (delivery.send.ts): Catch block returns undefined instead of re-throwing when the empty-text error is caught but no plain fallback exists, making it consistent with Mode A.
  • Mode E (send.ts): withTelegramHtmlParseFallback (used by the programmatic sendMessageTelegram tool path) now handles empty-text errors alongside HTML parse errors and retries as plain text.

Minor observations:

  • EMPTY_TEXT_ERR_RE is now defined independently in both delivery.send.ts and send.ts; consolidating into a shared helper would prevent future divergence.
  • sendPendingFollowUpText and sendTelegramVoiceFallbackText received the new empty-chunk guard but not the pre-existing if (!chunk) { continue; } null guard that deliverTextReply already had — a small defensive inconsistency.

Confidence Score: 4/5

  • This PR is safe to merge; it eliminates a well-characterised class of spurious user-visible errors with thorough test coverage and no observable behavioural regressions.
  • All five described failure modes are addressed with targeted, minimal changes. The widened return type (number | undefined) is propagated correctly through all callers. Tests cover the new code paths and confirm the old throw-on-empty behaviour is replaced by silent no-ops. The only deductions are a minor regex duplication and a small defensive-null-check inconsistency between the three delivery functions — neither affects correctness.
  • No files require special attention beyond the noted EMPTY_TEXT_ERR_RE duplication between src/telegram/send.ts and src/telegram/bot/delivery.send.ts.

Last reviewed commit: f2dab2d

Comment thread src/telegram/send.ts
Comment on lines 257 to 261

const EMPTY_TEXT_ERR_RE = /message text is empty|text must be non-empty/i;

function isTelegramHtmlParseError(err: unknown): boolean {
return PARSE_ERR_RE.test(formatErrorMessage(err));

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.

Duplicated EMPTY_TEXT_ERR_RE definition

EMPTY_TEXT_ERR_RE is now defined in both src/telegram/send.ts (here) and src/telegram/bot/delivery.send.ts. If Telegram ever introduces a third empty-text error wording, both definitions would need to be updated independently. Consider extracting the shared regex (alongside the existing PARSE_ERR_RE imports) into a shared constant, e.g., in network-errors.ts or a new telegram-errors.ts helper, so the two modules import from a single source of truth.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/telegram/send.ts
Line: 257-261

Comment:
**Duplicated `EMPTY_TEXT_ERR_RE` definition**

`EMPTY_TEXT_ERR_RE` is now defined in both `src/telegram/send.ts` (here) and `src/telegram/bot/delivery.send.ts`. If Telegram ever introduces a third empty-text error wording, both definitions would need to be updated independently. Consider extracting the shared regex (alongside the existing `PARSE_ERR_RE` imports) into a shared constant, e.g., in `network-errors.ts` or a new `telegram-errors.ts` helper, so the two modules import from a single source of truth.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Implemented in commit 773f674d2 — EMPTY_TEXT_ERR_RE moved to src/telegram/network-errors.ts (exported), local definitions removed from both delivery.send.ts and send.ts, both now import from the shared location.

Comment thread src/telegram/bot/delivery.replies.ts Outdated
Comment on lines +171 to +176
for (let i = 0; i < chunks.length; i += 1) {
const chunk = chunks[i];
if (!chunk.html?.trim() && !chunk.text?.trim()) {
logVerbose("telegram: skipping empty chunk in sendPendingFollowUpText");
continue;
}

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.

Missing null guard on chunk before empty-check in sendPendingFollowUpText

deliverTextReply (and the pre-existing code it evolved from) defends against a potentially falsy array element with if (!chunk) { continue; } before the new if (!chunk.html?.trim() && !chunk.text?.trim()) guard. sendPendingFollowUpText (and sendTelegramVoiceFallbackText) received the empty-chunk guard in this PR but not the null guard.

If chunkText() were ever to return a sparse/null element, chunk.html would throw a TypeError here because the optional-chain ?. only protects against chunk.html being null, not chunk itself being null.

While the markdownToTelegramChunks return type excludes null, adding the defensive if (!chunk) { continue; } line (matching deliverTextReply) would keep the three functions consistent:

Suggested change
for (let i = 0; i < chunks.length; i += 1) {
const chunk = chunks[i];
if (!chunk.html?.trim() && !chunk.text?.trim()) {
logVerbose("telegram: skipping empty chunk in sendPendingFollowUpText");
continue;
}
if (!chunk || (!chunk.html?.trim() && !chunk.text?.trim())) {
logVerbose("telegram: skipping empty chunk in sendPendingFollowUpText");
continue;
}

The same applies to sendTelegramVoiceFallbackText at line 235.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/telegram/bot/delivery.replies.ts
Line: 171-176

Comment:
**Missing null guard on `chunk` before empty-check in `sendPendingFollowUpText`**

`deliverTextReply` (and the pre-existing code it evolved from) defends against a potentially falsy array element with `if (!chunk) { continue; }` before the new `if (!chunk.html?.trim() && !chunk.text?.trim())` guard. `sendPendingFollowUpText` (and `sendTelegramVoiceFallbackText`) received the empty-chunk guard in this PR but not the null guard.

If `chunkText()` were ever to return a sparse/null element, `chunk.html` would throw a `TypeError` here because the optional-chain `?.` only protects against `chunk.html` being null, not `chunk` itself being null.

While the `markdownToTelegramChunks` return type excludes null, adding the defensive `if (!chunk) { continue; }` line (matching `deliverTextReply`) would keep the three functions consistent:

```suggestion
    if (!chunk || (!chunk.html?.trim() && !chunk.text?.trim())) {
      logVerbose("telegram: skipping empty chunk in sendPendingFollowUpText");
      continue;
    }
```

The same applies to `sendTelegramVoiceFallbackText` at line 235.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in commit 773f674d2 — added !chunk || null guard in both sendPendingFollowUpText and sendTelegramVoiceFallbackText, matching the pattern already in deliverTextReply.

Copilot AI 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.

Pull request overview

This PR updates the Telegram delivery and send paths to treat empty/whitespace-only message bodies as a silent no-op (instead of throwing and triggering user-visible fallback errors), and broadens empty-text error detection to cover Telegram’s newer wording.

Changes:

  • Make sendTelegramText return undefined (and log verbosely) when both formatted text and plain fallback are empty, or when Telegram rejects empty text and no fallback exists.
  • Add guards in delivery chunking/sending to skip empty chunks and ensure “replyToMode=first”/buttons attach to the first actually sent chunk.
  • Extend HTML→plain fallback logic in src/telegram/send.ts to also retry on Telegram empty-text errors, with tests covering both old/new error strings.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/telegram/bot/delivery.send.ts Skip empty sends instead of throwing; recognize newer Telegram empty-text error wording.
src/telegram/bot/delivery.replies.ts Skip empty chunks; adjust “first” semantics to apply to first sent chunk; avoid progress updates on skipped sends.
src/telegram/send.ts Treat empty-text API errors like HTML-parse errors for HTML→plain fallback in tool send path.
src/telegram/bot/delivery.test.ts Add/adjust tests for silent-skip behavior, threading semantics, and empty-text fallback.
src/telegram/send.test.ts Add test ensuring HTML→plain fallback also triggers on empty-text API errors.

Comment thread src/telegram/bot/delivery.replies.ts Outdated
Comment on lines +239 to +243
// Only apply reply reference, quote text, and buttons to the first sent chunk.
const replyToForChunk = !sentAnyChunk ? opts.replyToId : undefined;
const messageId = await sendTelegramText(opts.bot, opts.chatId, chunk.html, opts.runtime, {
replyToMessageId: replyToForChunk,
replyQuoteText: !appliedReplyTo ? opts.replyQuoteText : undefined,
replyQuoteText: !sentAnyChunk ? opts.replyQuoteText : undefined,

Copilot AI Mar 9, 2026

Copy link

Choose a reason for hiding this comment

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

The comment says reply reference, quote text, and buttons are only applied to the first sent chunk, but sendTelegramText does not use opts.replyQuoteText at all (it only sets reply_to_message_id). Either remove/adjust the comment + replyQuoteText plumbing here, or implement quote support in sendTelegramText (e.g. via Telegram reply_parameters.quote) so this logic is meaningful.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in commit 773f674d2 — comment updated to 'reply reference and buttons are only applied to the first sent chunk' (removed 'quote text' since replyQuoteText is not passed through to sendTelegramText).

Comment thread src/telegram/bot/delivery.test.ts Outdated
Comment on lines +505 to +507
// Force two chunks where the first renders to empty HTML.
// Use a small textLimit and text that will produce an empty first chunk.
await deliverReplies({

Copilot AI Mar 9, 2026

Copy link

Choose a reason for hiding this comment

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

This test claims to cover the case where the first chunk is empty and buttons attach to the next chunk, but the setup (textLimit: 4000, text: "real content here") is unlikely to produce multiple chunks or an empty first chunk. As written it mainly asserts reply_markup on the first call, so it may not exercise the intended behavior; consider forcing chunking (small textLimit + input that yields an empty first HTML chunk) and asserting reply_markup on the correct send call.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in commit 773f674d2 — test now uses textLimit: 20 with input that produces a whitespace-only first chunk followed by two real chunks. Asserts reply_markup is on the first actual sendMessage call (second chunk) and absent from subsequent calls.

@amittell
amittell force-pushed the fix/empty-text-silent-skip branch from f2dab2d to 773f674 Compare March 9, 2026 00:36
@amittell

amittell commented Mar 9, 2026

Copy link
Copy Markdown
Contributor Author

All review feedback addressed in commit 773f674d2:

  • EMPTY_TEXT_ERR_RE moved to network-errors.ts (single source of truth), imported in both delivery.send.ts and send.ts
  • Null guard added to both sendPendingFollowUpText and sendTelegramVoiceFallbackText matching pattern in deliverTextReply
  • Misleading replyQuoteText comment corrected
  • sentAnyChunk test rewritten to force actual multi-chunk scenario with textLimit: 20

@amittell

Copy link
Copy Markdown
Contributor Author

Note for reviewers: The feat/inbound-drain-queue branch (#41597) was built on top of this branch and includes these commits. If this PR merges first (preferred — it is smaller and focused), #41597 will be rebased to drop the duplicate commits. If #41597 merges first, this PR becomes a no-op and can be closed. Either order is safe.

@amittell
amittell force-pushed the fix/empty-text-silent-skip branch from 773f674 to a3a3718 Compare March 10, 2026 19:39
@amittell
amittell force-pushed the fix/empty-text-silent-skip branch from a3a3718 to 82db7df Compare March 10, 2026 20:38
amittell added a commit to amittell/openclaw that referenced this pull request Mar 11, 2026
- Move EMPTY_TEXT_ERR_RE to network-errors.ts, import in both consumers
- Add null guard on chunk in sendPendingFollowUpText + sendTelegramVoiceFallbackText
- Fix misleading replyQuoteText comment in delivery.replies.ts
- Fix sentAnyChunk test to actually force multi-chunk scenario
@amittell
amittell force-pushed the fix/empty-text-silent-skip branch from 82db7df to d589217 Compare March 12, 2026 21:10
@amittell
amittell force-pushed the fix/empty-text-silent-skip branch from d589217 to 07cad96 Compare March 13, 2026 00:38
@amittell
amittell force-pushed the fix/empty-text-silent-skip branch from 07cad96 to 4e133e4 Compare March 13, 2026 01:24
@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation app: web-ui App: web-ui labels Mar 13, 2026
@amittell

Copy link
Copy Markdown
Contributor Author

Restructured to correct paths + addressed all review comments

The previous commits placed files under src/telegram/ — but the actual Telegram code lives in extensions/telegram/src/. This caused CI failures (missing send.test-harness.js, missing delivery.js barrel, wrong import paths). Force-pushed a single clean commit that applies all changes to the correct locations.

Changes applied (extensions/telegram/src/)

network-errors.ts — Added export const EMPTY_TEXT_ERR_RE matching both 'message text is empty' and 'text must be non-empty'. Addresses greptile comment about duplicated definition.

bot/delivery.send.ts (Mode C/D)

  • Import EMPTY_TEXT_ERR_RE from ../network-errors.js (removed local definition)
  • Import logVerbose from globals
  • Return undefined instead of throw for empty-text cases; logVerbose both skip paths
  • Return type widened to Promise<number | undefined>

bot/delivery.replies.ts (Modes A/B)

  • Removed sendChunkedTelegramReplyText, markReplyApplied, resolveReplyToForSend, ReplyThreadDeliveryProgress imports (inlined all three helpers; DeliveryProgress is now self-contained)
  • deliverTextReply: inline loop, null-chunk guard, empty-HTML+text guard, sentAnyChunk flag (buttons attach to first sent chunk, not first iteration index), guards markReplyApplied/markDelivered on messageId != null
  • sendPendingFollowUpText: same inline loop with combined null+empty guard (addresses greptile missing-null-guard comment)
  • sendTelegramVoiceFallbackText: same pattern; appliedReplyTo → sentAnyChunk; updated comment to remove replyQuoteText mention (addresses Copilot comment)

send.ts (Mode E)

  • Import EMPTY_TEXT_ERR_RE from ./network-errors.js
  • Add isTelegramEmptyTextError()
  • withTelegramHtmlParseFallback: catches empty-text errors alongside parse errors, retries with plain text

bot/delivery.test.ts — Updated 'throws when empty' → 'silently skips'; added tests for multi-chunk button attachment (sentAnyChunk), Mode C fallback (text must be non-empty), Mode C fallback (message text is empty)

send.test.ts — Added two Mode E tests verifying plain-text retry on both empty-text error wordings

CI status

All 893 telegram extension tests pass locally. Format check clean. No src/telegram/ files remain.

@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: 8370de1e14

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +260 to +264
if (messageId != null) {
if (firstDeliveredMessageId == null) {
firstDeliveredMessageId = messageId;
}
sentAnyChunk = true;

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 marking voice fallback as delivered when no chunk is sent

In the VOICE_MESSAGES_FORBIDDEN path, sendTelegramVoiceFallbackText can now return undefined when Telegram rejects a chunk as empty and plainText is also empty/whitespace (for example formatting-only fallback text that trims non-empty before chunking but yields whitespace chunk text). Because deliverMediaReply still unconditionally calls markDelivered/markReplyApplied after this helper returns, the reply is reported as successfully delivered and reply threading advances even though no Telegram message was actually sent.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 54152dbe. markReplyApplied/markDelivered are now only called when sendTelegramVoiceFallbackText returns a non-null messageId. If all voice fallback chunks are empty (skipped), no delivery is marked.

@amittell

Copy link
Copy Markdown
Contributor Author

Re: comments #2936194807 & #2937611198

1. Empty-text retry with plain-text fallback (#2936194807): Already handled — the initial commit on this branch added isTelegramEmptyTextError to withTelegramHtmlParseFallback in send.ts (catches both PARSE_ERR_RE and EMPTY_TEXT_ERR_RE and retries with plain text). The bot-handler path in delivery.send.ts also handles both patterns in its shouldLog and catch block. Tests verify both 'text must be non-empty' and 'message text is empty' trigger the plain-text fallback. No additional changes needed.

2. Voice fallback delivery marking (#2937611198): Fixed in 3ca4fc5. sendTelegramVoiceFallbackText can return undefined when all chunks are empty (formatting-only fallback text that trims to whitespace). Previously, markReplyApplied and markDelivered were called unconditionally after the voice fallback. Now they're only called when fallbackMessageId != null, with a verbose log when no chunks were sendable.

@amittell
amittell force-pushed the fix/empty-text-silent-skip branch 2 times, most recently from 54152db to 853e128 Compare March 16, 2026 18:54

@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: 853e128b1b

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +134 to +136
if (!chunk.html?.trim() && !chunk.text?.trim()) {
logVerbose("telegram: skipping empty chunk in deliverTextReply");
return;

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.

P1 Badge Prevent skipped chunks from advancing delivery state

Returning early from this new empty-chunk guard does not actually skip delivery bookkeeping: sendChunkedTelegramReplyText still runs markReplyApplied and markDelivered after sendChunk resolves, so an unsent chunk is counted as delivered and can consume the reply target. This is reproducible whenever chunking yields an empty html+text chunk (the exact case this guard handles), and it can make deliverReplies report success with no Telegram message sent while also dropping reply_to_message_id/buttons for later chunks in replyToMode="first".

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed: changed sendChunk callback to return false for empty chunks, and updated sendChunkedTelegramReplyText to skip markReplyApplied/markDelivered when sendChunk returns false. This ensures empty chunks are fully skipped including all post-send bookkeeping — no more consuming the reply target or inflating deliveredCount. Also restored the missing sendChunkedTelegramReplyText import that was dropped during the branch rebase.

@amittell
amittell force-pushed the fix/empty-text-silent-skip branch from 587e7e6 to efefce2 Compare March 17, 2026 02:46

@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: efefce2fac

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +135 to +137
if (!chunk.html?.trim() && !chunk.text?.trim()) {
logVerbose("telegram: skipping empty chunk in deliverTextReply");
return false;

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.

P1 Badge Treat post-send empty-text skips as unsent chunks

sendChunk only returns false for pre-send !chunk.html && !chunk.text, but sendTelegramText can now no-op later by returning undefined when Telegram rejects HTML as empty and plainText is blank (delivery.send.ts catch path). In that case this callback returns void, so sendChunkedTelegramReplyText still runs delivery bookkeeping and can mark a reply as delivered/consumed even though no Telegram message was sent (same pattern also appears in sendPendingFollowUpText). This is reproducible with formatting-only chunks that survive as non-empty HTML but trim to empty plain text.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed: updated sendChunk callbacks in both deliverTextReply and sendPendingFollowUpText to explicitly check sendTelegramText's return — when it returns undefined (e.g. Telegram rejected empty content in the catch path), the callback now returns false, so the existing sent === false guard in sendChunkedTelegramReplyText correctly skips delivery bookkeeping. Previously, the void return fell through and advanced bookkeeping despite nothing being sent.

@amittell
amittell force-pushed the fix/empty-text-silent-skip branch from c8c6cfe to cb5ba6f Compare March 17, 2026 13:33

@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: cb5ba6f97e

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines 72 to 76
const sent = await params.sendChunk({
chunk,
isFirstChunk,
replyToMessageId,
replyMarkup: isFirstChunk ? params.replyMarkup : undefined,

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 Track first sent chunk before assigning reply markup

Now that sendChunk can return false to skip unsent chunks, this loop still keys replyMarkup off isFirstChunk (i === 0) rather than the first chunk that was actually delivered. If chunk 0 is skipped (for example, a leading whitespace-only/newline chunk), subsequent chunks can send successfully but never receive buttons, so the reply loses its inline keyboard despite being delivered. This path was introduced by the new skip behavior and should use a "first sent chunk" flag for first-chunk-only metadata.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed: introduced a firstSentChunk flag that is set on the first successful send. replyMarkup is now applied to the first actually-delivered chunk rather than i===0, so buttons are not lost when leading chunks are skipped.

@amittell
amittell force-pushed the fix/empty-text-silent-skip branch 2 times, most recently from 3882b02 to c28312c Compare March 17, 2026 18:07
@amittell
amittell force-pushed the fix/empty-text-silent-skip branch from c28312c to 1780816 Compare March 17, 2026 23:29
@amittell
amittell force-pushed the fix/empty-text-silent-skip branch from 1780816 to 4699ac8 Compare March 18, 2026 00:54
amittell added a commit to amittell/openclaw that referenced this pull request Mar 18, 2026
- Move EMPTY_TEXT_ERR_RE to network-errors.ts, import in both consumers
- Add null guard on chunk in sendPendingFollowUpText + sendTelegramVoiceFallbackText
- Fix misleading replyQuoteText comment in delivery.replies.ts
- Fix sentAnyChunk test to actually force multi-chunk scenario
Move changes to correct paths (extensions/telegram/src/ not src/telegram/).

Modes A–E all applied to the real source files:

A. deliverTextReply: inline loop with empty-chunk skip (sentAnyChunk flag),
   replacing sendChunkedTelegramReplyText. Buttons attach to first sent
   chunk (not first iteration index), guarded by messageId != null.

B. sendPendingFollowUpText + sendTelegramVoiceFallbackText: same inline
   loop pattern with null-chunk guard and empty-HTML+text guard.

C. EMPTY_TEXT_ERR_RE exported from network-errors.ts, matching both
   'message text is empty' and 'text must be non-empty'. Local definitions
   removed from delivery.send.ts; send.ts imports from same shared source.

D. sendTelegramText (delivery.send.ts): return undefined instead of throw
   when empty-text has no plain fallback; logVerbose both skip paths.
   Return type widened to Promise<number | undefined>.

E. withTelegramHtmlParseFallback (send.ts): also catches empty-text errors
   (isTelegramEmptyTextError) and retries with plain text, matching
   parse-error fallback behavior. isTelegramEmptyTextError added.

Inline resolveReplyToForSend + markReplyApplied replacing reply-threading
import that was removed. DeliveryProgress now self-contained.

Tests: update 'throws when empty' → 'silently skips'; add:
  - 'attaches buttons only to first sent chunk' (multi-chunk, sentAnyChunk)
  - 'falls back to plain text on text must be non-empty' (Mode C, delivery)
  - 'retries with plain text on text must be non-empty' (Mode E, send.ts)
  - 'retries with plain text on message text is empty' (Mode E, send.ts)

Closes openclaw#37278.
sendTelegramVoiceFallbackText can return undefined when all chunks are
empty (e.g. formatting-only fallback text that trims to whitespace).
Previously, markReplyApplied and markDelivered were called unconditionally
after the voice fallback, reporting the reply as delivered and advancing
reply threading even though no Telegram message was actually sent.

Fix: only call markReplyApplied/markDelivered when fallbackMessageId is
non-null, indicating at least one chunk was successfully sent.

Fixes #2937611198
@amittell
amittell force-pushed the fix/empty-text-silent-skip branch from 4699ac8 to 1e4a314 Compare March 18, 2026 02:23
@amittell

Copy link
Copy Markdown
Contributor Author

Superseded by #49391 (clean rebase, properly targets extensions/telegram/ instead of stale src/telegram/)

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: M

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)

2 participants