Skip to content

Commit 8e2b50c

Browse files
committed
fix(telegram): normalize delivery group chat ids
1 parent 208fb1a commit 8e2b50c

3 files changed

Lines changed: 40 additions & 2 deletions

File tree

src/telegram/send.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,28 @@ describe("sendMessageTelegram", () => {
470470
).rejects.toThrow(/could not be resolved to a numeric chat ID/i);
471471
});
472472

473+
it("sends bare group-prefixed numeric delivery targets without lookup", async () => {
474+
const sendMessage = vi.fn().mockResolvedValue({
475+
message_id: 1,
476+
chat: { id: "-100123" },
477+
});
478+
const getChat = vi.fn();
479+
const api = { sendMessage, getChat } as unknown as {
480+
sendMessage: typeof sendMessage;
481+
getChat: typeof getChat;
482+
};
483+
484+
await sendMessageTelegram("group:-100123", "hi", {
485+
token: "tok",
486+
api,
487+
});
488+
489+
expect(getChat).not.toHaveBeenCalled();
490+
expect(sendMessage).toHaveBeenCalledWith("-100123", "hi", {
491+
parse_mode: "HTML",
492+
});
493+
});
494+
473495
it("includes thread params in media messages", async () => {
474496
const chatId = "-1001234567890";
475497
const sendPhoto = vi.fn().mockResolvedValue({

src/telegram/targets.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ describe("normalizeTelegramChatId", () => {
9393
expect(normalizeTelegramChatId("123456789")).toBe("123456789");
9494
});
9595

96+
it("normalizes bare delivery-only group prefixes for numeric chat ids", () => {
97+
expect(normalizeTelegramChatId("group:-1001234567890")).toBe("-1001234567890");
98+
expect(normalizeTelegramChatId("group:123456789")).toBe("123456789");
99+
});
100+
96101
it("returns undefined for empty input", () => {
97102
expect(normalizeTelegramChatId(" ")).toBeUndefined();
98103
});
@@ -111,6 +116,11 @@ describe("normalizeTelegramLookupTarget", () => {
111116
expect(normalizeTelegramLookupTarget("123456789")).toBe("123456789");
112117
});
113118

119+
it("normalizes bare delivery-only group prefixes for numeric chat ids", () => {
120+
expect(normalizeTelegramLookupTarget("group:-1001234567890")).toBe("-1001234567890");
121+
expect(normalizeTelegramLookupTarget("group:123456789")).toBe("123456789");
122+
});
123+
114124
it("rejects invalid username forms", () => {
115125
expect(normalizeTelegramLookupTarget("@bad-handle")).toBeUndefined();
116126
expect(normalizeTelegramLookupTarget("bad-handle")).toBeUndefined();

src/telegram/targets.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ export type TelegramTarget = {
77
const TELEGRAM_NUMERIC_CHAT_ID_REGEX = /^-?\d+$/;
88
const TELEGRAM_USERNAME_REGEX = /^[A-Za-z0-9_]{5,}$/i;
99

10+
function normalizeTelegramDeliveryGroupChatId(raw: string): string {
11+
const trimmed = raw.trim();
12+
const match = /^group:\s*(-?\d+)$/i.exec(trimmed);
13+
return match?.[1] ?? trimmed;
14+
}
15+
1016
export function stripTelegramInternalPrefixes(to: string): string {
1117
let trimmed = to.trim();
1218
let strippedTelegramPrefix = false;
@@ -30,7 +36,7 @@ export function stripTelegramInternalPrefixes(to: string): string {
3036
}
3137

3238
export function normalizeTelegramChatId(raw: string): string | undefined {
33-
const stripped = stripTelegramInternalPrefixes(raw);
39+
const stripped = normalizeTelegramDeliveryGroupChatId(stripTelegramInternalPrefixes(raw));
3440
if (!stripped) {
3541
return undefined;
3642
}
@@ -45,7 +51,7 @@ export function isNumericTelegramChatId(raw: string): boolean {
4551
}
4652

4753
export function normalizeTelegramLookupTarget(raw: string): string | undefined {
48-
const stripped = stripTelegramInternalPrefixes(raw);
54+
const stripped = normalizeTelegramDeliveryGroupChatId(stripTelegramInternalPrefixes(raw));
4955
if (!stripped) {
5056
return undefined;
5157
}

0 commit comments

Comments
 (0)