UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

134 lines (133 loc) • 4.78 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { t as asNonArrayRecord } from "./record-coerce-CVPuDqcc.mjs"; import { a as coerceSecretRef, o as hasConfiguredSecretInput, p as normalizeSecretInputString } from "./types.secrets-BmNLLnHp.mjs"; import { n as normalizeAccountId, t as DEFAULT_ACCOUNT_ID } from "./account-id-BqKT3GuY.mjs"; import { t as canResolveEnvSecretRefInReadOnlyPath } from "./secret-ref-readonly.internal-CHEvk9t0.mjs"; import "./string-coerce-runtime-BwAZZDf5.mjs"; import "./routing-DmgGYTrU.mjs"; import "./secret-input-CpgOwFAi.mjs"; import "./secret-ref-readonly-B0VYl5Yv.mjs"; import { a as mergeTelegramAccountConfig, n as listTelegramAccountIds, o as resolveTelegramAccountConfig } from "./account-selection-CGYGB8bX.mjs"; import { t as readTelegramTokenFile } from "./token-Dmy_LoAW.mjs"; import { a as resolveTelegramAccountFallback } from "./accounts-C26vVqsr.mjs"; //#region extensions/telegram/src/account-inspect.ts function inspectTokenFile(e, t) { const n = normalizeOptionalString(e) ?? ""; if (!n) return null; const r = readTelegramTokenFile(n, t); return { token: r.token, tokenSource: r.source, tokenStatus: r.credentialDiagnostics ? "configured_unavailable" : "available", ...r.credentialDiagnostics ? { credentialDiagnostics: r.credentialDiagnostics } : {} }; } function inspectTokenValue(e) { const t = coerceSecretRef(e.value, e.cfg.secrets?.defaults); if (t?.source === "env") { const n = canResolveEnvSecretRefInReadOnlyPath({ cfg: e.cfg, provider: t.provider, id: t.id }) ? normalizeOptionalString(process.env[t.id]) : void 0; return { token: n ?? "", tokenSource: "env", tokenStatus: n ? "available" : "configured_unavailable" }; } const n = normalizeSecretInputString(e.value); if (n || hasConfiguredSecretInput(e.value, e.cfg.secrets?.defaults)) return { token: n || "", tokenSource: "config", tokenStatus: n ? "available" : "configured_unavailable" }; return null; } function readTelegramAccount(e) { return resolveTelegramAccountFallback(e, (t) => { const n = normalizeAccountId(t); const r = mergeTelegramAccountConfig(e.cfg, n); const i = { accountId: n, enabled: e.cfg.channels?.telegram?.enabled !== false && r.enabled !== false, name: normalizeOptionalString(r.name), config: r }; const a = resolveTelegramAccountConfig(e.cfg, n); const o = n === "default" || Boolean(a) || Object.keys(asNonArrayRecord(e.cfg.channels?.telegram?.accounts)).length === 0; const s = [{ config: a, path: `channels.telegram.accounts.${n}` }, ...o ? [{ config: e.cfg.channels?.telegram, path: "channels.telegram" }] : []]; for (const { config: t, path: n } of s) { const r = inspectTokenFile(t?.tokenFile, `${n}.tokenFile`) ?? inspectTokenValue({ cfg: e.cfg, value: t?.botToken }); if (r) return { ...i, ...r, configured: r.tokenStatus !== "missing" }; } const c = n === "default" ? normalizeOptionalString(e.envToken) ?? normalizeOptionalString(process.env.TELEGRAM_BOT_TOKEN) ?? "" : ""; if (c) return { ...i, token: c, tokenSource: "env", tokenStatus: "available", configured: true }; return { ...i, token: "", tokenSource: "none", tokenStatus: "missing", configured: false, stateReason: o ? void 0 : `not configured: unknown accountId "${n}" in multi-bot setup` }; }); } function findTelegramTokenOwnerAccountId(e) { const t = normalizeAccountId(e.accountId); const n = /* @__PURE__ */ new Map(); for (const r of listTelegramAccountIds(e.cfg)) { const i = readTelegramAccount({ ...e, accountId: r }); const a = i.token.trim(); if (!a) continue; const o = n.get(a); if (i.accountId === t) return o ?? null; if (!o) n.set(a, i.accountId); } return null; } function formatDuplicateTelegramTokenReason(e) { return `Duplicate Telegram bot token: account "${e.accountId}" shares a token with account "${e.ownerAccountId}". Keep one owner account per bot token.`; } function inspectTelegramAccount(e) { const t = readTelegramAccount(e); const n = t.token ? findTelegramTokenOwnerAccountId({ ...e, accountId: t.accountId }) : null; const r = resolveTelegramAccountConfig(e.cfg, t.accountId)?.groups ?? e.cfg.channels?.telegram?.groups; return { ...t, configured: t.configured && !n, stateReason: n ? formatDuplicateTelegramTokenReason({ accountId: t.accountId, ownerAccountId: n }) : t.stateReason, mode: t.config.webhookUrl ? "webhook" : "polling", allowUnmentionedGroups: Object.values(r ?? {}).some((e) => e?.requireMention === false) }; } //#endregion export { formatDuplicateTelegramTokenReason as n, inspectTelegramAccount as r, findTelegramTokenOwnerAccountId as t };