fix: align telegram token resolution

This commit is contained in:
Peter Steinberger
2026-01-01 21:22:59 +01:00
parent e0043906be
commit c7364de2f0
13 changed files with 278 additions and 72 deletions
+5 -2
View File
@@ -2,6 +2,7 @@ import { loadConfig } from "../config/config.js";
import type { RuntimeEnv } from "../runtime.js";
import { createTelegramBot } from "./bot.js";
import { makeProxyFetch } from "./proxy.js";
import { resolveTelegramToken } from "./token.js";
import { startTelegramWebhook } from "./webhook.js";
export type MonitorTelegramOpts = {
@@ -17,10 +18,12 @@ export type MonitorTelegramOpts = {
};
export async function monitorTelegramProvider(opts: MonitorTelegramOpts = {}) {
const token = (opts.token ?? process.env.TELEGRAM_BOT_TOKEN)?.trim();
const { token } = resolveTelegramToken(loadConfig(), {
envToken: opts.token,
});
if (!token) {
throw new Error(
"TELEGRAM_BOT_TOKEN or telegram.botToken is required for Telegram gateway",
"TELEGRAM_BOT_TOKEN or telegram.botToken/tokenFile is required for Telegram gateway",
);
}
+59
View File
@@ -0,0 +1,59 @@
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
import type { ClawdisConfig } from "../config/config.js";
import { resolveTelegramToken } from "./token.js";
function withTempDir(): string {
return fs.mkdtempSync(path.join(os.tmpdir(), "clawdis-telegram-token-"));
}
describe("resolveTelegramToken", () => {
afterEach(() => {
vi.unstubAllEnvs();
});
it("prefers env token over config", () => {
vi.stubEnv("TELEGRAM_BOT_TOKEN", "env-token");
const cfg = { telegram: { botToken: "cfg-token" } } as ClawdisConfig;
const res = resolveTelegramToken(cfg);
expect(res.token).toBe("env-token");
expect(res.source).toBe("env");
});
it("uses tokenFile when configured", () => {
vi.stubEnv("TELEGRAM_BOT_TOKEN", "");
const dir = withTempDir();
const tokenFile = path.join(dir, "token.txt");
fs.writeFileSync(tokenFile, "file-token\n", "utf-8");
const cfg = { telegram: { tokenFile } } as ClawdisConfig;
const res = resolveTelegramToken(cfg);
expect(res.token).toBe("file-token");
expect(res.source).toBe("tokenFile");
fs.rmSync(dir, { recursive: true, force: true });
});
it("falls back to config token when no env or tokenFile", () => {
vi.stubEnv("TELEGRAM_BOT_TOKEN", "");
const cfg = { telegram: { botToken: "cfg-token" } } as ClawdisConfig;
const res = resolveTelegramToken(cfg);
expect(res.token).toBe("cfg-token");
expect(res.source).toBe("config");
});
it("does not fall back to config when tokenFile is missing", () => {
vi.stubEnv("TELEGRAM_BOT_TOKEN", "");
const dir = withTempDir();
const tokenFile = path.join(dir, "missing-token.txt");
const cfg = {
telegram: { tokenFile, botToken: "cfg-token" },
} as ClawdisConfig;
const res = resolveTelegramToken(cfg);
expect(res.token).toBe("");
expect(res.source).toBe("none");
fs.rmSync(dir, { recursive: true, force: true });
});
});
+50
View File
@@ -0,0 +1,50 @@
import fs from "node:fs";
import type { ClawdisConfig } from "../config/config.js";
export type TelegramTokenSource = "env" | "tokenFile" | "config" | "none";
export type TelegramTokenResolution = {
token: string;
source: TelegramTokenSource;
};
type ResolveTelegramTokenOpts = {
envToken?: string | null;
logMissingFile?: (message: string) => void;
};
export function resolveTelegramToken(
cfg?: ClawdisConfig,
opts: ResolveTelegramTokenOpts = {},
): TelegramTokenResolution {
const envToken = (opts.envToken ?? process.env.TELEGRAM_BOT_TOKEN)?.trim();
if (envToken) {
return { token: envToken, source: "env" };
}
const tokenFile = cfg?.telegram?.tokenFile?.trim();
if (tokenFile) {
if (!fs.existsSync(tokenFile)) {
opts.logMissingFile?.(`telegram.tokenFile not found: ${tokenFile}`);
return { token: "", source: "none" };
}
try {
const token = fs.readFileSync(tokenFile, "utf-8").trim();
if (token) {
return { token, source: "tokenFile" };
}
} catch (err) {
opts.logMissingFile?.(`telegram.tokenFile read failed: ${String(err)}`);
return { token: "", source: "none" };
}
return { token: "", source: "none" };
}
const configToken = cfg?.telegram?.botToken?.trim();
if (configToken) {
return { token: configToken, source: "config" };
}
return { token: "", source: "none" };
}