fix: preserve telegram dm topic thread ids

This commit is contained in:
Ayaan Zaidi
2026-02-17 13:03:20 +05:30
committed by Ayaan Zaidi
parent f17b42d2f8
commit 9d9630c83a
2 changed files with 72 additions and 25 deletions

View File

@@ -850,8 +850,7 @@ describe("sendMessageTelegram", () => {
});
});
it("suppresses message_thread_id for private chat sends (#17242)", async () => {
// Private chats have positive numeric IDs; they never support forum topics.
it("keeps message_thread_id for private chat topic sends (#18974)", async () => {
const chatId = "123456789";
const sendMessage = vi.fn().mockResolvedValue({
message_id: 56,
@@ -867,10 +866,9 @@ describe("sendMessageTelegram", () => {
messageThreadId: 271,
});
// message_thread_id must NOT appear in private chats -- Telegram rejects it
// with "400: Bad Request: message thread not found".
expect(sendMessage).toHaveBeenCalledWith(chatId, "hello private", {
parse_mode: "HTML",
message_thread_id: 271,
});
});
@@ -927,6 +925,36 @@ describe("sendMessageTelegram", () => {
expect(res.messageId).toBe("58");
});
it("retries private chat sends without message_thread_id on thread-not-found", async () => {
const chatId = "123456789";
const threadErr = new Error("400: Bad Request: message thread not found");
const sendMessage = vi
.fn()
.mockRejectedValueOnce(threadErr)
.mockResolvedValueOnce({
message_id: 59,
chat: { id: chatId },
});
const api = { sendMessage } as unknown as {
sendMessage: typeof sendMessage;
};
const res = await sendMessageTelegram(chatId, "hello private", {
token: "tok",
api,
messageThreadId: 271,
});
expect(sendMessage).toHaveBeenNthCalledWith(1, chatId, "hello private", {
parse_mode: "HTML",
message_thread_id: 271,
});
expect(sendMessage).toHaveBeenNthCalledWith(2, chatId, "hello private", {
parse_mode: "HTML",
});
expect(res.messageId).toBe("59");
});
it("does not retry thread-not-found when no message_thread_id was provided", async () => {
const chatId = "123";
const threadErr = new Error("400: Bad Request: message thread not found");
@@ -944,6 +972,29 @@ describe("sendMessageTelegram", () => {
expect(sendMessage).toHaveBeenCalledTimes(1);
});
it("does not retry without message_thread_id on chat-not-found", async () => {
const chatId = "123456789";
const chatErr = new Error("400: Bad Request: chat not found");
const sendMessage = vi.fn().mockRejectedValueOnce(chatErr);
const api = { sendMessage } as unknown as {
sendMessage: typeof sendMessage;
};
await expect(
sendMessageTelegram(chatId, "hello private", {
token: "tok",
api,
messageThreadId: 271,
}),
).rejects.toThrow(/chat not found/i);
expect(sendMessage).toHaveBeenCalledTimes(1);
expect(sendMessage).toHaveBeenCalledWith(chatId, "hello private", {
parse_mode: "HTML",
message_thread_id: 271,
});
});
it("sets disable_notification when silent is true", async () => {
const chatId = "123";
const sendMessage = vi.fn().mockResolvedValue({