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.
This commit is contained in:
admin
2026-05-05 15:25:49 +00:00
Unverified
parent e49e379096
commit a076ae2d6d

View File

@@ -281,7 +281,7 @@ export class StreamConsumer {
async _sendFinalFormatted(text) { async _sendFinalFormatted(text) {
const html = markdownToHtml(text); const html = markdownToHtml(text);
// Try HTML first // Try HTML edit first (works for short messages)
try { try {
if (this._messageId) { if (this._messageId) {
await this.ctx.api.editMessageText(this._chatId, this._messageId, html, { parse_mode: 'HTML' }); await this.ctx.api.editMessageText(this._chatId, this._messageId, html, { parse_mode: 'HTML' });
@@ -297,26 +297,27 @@ export class StreamConsumer {
this._lastSentText = html; this._lastSentText = html;
return; return;
} catch (e) { } 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) // Delete the old streaming draft (has raw ** markers)
const plain = stripMarkdown(text);
try {
if (this._messageId) { if (this._messageId) {
await this.ctx.api.editMessageText(this._chatId, this._messageId, plain, { parse_mode: undefined }); try {
} else { await this.ctx.api.deleteMessage(this._chatId, this._messageId);
const msg = await this.ctx.api.sendMessage(this.ctx.chat.id, plain, { parse_mode: undefined }); this._messageId = null;
if (msg?.message_id) { } catch {
this._messageId = msg.message_id; // Ignore — may already be gone
this._chatId = msg.chat.id;
} }
} }
// Send fresh formatted message(s) via sendFormatted (handles splitting + fallback)
try {
await sendFormatted(this.ctx, text);
this._alreadySent = true; this._alreadySent = true;
this._finalResponseSent = true; this._finalResponseSent = true;
this._lastSentText = plain; logger.info(`✅ Sent formatted replacement (${text.length} chars)`);
} catch (e2) { } catch (e2) {
logger.error('Final plain text send also failed:', e2.message); logger.error('Formatted replacement also failed:', e2.message);
} }
} }