mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 19:44:59 +00:00
refactor(telegram): share getChat id lookup helper
This commit is contained in:
56
src/channels/telegram/api.test.ts
Normal file
56
src/channels/telegram/api.test.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { fetchTelegramChatId } from "./api.js";
|
||||
|
||||
describe("fetchTelegramChatId", () => {
|
||||
it("returns stringified id when Telegram getChat succeeds", async () => {
|
||||
const fetchMock = vi.fn(async () => ({
|
||||
ok: true,
|
||||
json: async () => ({ ok: true, result: { id: 12345 } }),
|
||||
}));
|
||||
vi.stubGlobal("fetch", fetchMock);
|
||||
|
||||
const id = await fetchTelegramChatId({
|
||||
token: "abc",
|
||||
chatId: "@user",
|
||||
});
|
||||
|
||||
expect(id).toBe("12345");
|
||||
expect(fetchMock).toHaveBeenCalledWith(
|
||||
"https://api.telegram.org/botabc/getChat?chat_id=%40user",
|
||||
undefined,
|
||||
);
|
||||
});
|
||||
|
||||
it("returns null when response is not ok", async () => {
|
||||
vi.stubGlobal(
|
||||
"fetch",
|
||||
vi.fn(async () => ({
|
||||
ok: false,
|
||||
json: async () => ({}),
|
||||
})),
|
||||
);
|
||||
|
||||
const id = await fetchTelegramChatId({
|
||||
token: "abc",
|
||||
chatId: "@user",
|
||||
});
|
||||
|
||||
expect(id).toBeNull();
|
||||
});
|
||||
|
||||
it("returns null on transport failures", async () => {
|
||||
vi.stubGlobal(
|
||||
"fetch",
|
||||
vi.fn(async () => {
|
||||
throw new Error("network failed");
|
||||
}),
|
||||
);
|
||||
|
||||
const id = await fetchTelegramChatId({
|
||||
token: "abc",
|
||||
chatId: "@user",
|
||||
});
|
||||
|
||||
expect(id).toBeNull();
|
||||
});
|
||||
});
|
||||
24
src/channels/telegram/api.ts
Normal file
24
src/channels/telegram/api.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
export async function fetchTelegramChatId(params: {
|
||||
token: string;
|
||||
chatId: string;
|
||||
signal?: AbortSignal;
|
||||
}): Promise<string | null> {
|
||||
const url = `https://api.telegram.org/bot${params.token}/getChat?chat_id=${encodeURIComponent(params.chatId)}`;
|
||||
try {
|
||||
const res = await fetch(url, params.signal ? { signal: params.signal } : undefined);
|
||||
if (!res.ok) {
|
||||
return null;
|
||||
}
|
||||
const data = (await res.json().catch(() => null)) as {
|
||||
ok?: boolean;
|
||||
result?: { id?: number | string };
|
||||
} | null;
|
||||
const id = data?.ok ? data?.result?.id : undefined;
|
||||
if (typeof id === "number" || typeof id === "string") {
|
||||
return String(id);
|
||||
}
|
||||
return null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user