diff --git a/scripts/telegram-bridge.js b/scripts/telegram-bridge.js index 27d5d7ba4..34cdfefbb 100755 --- a/scripts/telegram-bridge.js +++ b/scripts/telegram-bridge.js @@ -78,15 +78,18 @@ async function sendMessage(chatId, text, replyTo) { chunks.push(text.slice(i, i + 4000)); } for (const chunk of chunks) { - await tgApi("sendMessage", { + // tgApi() resolves even on API errors ({ok: false}), so .catch() + // never fires for Markdown parse failures. Check explicitly for + // Telegram's "can't parse entities" error before retrying plain. + const res = await tgApi("sendMessage", { chat_id: chatId, text: chunk, reply_to_message_id: replyTo, parse_mode: "Markdown", - }).catch(() => - // Retry without markdown if it fails (unbalanced formatting) - tgApi("sendMessage", { chat_id: chatId, text: chunk, reply_to_message_id: replyTo }), - ); + }); + if (res.error_code === 400 && /can't parse entities/i.test(res.description || "")) { + await tgApi("sendMessage", { chat_id: chatId, text: chunk, reply_to_message_id: replyTo }); + } } }