refactor(telegram): share account config helpers

This commit is contained in:
Peter Steinberger
2026-03-07 16:21:03 +00:00
parent 398bf51659
commit d02ef9efc2
2 changed files with 12 additions and 31 deletions

View File

@@ -3,9 +3,12 @@ import type { OpenClawConfig } from "../config/config.js";
import { hasConfiguredSecretInput, normalizeSecretInputString } from "../config/types.secrets.js";
import type { TelegramAccountConfig } from "../config/types.telegram.js";
import { resolveAccountWithDefaultFallback } from "../plugin-sdk/account-resolution.js";
import { resolveAccountEntry } from "../routing/account-lookup.js";
import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "../routing/session-key.js";
import { resolveDefaultTelegramAccountId } from "./accounts.js";
import {
mergeTelegramAccountConfig,
resolveDefaultTelegramAccountId,
resolveTelegramAccountConfig,
} from "./accounts.js";
export type TelegramCredentialStatus = "available" | "configured_unavailable" | "missing";
@@ -20,31 +23,6 @@ export type InspectedTelegramAccount = {
config: TelegramAccountConfig;
};
function resolveTelegramAccountConfig(
cfg: OpenClawConfig,
accountId: string,
): TelegramAccountConfig | undefined {
const normalized = normalizeAccountId(accountId);
return resolveAccountEntry(cfg.channels?.telegram?.accounts, normalized);
}
function mergeTelegramAccountConfig(cfg: OpenClawConfig, accountId: string): TelegramAccountConfig {
const {
accounts: _ignored,
defaultAccount: _ignoredDefaultAccount,
groups: channelGroups,
...base
} = (cfg.channels?.telegram ?? {}) as TelegramAccountConfig & {
accounts?: unknown;
defaultAccount?: unknown;
};
const account = resolveTelegramAccountConfig(cfg, accountId) ?? {};
const configuredAccountIds = Object.keys(cfg.channels?.telegram?.accounts ?? {});
const isMultiAccount = configuredAccountIds.length > 1;
const groups = account.groups ?? (isMultiAccount ? undefined : channelGroups);
return { ...base, ...account, groups };
}
function inspectTokenFile(pathValue: unknown): {
token: string;
tokenSource: "tokenFile" | "none";

View File

@@ -97,7 +97,7 @@ export function resolveDefaultTelegramAccountId(cfg: OpenClawConfig): string {
return ids[0] ?? DEFAULT_ACCOUNT_ID;
}
function resolveAccountConfig(
export function resolveTelegramAccountConfig(
cfg: OpenClawConfig,
accountId: string,
): TelegramAccountConfig | undefined {
@@ -105,7 +105,10 @@ function resolveAccountConfig(
return resolveAccountEntry(cfg.channels?.telegram?.accounts, normalized);
}
function mergeTelegramAccountConfig(cfg: OpenClawConfig, accountId: string): TelegramAccountConfig {
export function mergeTelegramAccountConfig(
cfg: OpenClawConfig,
accountId: string,
): TelegramAccountConfig {
const {
accounts: _ignored,
defaultAccount: _ignoredDefaultAccount,
@@ -115,7 +118,7 @@ function mergeTelegramAccountConfig(cfg: OpenClawConfig, accountId: string): Tel
accounts?: unknown;
defaultAccount?: unknown;
};
const account = resolveAccountConfig(cfg, accountId) ?? {};
const account = resolveTelegramAccountConfig(cfg, accountId) ?? {};
// In multi-account setups, channel-level `groups` must NOT be inherited by
// accounts that don't have their own `groups` config. A bot that is not a
@@ -138,7 +141,7 @@ export function createTelegramActionGate(params: {
const accountId = normalizeAccountId(params.accountId);
return createAccountActionGate({
baseActions: params.cfg.channels?.telegram?.actions,
accountActions: resolveAccountConfig(params.cfg, accountId)?.actions,
accountActions: resolveTelegramAccountConfig(params.cfg, accountId)?.actions,
});
}