refactor(telegram): unify username lookup plumbing

This commit is contained in:
Ayaan Zaidi 2026-03-21 10:00:34 +05:30
parent 24473b7dd0
commit 7914ac01ab
No known key found for this signature in database
4 changed files with 27 additions and 10 deletions

View File

@ -11,6 +11,26 @@ export function resolveTelegramChatLookupFetch(params?: {
return resolveTelegramFetch(proxyFetch, { network: params?.network });
}
export async function lookupTelegramChatId(params: {
token: string;
chatId: string;
signal?: AbortSignal;
apiRoot?: string;
proxyUrl?: string;
network?: TelegramNetworkConfig;
}): Promise<string | null> {
return fetchTelegramChatId({
token: params.token,
chatId: params.chatId,
signal: params.signal,
apiRoot: params.apiRoot,
fetchImpl: resolveTelegramChatLookupFetch({
proxyUrl: params.proxyUrl,
network: params.network,
}),
});
}
export async function fetchTelegramChatId(params: {
token: string;
chatId: string;

View File

@ -11,7 +11,7 @@ import type { ChannelSetupAdapter, ChannelSetupDmPolicy } from "openclaw/plugin-
import { formatCliCommand, formatDocsLink } from "openclaw/plugin-sdk/setup-tools";
import type { TelegramNetworkConfig } from "../runtime-api.js";
import { resolveDefaultTelegramAccountId, resolveTelegramAccount } from "./accounts.js";
import { fetchTelegramChatId, resolveTelegramChatLookupFetch } from "./api-fetch.js";
import { lookupTelegramChatId } from "./api-fetch.js";
const channel = "telegram" as const;
@ -51,10 +51,6 @@ export async function resolveTelegramAllowFromEntries(params: {
proxyUrl?: string;
network?: TelegramNetworkConfig;
}) {
const fetchImpl = resolveTelegramChatLookupFetch({
proxyUrl: params.proxyUrl,
network: params.network,
});
return await Promise.all(
params.entries.map(async (entry) => {
const numericId = parseTelegramAllowFromId(entry);
@ -66,11 +62,12 @@ export async function resolveTelegramAllowFromEntries(params: {
return { input: entry, resolved: false, id: null };
}
const username = stripped.startsWith("@") ? stripped : `@${stripped}`;
const id = await fetchTelegramChatId({
const id = await lookupTelegramChatId({
token: params.credentialValue,
chatId: username,
apiRoot: params.apiRoot,
fetchImpl,
proxyUrl: params.proxyUrl,
network: params.network,
});
return { input: entry, resolved: Boolean(id), id };
}),

View File

@ -4,8 +4,8 @@ import {
listTelegramAccountIds,
lookupTelegramChatId,
normalizeTelegramAllowFromEntry,
} from "../../extensions/telegram/api.js";
import type { TelegramNetworkConfig } from "../config/types.telegram.js";
} from "openclaw/plugin-sdk/telegram";
import type { TelegramNetworkConfig } from "openclaw/plugin-sdk/telegram";
import { normalizeChatChannelId } from "../channels/registry.js";
import { formatCliCommand } from "../cli/command-format.js";
import { resolveCommandSecretRefsViaGateway } from "../cli/command-secret-gateway.js";

View File

@ -82,7 +82,7 @@ export {
isNumericTelegramUserId,
normalizeTelegramAllowFromEntry,
} from "../../../extensions/telegram/api.js";
export { fetchTelegramChatId } from "../../../extensions/telegram/api.js";
export { fetchTelegramChatId, lookupTelegramChatId } from "../../../extensions/telegram/api.js";
export {
resolveTelegramInlineButtonsScope,
resolveTelegramTargetChatType,