From a076ae2d6d1421a4eb30812798c0d4a8f00f1dbc Mon Sep 17 00:00:00 2001 From: admin Date: Tue, 5 May 2026 15:25:49 +0000 Subject: [PATCH] fix: delete raw-markdown draft and resend formatted on final edit failure When streaming produces a long essay that exceeds Telegram's edit limit, the final HTML edit silently fails, leaving the user with raw ** markers. Now: delete the draft message and send fresh formatted message(s) via sendFormatted which handles HTML conversion, splitting, and fallback. --- src/bot/message-sender.js | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/bot/message-sender.js b/src/bot/message-sender.js index 5cef08c3..dffd2977 100644 --- a/src/bot/message-sender.js +++ b/src/bot/message-sender.js @@ -281,7 +281,7 @@ export class StreamConsumer { async _sendFinalFormatted(text) { const html = markdownToHtml(text); - // Try HTML first + // Try HTML edit first (works for short messages) try { if (this._messageId) { await this.ctx.api.editMessageText(this._chatId, this._messageId, html, { parse_mode: 'HTML' }); @@ -297,26 +297,27 @@ export class StreamConsumer { this._lastSentText = html; return; } catch (e) { - logger.warn(`Final HTML edit failed (${e.message}), falling back to plain text`); + logger.warn(`Final HTML edit failed (${e.message}), deleting draft and resending`); } - // Fallback: stripped plain text (no raw ** showing) - const plain = stripMarkdown(text); - try { - if (this._messageId) { - await this.ctx.api.editMessageText(this._chatId, this._messageId, plain, { parse_mode: undefined }); - } else { - const msg = await this.ctx.api.sendMessage(this.ctx.chat.id, plain, { parse_mode: undefined }); - if (msg?.message_id) { - this._messageId = msg.message_id; - this._chatId = msg.chat.id; - } + // Delete the old streaming draft (has raw ** markers) + if (this._messageId) { + try { + await this.ctx.api.deleteMessage(this._chatId, this._messageId); + this._messageId = null; + } catch { + // Ignore — may already be gone } + } + + // Send fresh formatted message(s) via sendFormatted (handles splitting + fallback) + try { + await sendFormatted(this.ctx, text); this._alreadySent = true; this._finalResponseSent = true; - this._lastSentText = plain; + logger.info(`✅ Sent formatted replacement (${text.length} chars)`); } catch (e2) { - logger.error('Final plain text send also failed:', e2.message); + logger.error('Formatted replacement also failed:', e2.message); } }