mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 05:01:23 +00:00
fix: align telegram token resolution
This commit is contained in:
@@ -51,6 +51,7 @@ function mockConfig(
|
||||
storePath: string,
|
||||
routingOverrides?: Partial<NonNullable<ClawdisConfig["routing"]>>,
|
||||
agentOverrides?: Partial<NonNullable<ClawdisConfig["agent"]>>,
|
||||
telegramOverrides?: Partial<NonNullable<ClawdisConfig["telegram"]>>,
|
||||
) {
|
||||
configSpy.mockReturnValue({
|
||||
agent: {
|
||||
@@ -60,6 +61,7 @@ function mockConfig(
|
||||
},
|
||||
session: { store: storePath, mainKey: "main" },
|
||||
routing: routingOverrides ? { ...routingOverrides } : undefined,
|
||||
telegram: telegramOverrides ? { ...telegramOverrides } : undefined,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -198,4 +200,46 @@ describe("agentCommand", () => {
|
||||
expect(callArgs?.prompt).toBe("ping");
|
||||
});
|
||||
});
|
||||
|
||||
it("passes telegram token when delivering", async () => {
|
||||
await withTempHome(async (home) => {
|
||||
const store = path.join(home, "sessions.json");
|
||||
mockConfig(home, store, undefined, undefined, { botToken: "t-1" });
|
||||
const deps = {
|
||||
sendMessageWhatsApp: vi.fn(),
|
||||
sendMessageTelegram: vi
|
||||
.fn()
|
||||
.mockResolvedValue({ messageId: "t1", chatId: "123" }),
|
||||
sendMessageDiscord: vi.fn(),
|
||||
sendMessageSignal: vi.fn(),
|
||||
};
|
||||
|
||||
const prevTelegramToken = process.env.TELEGRAM_BOT_TOKEN;
|
||||
process.env.TELEGRAM_BOT_TOKEN = "";
|
||||
try {
|
||||
await agentCommand(
|
||||
{
|
||||
message: "hi",
|
||||
to: "123",
|
||||
deliver: true,
|
||||
provider: "telegram",
|
||||
},
|
||||
runtime,
|
||||
deps,
|
||||
);
|
||||
|
||||
expect(deps.sendMessageTelegram).toHaveBeenCalledWith(
|
||||
"123",
|
||||
"ok",
|
||||
expect.objectContaining({ token: "t-1" }),
|
||||
);
|
||||
} finally {
|
||||
if (prevTelegramToken === undefined) {
|
||||
delete process.env.TELEGRAM_BOT_TOKEN;
|
||||
} else {
|
||||
process.env.TELEGRAM_BOT_TOKEN = prevTelegramToken;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -38,6 +38,7 @@ import {
|
||||
} from "../config/sessions.js";
|
||||
import { emitAgentEvent } from "../infra/agent-events.js";
|
||||
import { defaultRuntime, type RuntimeEnv } from "../runtime.js";
|
||||
import { resolveTelegramToken } from "../telegram/token.js";
|
||||
import { normalizeE164 } from "../utils.js";
|
||||
|
||||
type AgentCommandOpts = {
|
||||
@@ -218,6 +219,8 @@ export async function agentCommand(
|
||||
? buildWorkspaceSkillSnapshot(workspaceDir, { config: cfg })
|
||||
: sessionEntry?.skillsSnapshot;
|
||||
|
||||
const { token: telegramToken } = resolveTelegramToken(cfg);
|
||||
|
||||
if (skillsSnapshot && sessionStore && sessionKey && needsSkillsSnapshot) {
|
||||
const current = sessionEntry ?? {
|
||||
sessionId,
|
||||
@@ -544,6 +547,7 @@ export async function agentCommand(
|
||||
for (const chunk of chunkText(text, 4000)) {
|
||||
await deps.sendMessageTelegram(telegramTarget, chunk, {
|
||||
verbose: false,
|
||||
token: telegramToken || undefined,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
@@ -554,6 +558,7 @@ export async function agentCommand(
|
||||
await deps.sendMessageTelegram(telegramTarget, caption, {
|
||||
verbose: false,
|
||||
mediaUrl: url,
|
||||
token: telegramToken || undefined,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import fs from "node:fs";
|
||||
|
||||
import { loadConfig } from "../config/config.js";
|
||||
import { loadSessionStore, resolveStorePath } from "../config/sessions.js";
|
||||
import { type DiscordProbe, probeDiscord } from "../discord/probe.js";
|
||||
@@ -7,6 +5,7 @@ import { callGateway } from "../gateway/call.js";
|
||||
import { info } from "../globals.js";
|
||||
import type { RuntimeEnv } from "../runtime.js";
|
||||
import { probeTelegram, type TelegramProbe } from "../telegram/probe.js";
|
||||
import { resolveTelegramToken } from "../telegram/token.js";
|
||||
import { resolveHeartbeatSeconds } from "../web/reconnect.js";
|
||||
import {
|
||||
getWebAuthAgeMs,
|
||||
@@ -55,25 +54,6 @@ export type HealthSummary = {
|
||||
|
||||
const DEFAULT_TIMEOUT_MS = 10_000;
|
||||
|
||||
function loadTelegramToken(cfg: ReturnType<typeof loadConfig>): string {
|
||||
const env = process.env.TELEGRAM_BOT_TOKEN?.trim();
|
||||
if (env) return env;
|
||||
|
||||
const tokenFile = cfg.telegram?.tokenFile?.trim();
|
||||
if (tokenFile) {
|
||||
try {
|
||||
if (fs.existsSync(tokenFile)) {
|
||||
const token = fs.readFileSync(tokenFile, "utf-8").trim();
|
||||
if (token) return token;
|
||||
}
|
||||
} catch {
|
||||
// Ignore errors; health should be non-fatal.
|
||||
}
|
||||
}
|
||||
|
||||
return cfg.telegram?.botToken?.trim() ?? "";
|
||||
}
|
||||
|
||||
export async function getHealthSnapshot(
|
||||
timeoutMs?: number,
|
||||
): Promise<HealthSummary> {
|
||||
@@ -95,7 +75,7 @@ export async function getHealthSnapshot(
|
||||
|
||||
const start = Date.now();
|
||||
const cappedTimeout = Math.max(1000, timeoutMs ?? DEFAULT_TIMEOUT_MS);
|
||||
const telegramToken = loadTelegramToken(cfg);
|
||||
const { token: telegramToken } = resolveTelegramToken(cfg);
|
||||
const telegramConfigured = telegramToken.trim().length > 0;
|
||||
const telegramProxy = cfg.telegram?.proxy;
|
||||
const telegramProbe = telegramConfigured
|
||||
|
||||
@@ -4,6 +4,11 @@ import type { CliDeps } from "../cli/deps.js";
|
||||
import type { RuntimeEnv } from "../runtime.js";
|
||||
import { sendCommand } from "./send.js";
|
||||
|
||||
let testConfig: Record<string, unknown> = {};
|
||||
vi.mock("../config/config.js", () => ({
|
||||
loadConfig: () => testConfig,
|
||||
}));
|
||||
|
||||
const callGatewayMock = vi.fn();
|
||||
vi.mock("../gateway/call.js", () => ({
|
||||
callGateway: (...args: unknown[]) => callGatewayMock(...args),
|
||||
@@ -16,6 +21,7 @@ const originalDiscordToken = process.env.DISCORD_BOT_TOKEN;
|
||||
beforeEach(() => {
|
||||
process.env.TELEGRAM_BOT_TOKEN = "token-abc";
|
||||
process.env.DISCORD_BOT_TOKEN = "token-discord";
|
||||
testConfig = {};
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
@@ -75,6 +81,7 @@ describe("sendCommand", () => {
|
||||
.fn()
|
||||
.mockResolvedValue({ messageId: "t1", chatId: "123" }),
|
||||
});
|
||||
testConfig = { telegram: { botToken: "token-abc" } };
|
||||
await sendCommand(
|
||||
{ to: "123", message: "hi", provider: "telegram" },
|
||||
deps,
|
||||
@@ -88,6 +95,26 @@ describe("sendCommand", () => {
|
||||
expect(deps.sendMessageWhatsApp).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("uses config token for telegram when env is missing", async () => {
|
||||
process.env.TELEGRAM_BOT_TOKEN = "";
|
||||
testConfig = { telegram: { botToken: "cfg-token" } };
|
||||
const deps = makeDeps({
|
||||
sendMessageTelegram: vi
|
||||
.fn()
|
||||
.mockResolvedValue({ messageId: "t1", chatId: "123" }),
|
||||
});
|
||||
await sendCommand(
|
||||
{ to: "123", message: "hi", provider: "telegram" },
|
||||
deps,
|
||||
runtime,
|
||||
);
|
||||
expect(deps.sendMessageTelegram).toHaveBeenCalledWith(
|
||||
"123",
|
||||
"hi",
|
||||
expect.objectContaining({ token: "cfg-token" }),
|
||||
);
|
||||
});
|
||||
|
||||
it("routes to discord provider", async () => {
|
||||
const deps = makeDeps({
|
||||
sendMessageDiscord: vi
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import type { CliDeps } from "../cli/deps.js";
|
||||
import { loadConfig } from "../config/config.js";
|
||||
import { callGateway, randomIdempotencyKey } from "../gateway/call.js";
|
||||
import { success } from "../globals.js";
|
||||
import type { RuntimeEnv } from "../runtime.js";
|
||||
import { resolveTelegramToken } from "../telegram/token.js";
|
||||
|
||||
export async function sendCommand(
|
||||
opts: {
|
||||
@@ -25,8 +27,9 @@ export async function sendCommand(
|
||||
}
|
||||
|
||||
if (provider === "telegram") {
|
||||
const { token } = resolveTelegramToken(loadConfig());
|
||||
const result = await deps.sendMessageTelegram(opts.to, opts.message, {
|
||||
token: process.env.TELEGRAM_BOT_TOKEN,
|
||||
token: token || undefined,
|
||||
mediaUrl: opts.media,
|
||||
});
|
||||
runtime.log(
|
||||
|
||||
Reference in New Issue
Block a user