telegram: use live runtime helpers in channel status

This commit is contained in:
Jacob Tomlinson
2026-03-27 20:14:12 +00:00
parent 953a438420
commit eb6a3fca26
2 changed files with 53 additions and 28 deletions

View File

@@ -107,7 +107,12 @@ function installGatewayRuntime(params?: { probeOk?: boolean; botUsername?: strin
groups: [],
elapsedMs: 0,
}));
installTelegramRuntime();
installTelegramRuntime({
probeTelegram,
collectTelegramUnmentionedGroupIds: collectUnmentionedGroupIds,
auditTelegramGroupMembership: auditGroupMembership,
monitorTelegramProvider,
});
return {
monitorTelegramProvider,
probeTelegram,
@@ -288,12 +293,7 @@ describe("telegramPlugin duplicate token guard", () => {
});
it("passes account proxy and network settings into Telegram probes", async () => {
const runtimeProbeTelegram = vi.fn(async () => {
throw new Error("runtime probe should not be used");
});
installTelegramRuntime({
probeTelegram: runtimeProbeTelegram,
});
installGatewayRuntime();
probeTelegramMock.mockResolvedValue({
ok: true,
bot: { username: "opsbot" },
@@ -316,20 +316,10 @@ describe("telegramPlugin duplicate token guard", () => {
dnsResultOrder: "ipv4first",
},
});
expect(runtimeProbeTelegram).not.toHaveBeenCalled();
});
it("passes account proxy and network settings into Telegram membership audits", async () => {
const runtimeCollectUnmentionedGroupIds = vi.fn(() => {
throw new Error("runtime audit helper should not be used");
});
const runtimeAuditGroupMembership = vi.fn(async () => {
throw new Error("runtime audit helper should not be used");
});
installTelegramRuntime({
collectUnmentionedGroupIds: runtimeCollectUnmentionedGroupIds,
auditGroupMembership: runtimeAuditGroupMembership,
});
installGatewayRuntime();
collectTelegramUnmentionedGroupIdsMock.mockReturnValue({
groupIds: ["-100123"],
unresolvedGroups: 0,
@@ -373,8 +363,6 @@ describe("telegramPlugin duplicate token guard", () => {
},
timeoutMs: 5000,
});
expect(runtimeCollectUnmentionedGroupIds).not.toHaveBeenCalled();
expect(runtimeAuditGroupMembership).not.toHaveBeenCalled();
});
it("forwards mediaLocalRoots to sendMessageTelegram for outbound media sends", async () => {

View File

@@ -41,7 +41,7 @@ import {
} from "./accounts.js";
import { resolveTelegramAutoThreadId } from "./action-threading.js";
import { buildTelegramExecApprovalButtons } from "./approval-buttons.js";
import { auditTelegramGroupMembership, collectTelegramUnmentionedGroupIds } from "./audit.js";
import * as auditModule from "./audit.js";
import { buildTelegramGroupPeerId } from "./bot/helpers.js";
import {
listTelegramDirectoryGroupsFromConfig,
@@ -59,11 +59,12 @@ import {
resolveTelegramGroupRequireMention,
resolveTelegramGroupToolPolicy,
} from "./group-policy.js";
import { monitorTelegramProvider } from "./monitor.js";
import * as monitorModule from "./monitor.js";
import { looksLikeTelegramTargetId, normalizeTelegramMessagingTarget } from "./normalize.js";
import { sendTelegramPayloadMessages } from "./outbound-adapter.js";
import { parseTelegramReplyToMessageId, parseTelegramThreadId } from "./outbound-params.js";
import { probeTelegram, type TelegramProbe } from "./probe.js";
import * as probeModule from "./probe.js";
import type { TelegramProbe } from "./probe.js";
import { getTelegramRuntime } from "./runtime.js";
import { sendTypingTelegram } from "./send.js";
import { telegramSetupAdapter } from "./setup-core.js";
@@ -83,6 +84,42 @@ type TelegramSendFn = ReturnType<
type TelegramSendOptions = NonNullable<Parameters<TelegramSendFn>[2]>;
type TelegramStatusRuntimeHelpers = {
probeTelegram?: typeof probeModule.probeTelegram;
collectTelegramUnmentionedGroupIds?: typeof auditModule.collectTelegramUnmentionedGroupIds;
auditTelegramGroupMembership?: typeof auditModule.auditTelegramGroupMembership;
monitorTelegramProvider?: typeof monitorModule.monitorTelegramProvider;
};
function getTelegramStatusRuntimeHelpers(): TelegramStatusRuntimeHelpers {
return (getTelegramRuntime().channel?.telegram ?? {}) as TelegramStatusRuntimeHelpers;
}
function resolveTelegramProbe() {
return getTelegramStatusRuntimeHelpers().probeTelegram ?? probeModule.probeTelegram;
}
function resolveTelegramAuditCollector() {
return (
getTelegramStatusRuntimeHelpers().collectTelegramUnmentionedGroupIds ??
auditModule.collectTelegramUnmentionedGroupIds
);
}
function resolveTelegramAuditMembership() {
return (
getTelegramStatusRuntimeHelpers().auditTelegramGroupMembership ??
auditModule.auditTelegramGroupMembership
);
}
function resolveTelegramMonitor() {
return (
getTelegramStatusRuntimeHelpers().monitorTelegramProvider ??
monitorModule.monitorTelegramProvider
);
}
function buildTelegramSendOptions(params: {
cfg: OpenClawConfig;
mediaUrl?: string | null;
@@ -443,7 +480,7 @@ export const telegramPlugin = createChatChannelPlugin({
collectStatusIssues: collectTelegramStatusIssues,
buildChannelSummary: ({ snapshot }) => buildTokenChannelStatusSummary(snapshot),
probeAccount: async ({ account, timeoutMs }) =>
probeTelegram(account.token, timeoutMs, {
resolveTelegramProbe()(account.token, timeoutMs, {
accountId: account.accountId,
proxyUrl: account.config.proxy,
network: account.config.network,
@@ -478,7 +515,7 @@ export const telegramPlugin = createChatChannelPlugin({
cfg.channels?.telegram?.accounts?.[account.accountId]?.groups ??
cfg.channels?.telegram?.groups;
const { groupIds, unresolvedGroups, hasWildcardUnmentionedGroups } =
collectTelegramUnmentionedGroupIds(groups);
resolveTelegramAuditCollector()(groups);
if (!groupIds.length && unresolvedGroups === 0 && !hasWildcardUnmentionedGroups) {
return undefined;
}
@@ -493,7 +530,7 @@ export const telegramPlugin = createChatChannelPlugin({
elapsedMs: 0,
};
}
const audit = await auditTelegramGroupMembership({
const audit = await resolveTelegramAuditMembership()({
token: account.token,
botId,
groupIds,
@@ -559,7 +596,7 @@ export const telegramPlugin = createChatChannelPlugin({
const token = (account.token ?? "").trim();
let telegramBotLabel = "";
try {
const probe = await probeTelegram(token, 2500, {
const probe = await resolveTelegramProbe()(token, 2500, {
accountId: account.accountId,
proxyUrl: account.config.proxy,
network: account.config.network,
@@ -575,7 +612,7 @@ export const telegramPlugin = createChatChannelPlugin({
}
}
ctx.log?.info(`[${account.accountId}] starting provider${telegramBotLabel}`);
return monitorTelegramProvider({
return resolveTelegramMonitor()({
token,
accountId: account.accountId,
config: ctx.cfg,