* feat(telegram): support custom apiRoot for alternative API endpoints Add `apiRoot` config option to allow users to specify custom Telegram Bot API endpoints (e.g., self-hosted Bot API servers). Threads the configured base URL through all Telegram API call sites: bot creation, send, probe, audit, media download, and api-fetch. Extends SSRF policy to dynamically trust custom apiRoot hostname for media downloads. Closes #28535 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix(telegram): thread apiRoot through allowFrom lookups * fix(telegram): honor lookup transport and local file paths * refactor(telegram): unify username lookup plumbing * fix(telegram): restore doctor lookup imports * fix: document Telegram apiRoot support (#48842) (thanks @Cypherm) --------- Co-authored-by: Cypherm <28184436+Cypherm@users.noreply.github.com> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Co-authored-by: Ayaan Zaidi <hi@obviy.us>
149 lines
4.1 KiB
TypeScript
149 lines
4.1 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const { botApi, botCtorSpy } = vi.hoisted(() => ({
|
|
botApi: {
|
|
sendMessage: vi.fn(),
|
|
setMessageReaction: vi.fn(),
|
|
deleteMessage: vi.fn(),
|
|
},
|
|
botCtorSpy: vi.fn(),
|
|
}));
|
|
|
|
const { loadConfig } = vi.hoisted(() => ({
|
|
loadConfig: vi.fn(() => ({})),
|
|
}));
|
|
|
|
const { makeProxyFetch } = vi.hoisted(() => ({
|
|
makeProxyFetch: vi.fn(),
|
|
}));
|
|
|
|
const { resolveTelegramFetch } = vi.hoisted(() => ({
|
|
resolveTelegramFetch: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("openclaw/plugin-sdk/config-runtime", async () => {
|
|
const actual = await vi.importActual<typeof import("openclaw/plugin-sdk/config-runtime")>(
|
|
"openclaw/plugin-sdk/config-runtime",
|
|
);
|
|
return {
|
|
...actual,
|
|
loadConfig,
|
|
};
|
|
});
|
|
|
|
vi.mock("./proxy.js", () => ({
|
|
makeProxyFetch,
|
|
}));
|
|
|
|
vi.mock("./fetch.js", () => ({
|
|
resolveTelegramFetch,
|
|
resolveTelegramApiBase: (apiRoot?: string) =>
|
|
apiRoot?.trim()?.replace(/\/+$/, "") || "https://api.telegram.org",
|
|
}));
|
|
|
|
vi.mock("grammy", () => ({
|
|
Bot: class {
|
|
api = botApi;
|
|
catch = vi.fn();
|
|
constructor(
|
|
public token: string,
|
|
public options?: { client?: { fetch?: typeof fetch; timeoutSeconds?: number } },
|
|
) {
|
|
botCtorSpy(token, options);
|
|
}
|
|
},
|
|
InputFile: class {},
|
|
}));
|
|
|
|
import {
|
|
deleteMessageTelegram,
|
|
reactMessageTelegram,
|
|
resetTelegramClientOptionsCacheForTests,
|
|
sendMessageTelegram,
|
|
} from "./send.js";
|
|
|
|
describe("telegram proxy client", () => {
|
|
const proxyUrl = "http://proxy.test:8080";
|
|
|
|
const prepareProxyFetch = () => {
|
|
const proxyFetch = vi.fn();
|
|
const fetchImpl = vi.fn();
|
|
makeProxyFetch.mockReturnValue(proxyFetch as unknown as typeof fetch);
|
|
resolveTelegramFetch.mockReturnValue(fetchImpl as unknown as typeof fetch);
|
|
return { proxyFetch, fetchImpl };
|
|
};
|
|
|
|
const expectProxyClient = (fetchImpl: ReturnType<typeof vi.fn>) => {
|
|
expect(makeProxyFetch).toHaveBeenCalledWith(proxyUrl);
|
|
expect(resolveTelegramFetch).toHaveBeenCalledWith(expect.any(Function), { network: undefined });
|
|
expect(botCtorSpy).toHaveBeenCalledWith(
|
|
"tok",
|
|
expect.objectContaining({
|
|
client: expect.objectContaining({ fetch: fetchImpl }),
|
|
}),
|
|
);
|
|
};
|
|
|
|
beforeEach(() => {
|
|
resetTelegramClientOptionsCacheForTests();
|
|
vi.unstubAllEnvs();
|
|
botApi.sendMessage.mockResolvedValue({ message_id: 1, chat: { id: "123" } });
|
|
botApi.setMessageReaction.mockResolvedValue(undefined);
|
|
botApi.deleteMessage.mockResolvedValue(true);
|
|
botCtorSpy.mockClear();
|
|
loadConfig.mockReturnValue({
|
|
channels: { telegram: { accounts: { foo: { proxy: proxyUrl } } } },
|
|
});
|
|
makeProxyFetch.mockClear();
|
|
resolveTelegramFetch.mockClear();
|
|
});
|
|
|
|
it("reuses cached Telegram client options for repeated sends with same account transport settings", async () => {
|
|
const { fetchImpl } = prepareProxyFetch();
|
|
vi.stubEnv("VITEST", "");
|
|
vi.stubEnv("NODE_ENV", "production");
|
|
|
|
await sendMessageTelegram("123", "first", { token: "tok", accountId: "foo" });
|
|
await sendMessageTelegram("123", "second", { token: "tok", accountId: "foo" });
|
|
|
|
expect(makeProxyFetch).toHaveBeenCalledTimes(1);
|
|
expect(resolveTelegramFetch).toHaveBeenCalledTimes(1);
|
|
expect(botCtorSpy).toHaveBeenCalledTimes(2);
|
|
expect(botCtorSpy).toHaveBeenNthCalledWith(
|
|
1,
|
|
"tok",
|
|
expect.objectContaining({
|
|
client: expect.objectContaining({ fetch: fetchImpl }),
|
|
}),
|
|
);
|
|
expect(botCtorSpy).toHaveBeenNthCalledWith(
|
|
2,
|
|
"tok",
|
|
expect.objectContaining({
|
|
client: expect.objectContaining({ fetch: fetchImpl }),
|
|
}),
|
|
);
|
|
});
|
|
|
|
it.each([
|
|
{
|
|
name: "sendMessage",
|
|
run: () => sendMessageTelegram("123", "hi", { token: "tok", accountId: "foo" }),
|
|
},
|
|
{
|
|
name: "reactions",
|
|
run: () => reactMessageTelegram("123", "456", "✅", { token: "tok", accountId: "foo" }),
|
|
},
|
|
{
|
|
name: "deleteMessage",
|
|
run: () => deleteMessageTelegram("123", "456", { token: "tok", accountId: "foo" }),
|
|
},
|
|
])("uses proxy fetch for $name", async (testCase) => {
|
|
const { fetchImpl } = prepareProxyFetch();
|
|
|
|
await testCase.run();
|
|
|
|
expectProxyClient(fetchImpl);
|
|
});
|
|
});
|