mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 05:20:43 +00:00
51 lines
1.9 KiB
TypeScript
51 lines
1.9 KiB
TypeScript
import { normalizeAccountId } from "openclaw/plugin-sdk/account-core";
|
|
import { mapAllowFromEntries } from "openclaw/plugin-sdk/channel-config-helpers";
|
|
import type { OpenClawConfig, TelegramAccountConfig } from "openclaw/plugin-sdk/config-runtime";
|
|
import { createResolvedDirectoryEntriesLister } from "openclaw/plugin-sdk/directory-runtime";
|
|
import { mergeTelegramAccountConfig } from "./account-config.js";
|
|
import { resolveDefaultTelegramAccountSelection } from "./account-selection.js";
|
|
|
|
type TelegramDirectoryAccount = {
|
|
config: TelegramAccountConfig;
|
|
};
|
|
|
|
function resolveTelegramDirectoryAccount(
|
|
cfg: OpenClawConfig,
|
|
accountId?: string | null,
|
|
): TelegramDirectoryAccount {
|
|
const resolvedAccountId = accountId?.trim()
|
|
? normalizeAccountId(accountId)
|
|
: resolveDefaultTelegramAccountSelection(cfg).accountId;
|
|
return {
|
|
config: mergeTelegramAccountConfig(cfg, resolvedAccountId),
|
|
};
|
|
}
|
|
|
|
export const listTelegramDirectoryPeersFromConfig =
|
|
createResolvedDirectoryEntriesLister<TelegramDirectoryAccount>({
|
|
kind: "user",
|
|
resolveAccount: (cfg, accountId) => resolveTelegramDirectoryAccount(cfg, accountId),
|
|
resolveSources: (account) => [
|
|
mapAllowFromEntries(account.config.allowFrom),
|
|
Object.keys(account.config.dms ?? {}),
|
|
],
|
|
normalizeId: (entry) => {
|
|
const trimmed = entry.replace(/^(telegram|tg):/i, "").trim();
|
|
if (!trimmed) {
|
|
return null;
|
|
}
|
|
if (/^-?\d+$/.test(trimmed)) {
|
|
return trimmed;
|
|
}
|
|
return trimmed.startsWith("@") ? trimmed : `@${trimmed}`;
|
|
},
|
|
});
|
|
|
|
export const listTelegramDirectoryGroupsFromConfig =
|
|
createResolvedDirectoryEntriesLister<TelegramDirectoryAccount>({
|
|
kind: "group",
|
|
resolveAccount: (cfg, accountId) => resolveTelegramDirectoryAccount(cfg, accountId),
|
|
resolveSources: (account) => [Object.keys(account.config.groups ?? {})],
|
|
normalizeId: (entry) => entry.trim() || null,
|
|
});
|