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) {
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);
}
}