Skip to content

Commit b860487

Browse files
Log Telegram outbound delivery success
1 parent c2ba5c4 commit b860487

2 files changed

Lines changed: 59 additions & 9 deletions

File tree

extensions/telegram/src/send.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2052,6 +2052,43 @@ describe("sendMessageTelegram", () => {
20522052
expect(logs).not.toContain(body);
20532053
});
20542054

2055+
it("logs threadless outbound text delivery after missing-thread fallback", async () => {
2056+
const logFile = captureInfoLogs();
2057+
const chatId = "-1001234567890";
2058+
const body = "fallback reply body should stay private";
2059+
const threadErr = new Error("400: Bad Request: message thread not found");
2060+
const sendMessage = vi
2061+
.fn()
2062+
.mockRejectedValueOnce(threadErr)
2063+
.mockResolvedValueOnce({
2064+
message_id: 322,
2065+
chat: { id: chatId },
2066+
});
2067+
const api = { sendMessage } as unknown as {
2068+
sendMessage: typeof sendMessage;
2069+
};
2070+
2071+
await sendMessageTelegram(`telegram:group:${chatId}:topic:271`, body, {
2072+
cfg: TELEGRAM_TEST_CFG,
2073+
token: "tok",
2074+
accountId: "ops",
2075+
api,
2076+
});
2077+
2078+
expect(sendMessage).toHaveBeenNthCalledWith(1, chatId, body, {
2079+
parse_mode: "HTML",
2080+
message_thread_id: 271,
2081+
});
2082+
expect(sendMessage).toHaveBeenNthCalledWith(2, chatId, body, {
2083+
parse_mode: "HTML",
2084+
});
2085+
const logs = capturedLogText(logFile);
2086+
expect(logs).toContain("outbound send ok");
2087+
expect(logs).toContain("messageId=322");
2088+
expect(logs).not.toContain("threadId=271");
2089+
expect(logs).not.toContain(body);
2090+
});
2091+
20552092
it("logs successful outbound media delivery without caption or media location", async () => {
20562093
const logFile = captureInfoLogs();
20572094
const chatId = "123";
@@ -2125,6 +2162,7 @@ describe("sendMessageTelegram", () => {
21252162
});
21262163

21272164
it("retries media sends without message_thread_id when thread is missing", async () => {
2165+
const logFile = captureInfoLogs();
21282166
const chatId = "-100123";
21292167
const threadErr = new Error("400: Bad Request: message thread not found");
21302168
const sendPhoto = vi
@@ -2172,6 +2210,10 @@ describe("sendMessageTelegram", () => {
21722210
},
21732211
);
21742212
expect(res.messageId).toBe("59");
2213+
const logs = capturedLogText(logFile);
2214+
expect(logs).toContain("outbound send ok");
2215+
expect(logs).toContain("messageId=59");
2216+
expect(logs).not.toContain("threadId=271");
21752217
});
21762218

21772219
it("defaults outbound media uploads to 100MB", async () => {

extensions/telegram/src/send.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -584,9 +584,9 @@ async function withTelegramThreadFallback<
584584
verbose: boolean | undefined,
585585
allowThreadlessRetry: boolean,
586586
attempt: (effectiveParams: TParams, effectiveLabel: string) => Promise<T>,
587-
): Promise<T> {
587+
): Promise<{ result: T; acceptedParams: TParams }> {
588588
try {
589-
return await attempt(params, label);
589+
return { result: await attempt(params, label), acceptedParams: params };
590590
} catch (err) {
591591
// Do not widen this fallback to cover "chat not found".
592592
// chat-not-found is routing/auth/membership/token; stripping thread IDs hides root cause.
@@ -603,7 +603,10 @@ async function withTelegramThreadFallback<
603603
);
604604
}
605605
const retriedParams = removeMessageThreadIdParam(params);
606-
return await attempt(retriedParams, `${label}-threadless`);
606+
return {
607+
result: await attempt(retriedParams, `${label}-threadless`),
608+
acceptedParams: retriedParams,
609+
};
607610
}
608611
}
609612

@@ -763,17 +766,22 @@ export async function sendMessageTelegram(
763766
): Promise<{ messageId: string; chatId: string }> => {
764767
let lastMessageId = "";
765768
let lastChatId = chatId;
769+
let lastAcceptedParams: TelegramThreadScopedParams | undefined;
766770
let sentChunkCount = 0;
767771
for (let index = 0; index < chunks.length; index += 1) {
768772
const chunk = chunks[index];
769773
if (!chunk) {
770774
continue;
771775
}
772-
const res = await sendTelegramTextChunk(chunk, buildTextParams(index === chunks.length - 1));
776+
const { result: res, acceptedParams } = await sendTelegramTextChunk(
777+
chunk,
778+
buildTextParams(index === chunks.length - 1),
779+
);
773780
const messageId = resolveTelegramMessageIdOrThrow(res, context);
774781
recordSentMessage(chatId, messageId, cfg);
775782
lastMessageId = String(messageId);
776783
lastChatId = String(res?.chat?.id ?? chatId);
784+
lastAcceptedParams = acceptedParams;
777785
sentChunkCount += 1;
778786
}
779787
if (lastMessageId) {
@@ -783,7 +791,7 @@ export async function sendMessageTelegram(
783791
messageId: lastMessageId,
784792
operation: "sendMessage",
785793
deliveryKind: "text",
786-
messageThreadId: threadParams.message_thread_id,
794+
messageThreadId: lastAcceptedParams?.message_thread_id,
787795
replyToMessageId: opts.replyToMessageId,
788796
silent: opts.silent,
789797
chunkCount: sentChunkCount,
@@ -1015,7 +1023,7 @@ export async function sendMessageTelegram(
10151023
};
10161024
})();
10171025

1018-
const result = await sendMedia(mediaSender.label, mediaSender.sender);
1026+
const { result, acceptedParams } = await sendMedia(mediaSender.label, mediaSender.sender);
10191027
const mediaMessageId = resolveTelegramMessageIdOrThrow(result, "media send");
10201028
const resolvedChatId = String(result?.chat?.id ?? chatId);
10211029
recordSentMessage(chatId, mediaMessageId, cfg);
@@ -1028,7 +1036,7 @@ export async function sendMessageTelegram(
10281036
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
10291037
.join("")}`,
10301038
deliveryKind: mediaSender.label,
1031-
messageThreadId: threadParams.message_thread_id,
1039+
messageThreadId: acceptedParams?.message_thread_id,
10321040
replyToMessageId: opts.replyToMessageId,
10331041
silent: opts.silent,
10341042
});
@@ -1598,7 +1606,7 @@ export async function sendStickerTelegram(
15981606

15991607
const stickerParams = hasThreadParams ? threadParams : undefined;
16001608

1601-
const result = await withTelegramThreadFallback(
1609+
const { result } = await withTelegramThreadFallback(
16021610
stickerParams,
16031611
"sticker",
16041612
opts.verbose,
@@ -1706,7 +1714,7 @@ export async function sendPollTelegram(
17061714
...(opts.silent === true ? { disable_notification: true } : {}),
17071715
};
17081716

1709-
const result = await withTelegramThreadFallback(
1717+
const { result } = await withTelegramThreadFallback(
17101718
pollParams,
17111719
"poll",
17121720
opts.verbose,

0 commit comments

Comments
 (0)