mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-26 01:11:37 +00:00
* feat(telegram): support custom apiRoot for alternative API endpoints Add `apiRoot` config option to allow users to specify custom Telegram Bot API endpoints (e.g., self-hosted Bot API servers). Threads the configured base URL through all Telegram API call sites: bot creation, send, probe, audit, media download, and api-fetch. Extends SSRF policy to dynamically trust custom apiRoot hostname for media downloads. Closes #28535 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix(telegram): thread apiRoot through allowFrom lookups * fix(telegram): honor lookup transport and local file paths * refactor(telegram): unify username lookup plumbing * fix(telegram): restore doctor lookup imports * fix: document Telegram apiRoot support (#48842) (thanks @Cypherm) --------- Co-authored-by: Cypherm <28184436+Cypherm@users.noreply.github.com> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Co-authored-by: Ayaan Zaidi <hi@obviy.us>
109 lines
2.9 KiB
TypeScript
109 lines
2.9 KiB
TypeScript
import type { TelegramGroupConfig } from "openclaw/plugin-sdk/config-runtime";
|
|
import type { TelegramNetworkConfig } from "openclaw/plugin-sdk/config-runtime";
|
|
|
|
export type TelegramGroupMembershipAuditEntry = {
|
|
chatId: string;
|
|
ok: boolean;
|
|
status?: string | null;
|
|
error?: string | null;
|
|
matchKey?: string;
|
|
matchSource?: "id";
|
|
};
|
|
|
|
export type TelegramGroupMembershipAudit = {
|
|
ok: boolean;
|
|
checkedGroups: number;
|
|
unresolvedGroups: number;
|
|
hasWildcardUnmentionedGroups: boolean;
|
|
groups: TelegramGroupMembershipAuditEntry[];
|
|
elapsedMs: number;
|
|
};
|
|
|
|
export function collectTelegramUnmentionedGroupIds(
|
|
groups: Record<string, TelegramGroupConfig> | undefined,
|
|
) {
|
|
if (!groups || typeof groups !== "object") {
|
|
return {
|
|
groupIds: [] as string[],
|
|
unresolvedGroups: 0,
|
|
hasWildcardUnmentionedGroups: false,
|
|
};
|
|
}
|
|
const hasWildcardUnmentionedGroups =
|
|
Boolean(groups["*"]?.requireMention === false) && groups["*"]?.enabled !== false;
|
|
const groupIds: string[] = [];
|
|
let unresolvedGroups = 0;
|
|
for (const [key, value] of Object.entries(groups)) {
|
|
if (key === "*") {
|
|
continue;
|
|
}
|
|
if (!value || typeof value !== "object") {
|
|
continue;
|
|
}
|
|
if (value.enabled === false) {
|
|
continue;
|
|
}
|
|
if (value.requireMention !== false) {
|
|
continue;
|
|
}
|
|
const id = String(key).trim();
|
|
if (!id) {
|
|
continue;
|
|
}
|
|
if (/^-?\d+$/.test(id)) {
|
|
groupIds.push(id);
|
|
} else {
|
|
unresolvedGroups += 1;
|
|
}
|
|
}
|
|
groupIds.sort((a, b) => a.localeCompare(b));
|
|
return { groupIds, unresolvedGroups, hasWildcardUnmentionedGroups };
|
|
}
|
|
|
|
export type AuditTelegramGroupMembershipParams = {
|
|
token: string;
|
|
botId: number;
|
|
groupIds: string[];
|
|
proxyUrl?: string;
|
|
network?: TelegramNetworkConfig;
|
|
apiRoot?: string;
|
|
timeoutMs: number;
|
|
};
|
|
|
|
let auditMembershipRuntimePromise: Promise<typeof import("./audit-membership-runtime.js")> | null =
|
|
null;
|
|
|
|
function loadAuditMembershipRuntime() {
|
|
auditMembershipRuntimePromise ??= import("./audit-membership-runtime.js");
|
|
return auditMembershipRuntimePromise;
|
|
}
|
|
|
|
export async function auditTelegramGroupMembership(
|
|
params: AuditTelegramGroupMembershipParams,
|
|
): Promise<TelegramGroupMembershipAudit> {
|
|
const started = Date.now();
|
|
const token = params.token?.trim() ?? "";
|
|
if (!token || params.groupIds.length === 0) {
|
|
return {
|
|
ok: true,
|
|
checkedGroups: 0,
|
|
unresolvedGroups: 0,
|
|
hasWildcardUnmentionedGroups: false,
|
|
groups: [],
|
|
elapsedMs: Date.now() - started,
|
|
};
|
|
}
|
|
|
|
// Lazy import to avoid pulling `undici` (ProxyAgent) into cold-path callers that only need
|
|
// `collectTelegramUnmentionedGroupIds` (e.g. config audits).
|
|
const { auditTelegramGroupMembershipImpl } = await loadAuditMembershipRuntime();
|
|
const result = await auditTelegramGroupMembershipImpl({
|
|
...params,
|
|
token,
|
|
});
|
|
return {
|
|
...result,
|
|
elapsedMs: Date.now() - started,
|
|
};
|
|
}
|