fix: recover telegram sends from stale thread ids

This commit is contained in:
Ayaan Zaidi
2026-02-09 08:35:53 +05:30
committed by Ayaan Zaidi
parent 5ac1be9cb6
commit d7bd68ff24
5 changed files with 343 additions and 67 deletions

View File

@@ -478,6 +478,36 @@ describe("sendMessageTelegram", () => {
});
});
it("retries without message_thread_id when Telegram reports missing thread", async () => {
const chatId = "123";
const threadErr = new Error("400: Bad Request: message thread not found");
const sendMessage = vi
.fn()
.mockRejectedValueOnce(threadErr)
.mockResolvedValueOnce({
message_id: 58,
chat: { id: chatId },
});
const api = { sendMessage } as unknown as {
sendMessage: typeof sendMessage;
};
const res = await sendMessageTelegram(chatId, "hello forum", {
token: "tok",
api,
messageThreadId: 271,
});
expect(sendMessage).toHaveBeenNthCalledWith(1, chatId, "hello forum", {
parse_mode: "HTML",
message_thread_id: 271,
});
expect(sendMessage).toHaveBeenNthCalledWith(2, chatId, "hello forum", {
parse_mode: "HTML",
});
expect(res.messageId).toBe("58");
});
it("sets disable_notification when silent is true", async () => {
const chatId = "123";
const sendMessage = vi.fn().mockResolvedValue({
@@ -566,6 +596,45 @@ describe("sendMessageTelegram", () => {
reply_to_message_id: 500,
});
});
it("retries media sends without message_thread_id when thread is missing", async () => {
const chatId = "123";
const threadErr = new Error("400: Bad Request: message thread not found");
const sendPhoto = vi
.fn()
.mockRejectedValueOnce(threadErr)
.mockResolvedValueOnce({
message_id: 59,
chat: { id: chatId },
});
const api = { sendPhoto } as unknown as {
sendPhoto: typeof sendPhoto;
};
loadWebMedia.mockResolvedValueOnce({
buffer: Buffer.from("fake-image"),
contentType: "image/jpeg",
fileName: "photo.jpg",
});
const res = await sendMessageTelegram(chatId, "photo", {
token: "tok",
api,
mediaUrl: "https://example.com/photo.jpg",
messageThreadId: 271,
});
expect(sendPhoto).toHaveBeenNthCalledWith(1, chatId, expect.anything(), {
caption: "photo",
parse_mode: "HTML",
message_thread_id: 271,
});
expect(sendPhoto).toHaveBeenNthCalledWith(2, chatId, expect.anything(), {
caption: "photo",
parse_mode: "HTML",
});
expect(res.messageId).toBe("59");
});
});
describe("sendStickerTelegram", () => {
@@ -626,6 +695,33 @@ describe("sendStickerTelegram", () => {
});
});
it("retries sticker sends without message_thread_id when thread is missing", async () => {
const chatId = "123";
const threadErr = new Error("400: Bad Request: message thread not found");
const sendSticker = vi
.fn()
.mockRejectedValueOnce(threadErr)
.mockResolvedValueOnce({
message_id: 109,
chat: { id: chatId },
});
const api = { sendSticker } as unknown as {
sendSticker: typeof sendSticker;
};
const res = await sendStickerTelegram(chatId, "fileId123", {
token: "tok",
api,
messageThreadId: 271,
});
expect(sendSticker).toHaveBeenNthCalledWith(1, chatId, "fileId123", {
message_thread_id: 271,
});
expect(sendSticker).toHaveBeenNthCalledWith(2, chatId, "fileId123", undefined);
expect(res.messageId).toBe("109");
});
it("includes reply_to_message_id for threaded replies", async () => {
const chatId = "123";
const fileId = "CAACAgIAAxkBAAI...sticker_file_id";