mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-18 13:30:48 +00:00
136 lines
4.8 KiB
TypeScript
136 lines
4.8 KiB
TypeScript
import { createScopedChannelConfigBase } from "openclaw/plugin-sdk/compat";
|
|
import {
|
|
createScopedAccountConfigAccessors,
|
|
formatAllowFromLowercase,
|
|
} from "openclaw/plugin-sdk/compat";
|
|
import { buildChannelConfigSchema } from "../../../src/channels/plugins/config-schema.js";
|
|
import type { ChannelPlugin } from "../../../src/channels/plugins/types.plugin.js";
|
|
import { getChatChannelMeta } from "../../../src/channels/registry.js";
|
|
import type { OpenClawConfig } from "../../../src/config/config.js";
|
|
import { TelegramConfigSchema } from "../../../src/config/zod-schema.providers-core.js";
|
|
import { normalizeAccountId } from "../../../src/routing/session-key.js";
|
|
import { inspectTelegramAccount } from "./account-inspect.js";
|
|
import {
|
|
listTelegramAccountIds,
|
|
resolveDefaultTelegramAccountId,
|
|
resolveTelegramAccount,
|
|
type ResolvedTelegramAccount,
|
|
} from "./accounts.js";
|
|
|
|
export const TELEGRAM_CHANNEL = "telegram" as const;
|
|
|
|
export function findTelegramTokenOwnerAccountId(params: {
|
|
cfg: OpenClawConfig;
|
|
accountId: string;
|
|
}): string | null {
|
|
const normalizedAccountId = normalizeAccountId(params.accountId);
|
|
const tokenOwners = new Map<string, string>();
|
|
for (const id of listTelegramAccountIds(params.cfg)) {
|
|
const account = inspectTelegramAccount({ cfg: params.cfg, accountId: id });
|
|
const token = (account.token ?? "").trim();
|
|
if (!token) {
|
|
continue;
|
|
}
|
|
const ownerAccountId = tokenOwners.get(token);
|
|
if (!ownerAccountId) {
|
|
tokenOwners.set(token, account.accountId);
|
|
continue;
|
|
}
|
|
if (account.accountId === normalizedAccountId) {
|
|
return ownerAccountId;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export function formatDuplicateTelegramTokenReason(params: {
|
|
accountId: string;
|
|
ownerAccountId: string;
|
|
}): string {
|
|
return (
|
|
`Duplicate Telegram bot token: account "${params.accountId}" shares a token with ` +
|
|
`account "${params.ownerAccountId}". Keep one owner account per bot token.`
|
|
);
|
|
}
|
|
|
|
export const telegramConfigAccessors = createScopedAccountConfigAccessors({
|
|
resolveAccount: ({ cfg, accountId }) => resolveTelegramAccount({ cfg, accountId }),
|
|
resolveAllowFrom: (account: ResolvedTelegramAccount) => account.config.allowFrom,
|
|
formatAllowFrom: (allowFrom) =>
|
|
formatAllowFromLowercase({ allowFrom, stripPrefixRe: /^(telegram|tg):/i }),
|
|
resolveDefaultTo: (account: ResolvedTelegramAccount) => account.config.defaultTo,
|
|
});
|
|
|
|
export const telegramConfigBase = createScopedChannelConfigBase<ResolvedTelegramAccount>({
|
|
sectionKey: TELEGRAM_CHANNEL,
|
|
listAccountIds: listTelegramAccountIds,
|
|
resolveAccount: (cfg, accountId) => resolveTelegramAccount({ cfg, accountId }),
|
|
inspectAccount: (cfg, accountId) => inspectTelegramAccount({ cfg, accountId }),
|
|
defaultAccountId: resolveDefaultTelegramAccountId,
|
|
clearBaseFields: ["botToken", "tokenFile", "name"],
|
|
});
|
|
|
|
export function createTelegramPluginBase(params: {
|
|
setupWizard: NonNullable<ChannelPlugin<ResolvedTelegramAccount>["setupWizard"]>;
|
|
setup: NonNullable<ChannelPlugin<ResolvedTelegramAccount>["setup"]>;
|
|
}): Pick<
|
|
ChannelPlugin<ResolvedTelegramAccount>,
|
|
"id" | "meta" | "setupWizard" | "capabilities" | "reload" | "configSchema" | "config" | "setup"
|
|
> {
|
|
return {
|
|
id: TELEGRAM_CHANNEL,
|
|
meta: {
|
|
...getChatChannelMeta(TELEGRAM_CHANNEL),
|
|
quickstartAllowFrom: true,
|
|
},
|
|
setupWizard: params.setupWizard,
|
|
capabilities: {
|
|
chatTypes: ["direct", "group", "channel", "thread"],
|
|
reactions: true,
|
|
threads: true,
|
|
media: true,
|
|
polls: true,
|
|
nativeCommands: true,
|
|
blockStreaming: true,
|
|
},
|
|
reload: { configPrefixes: ["channels.telegram"] },
|
|
configSchema: buildChannelConfigSchema(TelegramConfigSchema),
|
|
config: {
|
|
...telegramConfigBase,
|
|
isConfigured: (account, cfg) => {
|
|
if (!account.token?.trim()) {
|
|
return false;
|
|
}
|
|
return !findTelegramTokenOwnerAccountId({ cfg, accountId: account.accountId });
|
|
},
|
|
unconfiguredReason: (account, cfg) => {
|
|
if (!account.token?.trim()) {
|
|
return "not configured";
|
|
}
|
|
const ownerAccountId = findTelegramTokenOwnerAccountId({
|
|
cfg,
|
|
accountId: account.accountId,
|
|
});
|
|
if (!ownerAccountId) {
|
|
return "not configured";
|
|
}
|
|
return formatDuplicateTelegramTokenReason({
|
|
accountId: account.accountId,
|
|
ownerAccountId,
|
|
});
|
|
},
|
|
describeAccount: (account, cfg) => ({
|
|
accountId: account.accountId,
|
|
name: account.name,
|
|
enabled: account.enabled,
|
|
configured:
|
|
Boolean(account.token?.trim()) &&
|
|
!findTelegramTokenOwnerAccountId({ cfg, accountId: account.accountId }),
|
|
tokenSource: account.tokenSource,
|
|
}),
|
|
...telegramConfigAccessors,
|
|
},
|
|
setup: params.setup,
|
|
};
|
|
}
|