Skip to content

Commit a866579

Browse files
fix(telegram): narrow retry to Markdown parse errors only
Address review feedback: only retry without parse_mode when Telegram returns 400 with "can't parse entities", instead of retrying on all failures. This avoids masking real errors (403, 429, invalid chat). Signed-off-by: brianyang <brianyang@signalpro.com.tw>
1 parent 738643f commit a866579

1 file changed

Lines changed: 6 additions & 10 deletions

File tree

scripts/telegram-bridge.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,16 @@ async function sendMessage(chatId, text, replyTo) {
7979
}
8080
for (const chunk of chunks) {
8181
// tgApi() resolves even on API errors ({ok: false}), so .catch()
82-
// never fires for Markdown parse failures. Check res.ok explicitly.
83-
let res = await tgApi("sendMessage", {
82+
// never fires for Markdown parse failures. Check explicitly for
83+
// Telegram's "can't parse entities" error before retrying plain.
84+
const res = await tgApi("sendMessage", {
8485
chat_id: chatId,
8586
text: chunk,
8687
reply_to_message_id: replyTo,
8788
parse_mode: "Markdown",
88-
}).catch((e) => ({ ok: false, description: e.message }));
89-
if (!res.ok) {
90-
// Retry without Markdown (unbalanced formatting from LLM output)
91-
res = await tgApi("sendMessage", {
92-
chat_id: chatId,
93-
text: chunk,
94-
reply_to_message_id: replyTo,
95-
}).catch((e) => ({ ok: false, description: e.message }));
89+
});
90+
if (res.error_code === 400 && /can't parse entities/i.test(res.description || "")) {
91+
await tgApi("sendMessage", { chat_id: chatId, text: chunk, reply_to_message_id: replyTo });
9692
}
9793
}
9894
}

0 commit comments

Comments
 (0)