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:
@@ -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);
|
if (this._messageId) {
|
||||||
try {
|
try {
|
||||||
if (this._messageId) {
|
await this.ctx.api.deleteMessage(this._chatId, this._messageId);
|
||||||
await this.ctx.api.editMessageText(this._chatId, this._messageId, plain, { parse_mode: undefined });
|
this._messageId = null;
|
||||||
} else {
|
} catch {
|
||||||
const msg = await this.ctx.api.sendMessage(this.ctx.chat.id, plain, { parse_mode: undefined });
|
// Ignore — may already be gone
|
||||||
if (msg?.message_id) {
|
|
||||||
this._messageId = msg.message_id;
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user