fix(doctor): warn on missing channel env tokens

This commit is contained in:
Peter Steinberger
2026-05-02 04:28:57 +01:00
parent d2f623d560
commit ac58dc2e92
8 changed files with 173 additions and 5 deletions

View File

@@ -5,6 +5,7 @@ import {
collectTelegramApiRootWarnings,
collectTelegramEmptyAllowlistExtraWarnings,
collectTelegramGroupPolicyWarnings,
collectTelegramMissingEnvTokenWarnings,
maybeRepairTelegramApiRoots,
maybeRepairTelegramAllowFromUsernames,
scanTelegramBotEndpointApiRoots,
@@ -62,7 +63,7 @@ describe("telegram doctor", () => {
enabled: true,
token: "tok",
tokenSource: "config",
tokenStatus: "configured",
tokenStatus: "available",
});
lookupTelegramChatIdMock.mockReset();
});
@@ -355,4 +356,70 @@ describe("telegram doctor", () => {
"- channels.telegram.apiRoot: removed trailing /bot<TOKEN> from Telegram apiRoot.",
]);
});
it("warns when default env fallback token is missing after migration", async () => {
const cfg = {
channels: {
telegram: {
allowFrom: ["123"],
},
},
} as unknown as OpenClawConfig;
inspectTelegramAccountMock.mockReturnValueOnce({
enabled: true,
token: "",
tokenSource: "none",
tokenStatus: "missing",
configured: false,
config: {},
});
expect(collectTelegramMissingEnvTokenWarnings({ cfg, env: {} })).toEqual([
expect.stringContaining("TELEGRAM_BOT_TOKEN is absent"),
]);
inspectTelegramAccountMock.mockReturnValueOnce({
enabled: true,
token: "123:tok",
tokenSource: "env",
tokenStatus: "available",
configured: true,
config: {},
});
expect(
collectTelegramMissingEnvTokenWarnings({ cfg, env: { TELEGRAM_BOT_TOKEN: "123:tok" } }),
).toEqual([]);
inspectTelegramAccountMock.mockReturnValueOnce({
enabled: true,
token: "",
tokenSource: "none",
tokenStatus: "missing",
configured: false,
config: {},
});
expect(
await telegramDoctor.collectPreviewWarnings?.({
cfg,
doctorFixCommand: "openclaw doctor --fix",
env: {},
}),
).toContainEqual(expect.stringContaining("TELEGRAM_BOT_TOKEN is absent"));
});
it("does not warn about TELEGRAM_BOT_TOKEN when a non-default account is selected", () => {
const cfg = {
channels: {
telegram: {
accounts: {
work: {
botToken: "123:work",
},
},
},
},
} as unknown as OpenClawConfig;
expect(collectTelegramMissingEnvTokenWarnings({ cfg, env: {} })).toEqual([]);
});
});

View File

@@ -6,7 +6,11 @@ import type { OpenClawConfig } from "openclaw/plugin-sdk/config-types";
import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime";
import { normalizeOptionalString } from "openclaw/plugin-sdk/text-runtime";
import { inspectTelegramAccount } from "./account-inspect.js";
import { listTelegramAccountIds, resolveTelegramAccount } from "./accounts.js";
import {
listTelegramAccountIds,
resolveDefaultTelegramAccountId,
resolveTelegramAccount,
} from "./accounts.js";
import { isNumericTelegramSenderUserId, normalizeTelegramAllowFromEntry } from "./allow-from.js";
import { lookupTelegramChatId } from "./api-fetch.js";
import { hasTelegramBotEndpointApiRoot, normalizeTelegramApiRoot } from "./api-root.js";
@@ -224,6 +228,26 @@ export function maybeRepairTelegramApiRoots(cfg: OpenClawConfig): {
};
}
export function collectTelegramMissingEnvTokenWarnings(params: {
cfg: OpenClawConfig;
env?: NodeJS.ProcessEnv;
}): string[] {
if (resolveDefaultTelegramAccountId(params.cfg) !== "default") {
return [];
}
const account = inspectTelegramAccount({
cfg: params.cfg,
accountId: "default",
envToken: params.env?.TELEGRAM_BOT_TOKEN ?? "",
});
if (!account.enabled || account.tokenStatus !== "missing" || account.tokenSource !== "none") {
return [];
}
return [
"- channels.telegram: default account has no available bot token, and TELEGRAM_BOT_TOKEN is absent in this doctor environment. After migration, verify TELEGRAM_BOT_TOKEN is present in the state-dir .env or configure channels.telegram.botToken / channels.telegram.accounts.default.botToken as a SecretRef.",
];
}
async function repairTelegramConfig(params: { cfg: OpenClawConfig }): Promise<{
config: OpenClawConfig;
changes: string[];
@@ -472,7 +496,8 @@ export function collectTelegramEmptyAllowlistExtraWarnings(
export const telegramDoctor: ChannelDoctorAdapter = {
legacyConfigRules: TELEGRAM_LEGACY_CONFIG_RULES,
normalizeCompatibilityConfig: normalizeTelegramCompatibilityConfig,
collectPreviewWarnings: ({ cfg, doctorFixCommand }) => [
collectPreviewWarnings: ({ cfg, doctorFixCommand, env }) => [
...collectTelegramMissingEnvTokenWarnings({ cfg, env }),
...collectTelegramInvalidAllowFromWarnings({
hits: scanTelegramInvalidAllowFromEntries(cfg),
doctorFixCommand,