openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
351 lines (350 loc) • 11.1 kB
JavaScript
import { r as createLazyRuntimeModule } from "./lazy-runtime-zGjszCtt.mjs";
import { h as readRuntimeConfig } from "./runtime-snapshot-WbwbY5b7.mjs";
import { r as logVerbose } from "./globals-CgepmiqE.mjs";
import "./runtime-config-snapshot-Dy0VFqWf.mjs";
import "./runtime-env-D-Pa5lD6.mjs";
import { t as readChannelAllowFromStore } from "./pairing-store.read-5Rowoyv-.mjs";
import "./conversation-runtime-CefOz30L.mjs";
import { i as resolveTelegramAccount } from "./accounts-C26vVqsr.mjs";
import { n as withTelegramApiErrorLogging } from "./api-logging-D-u3hsql.mjs";
import { B as resolveTelegramDmAllow, U as resolveTelegramEffectiveDmPolicy, _ as resolveTelegramGroupAllowFromContext, c as buildTelegramThreadParams, d as extractTelegramForumFlag, h as resolveTelegramForumFlag, m as resolveTelegramBotHasTopicsEnabled, y as resolveTelegramMessageThreadSpec } from "./helpers-CoGccTxe.mjs";
import { n as getTelegramRuntime } from "./runtime-Gy4vBjmB.mjs";
import { M as resolveTelegramTableMode, j as resolveTelegramRichMessages } from "./text-chunk-limit-DTiKmKhG.mjs";
import { d as touchTelegramConversationRoute, o as inspectTelegramConversationRouteAsync, u as resolveTelegramTargetSession } from "./outbound-adapter-xqQwShsd.mjs";
import { J as evaluateTelegramGroupPolicyAccess, q as evaluateTelegramGroupBaseAccess } from "./send-CkT6jkYo.mjs";
import { c as createTelegramIngressResolver, l as resolveTelegramCommandIngressAuthorization, m as telegramConversation } from "./progress-draft-preview-CFzpew0W.mjs";
import { n as resolveTelegramMessageTurnSettings } from "./bot-message-_DVnRpjY.mjs";
//#region extensions/telegram/src/bot-native-command-dispatch.ts
const loadTelegramNativeCommandDeliveryRuntime = createLazyRuntimeModule(() => import("./delivery-B8AZnTBr.mjs"));
const loadTelegramNativeCommandRuntime = createLazyRuntimeModule(async () => {
const { ensureConfiguredBindingRouteReady: e, getAgentScopedMediaLocalRoots: t, getSessionEntryAsync: n, recordDeliveredCommandExchange: r, resolveChunkMode: i, resolveStorePath: a } = await import("./bot-native-commands.runtime.js");
return {
ensureConfiguredBindingRouteReady: e,
getAgentScopedMediaLocalRoots: t,
getSessionEntryAsync: n,
recordDeliveredCommandExchange: r,
resolveChunkMode: i,
resolveStorePath: a
};
});
async function resolveTelegramNativeCommandThreadContext(e) {
const { msg: t, bot: n } = e;
const r = t.chat.id;
const i = t.chat.type === "group" || t.chat.type === "supergroup";
const a = t.chat.is_direct_messages === true ? false : await resolveTelegramForumFlag({
chatId: r,
chatType: t.chat.type,
isGroup: i,
isForum: extractTelegramForumFlag(t.chat),
isTopicMessage: t.is_topic_message,
getChat: (e) => n.api.getChat(e)
});
const o = resolveTelegramMessageThreadSpec(t, a);
return {
chatId: r,
isGroup: i,
threadSpec: o,
threadParams: buildTelegramThreadParams(o)
};
}
async function resolveTelegramCommandAuth(e) {
const { msg: t, bot: n, cfg: r, accountId: i, telegramCfg: a, requireAuth: o } = e;
const { chatId: s, isGroup: c, threadSpec: l, threadParams: u } = await resolveTelegramNativeCommandThreadContext({
msg: t,
bot: n
});
const d = t.from?.id ? String(t.from.id) : "";
const f = e.resolveTelegramGroupConfig(s, l.id, r);
const p = await inspectTelegramConversationRouteAsync({
cfg: r,
accountId: i,
chatId: s,
isGroup: c,
threadSpec: l,
senderId: d,
topicAgentId: f.topicConfig?.agentId
});
const { route: m } = p;
const h = resolveTelegramTargetSession({
cfg: r,
route: m,
bindingMode: p.bindingMode,
chatId: s,
isGroup: c,
senderId: d,
dmThreadId: l.scope === "dm" ? l.id : void 0,
botHasTopicsEnabled: resolveTelegramBotHasTopicsEnabled(e.botUser)
});
const g = {
cfg: r,
accountId: i,
chatId: s,
isGroup: c,
senderId: d,
dmPolicy: a.dmPolicy ?? "pairing"
};
const _ = l.id;
const v = m.agentId;
const y = String(t.message_id);
const b = t.text ?? "";
const x = telegramConversation({
...g,
resolvedThreadId: _
});
const S = await createTelegramIngressResolver({
accountId: i,
cfg: r,
useDefaultPairingStore: false
}).event({
subject: { stableId: d },
conversation: x,
contextBinding: {
agentId: v,
sessionKey: h,
messageId: y,
inboundEventKind: "user_request"
},
event: {
kind: "native-command",
authMode: "none",
mayPair: false
},
dmPolicy: g.dmPolicy,
groupPolicy: "allowlist",
command: false
});
const C = getTelegramRuntime().channel.inbound.buildContext({
channel: "telegram",
accountId: i,
channelIngress: S,
messageId: y,
from: c ? `telegram:group:${s}` : `telegram:${s}`,
sender: { id: d },
conversation: x,
route: {
agentId: v,
routeSessionKey: h
},
reply: {
to: `telegram:${s}`,
messageThreadId: _
},
message: {
rawBody: b,
inboundEventKind: "user_request"
}
});
const w = await resolveTelegramCommandIngressAuthorization({
...g,
ownerContext: C
});
const T = await resolveTelegramGroupAllowFromContext({
cfg: r,
chatId: s,
accountId: i,
dmPolicy: a.dmPolicy,
allowFrom: e.allowFrom,
senderId: d,
isGroup: c,
threadSpec: l,
groupAllowFrom: e.groupAllowFrom,
skipPairingStoreRead: w.authorizedByConfig,
readChannelAllowFromStore,
resolveTelegramGroupConfig: () => f
});
const { resolvedThreadId: E, dmThreadId: D, storeAllowFrom: O, groupConfig: k, groupAllowOverride: A, effectiveGroupAllow: j } = T;
const M = resolveTelegramEffectiveDmPolicy({
isGroup: c,
groupConfig: k,
dmPolicy: a.dmPolicy
});
const N = !c && k && "requireTopic" in k ? k.requireTopic : void 0;
if (!c && N === true && D == null) {
logVerbose(`Blocked telegram command in DM ${s}: requireTopic=true but no topic present`);
return null;
}
const rejectNotAuthorized = async () => {
await withTelegramApiErrorLogging({
operation: "sendMessage",
fn: () => n.api.sendMessage(s, "You are not authorized to use this command.", u ?? {})
});
return null;
};
const P = evaluateTelegramGroupBaseAccess({
...T,
senderId: d,
enforceAllowOverride: o,
requireSenderForAllowOverride: true
});
if (!P.allowed) {
if (P.reason === "group-disabled") {
logVerbose(`Blocked telegram command in group ${s} (group disabled)`);
return null;
}
if (P.reason === "topic-disabled") {
logVerbose(`Blocked telegram command in topic ${s} (${E ?? "unknown"}) (topic disabled)`);
return null;
}
return await rejectNotAuthorized();
}
const F = evaluateTelegramGroupPolicyAccess({
...T,
isGroup: c,
chatId: s,
cfg: r,
telegramCfg: a,
senderId: d,
resolveGroupPolicy: e.resolveGroupPolicy,
enforceAllowlistAuthorization: o && !w.authorizedByConfig,
allowEmptyAllowlistEntries: true
});
if (!F.allowed) {
if (F.reason === "group-policy-disabled") {
logVerbose("Blocked telegram command (groupPolicy: disabled)");
return null;
}
if (F.reason === "group-policy-allowlist-no-sender" || F.reason === "group-policy-allowlist-unauthorized") return await rejectNotAuthorized();
if (F.reason === "group-chat-not-allowed") {
logVerbose(`Blocked telegram command in group ${s} (group not allowed)`);
return null;
}
}
const { effectiveAllow: I } = await resolveTelegramDmAllow({
cfg: r,
allowFrom: A ?? e.allowFrom,
accountId: i,
senderId: d,
storeAllowFrom: c ? [] : O,
dmPolicy: M
});
const { authorized: L, senderIsOwner: R, assertOwnerCurrent: z } = await resolveTelegramCommandIngressAuthorization({
...g,
dmPolicy: M,
resolvedThreadId: E,
effectiveDmAllow: I,
effectiveGroupAllow: j,
eventKind: "native-command",
ownerContext: C
});
if (o && !L) return await rejectNotAuthorized();
return {
chatId: s,
isGroup: c,
senderId: d,
threadSpec: l,
threadParams: u,
commandAuthorized: L,
senderIsOwner: R,
assertOwnerCurrent: z,
route: m,
targetSessionKey: h,
inspectedRoute: p,
ownerContext: C
};
}
async function prepareTelegramCommandDispatch(e) {
const t = readRuntimeConfig();
const n = resolveTelegramAccount({
cfg: t,
accountId: e.accountId
}).config;
const r = resolveTelegramMessageTurnSettings({
accountId: e.accountId,
cfg: t,
telegramCfg: n,
opts: e.opts
});
const i = await resolveTelegramCommandAuth({
...e,
cfg: t,
telegramCfg: n,
allowFrom: r.allowFrom,
groupAllowFrom: r.groupAllowFrom
});
if (!i) return null;
const { route: a, bindingMode: o } = i.inspectedRoute;
const s = await loadTelegramNativeCommandRuntime();
i.assertOwnerCurrent?.();
await touchTelegramConversationRoute(i.inspectedRoute);
i.assertOwnerCurrent?.();
if (o.kind === "configured") {
i.assertOwnerCurrent?.();
const n = await s.ensureConfiguredBindingRouteReady({
cfg: t,
bindingResolution: o.binding,
assertActive: i.assertOwnerCurrent
});
if (!n.ok) {
logVerbose(`telegram native command: configured ACP binding unavailable for topic ${o.binding.record.conversation.conversationId}: ${n.error}`);
await withTelegramApiErrorLogging({
operation: "sendMessage",
runtime: e.runtime,
fn: () => e.bot.api.sendMessage(i.chatId, "Configured ACP binding is unavailable right now. Please try again.", i.threadParams ?? {})
});
return null;
}
}
const c = s.getAgentScopedMediaLocalRoots(t, a.agentId);
const l = {
cfg: t,
accountId: a.accountId,
accountConfig: n
};
const u = resolveTelegramRichMessages(l);
const d = resolveTelegramTableMode(l);
const f = s.resolveChunkMode(t, "telegram", a.accountId);
const p = {
cfg: t,
ownerAgentId: e.opts.ownerAgentId,
chatId: String(i.chatId),
accountId: a.accountId,
sessionKeyForInternalHooks: i.targetSessionKey,
policySessionKey: i.targetSessionKey,
mirrorIsGroup: i.isGroup,
mirrorGroupId: i.isGroup ? String(i.chatId) : void 0,
token: e.opts.token,
runtime: e.runtime,
bot: e.bot,
mediaLocalRoots: c,
mediaMaxBytes: e.mediaMaxBytes,
replyToMode: r.replyToMode,
textLimit: r.textLimit,
thread: i.threadSpec,
tableMode: d,
chunkMode: f,
linkPreview: n.linkPreview,
richMessages: u
};
const m = s.resolveStorePath(t.session?.store, { agentId: a.agentId });
let h = (await s.getSessionEntryAsync({
agentId: a.agentId,
sessionKey: i.targetSessionKey,
storePath: m
}))?.sessionId;
return {
...e,
runtimeCfg: t,
runtimeTelegramCfg: n,
...i,
nativeCommandRuntime: s,
deliveryOptions: p,
loadDeliveryRuntime: loadTelegramNativeCommandDeliveryRuntime,
recordDeliveredReply: async (n, r, o) => {
const c = await s.recordDeliveredCommandExchange({
config: t,
agentId: a.agentId,
sessionKey: i.targetSessionKey,
expectedSessionId: h,
assertCurrent: i.assertOwnerCurrent,
commandText: n,
replyText: r,
commandId: `telegram:${a.accountId}:${i.chatId}:${e.msg.message_id}`,
replyId: o
});
if (c.ok) h = c.target.sessionId;
else logVerbose(`telegram command transcript skipped: ${c.reason}`);
}
};
}
//#endregion
export { resolveTelegramNativeCommandThreadContext as n, prepareTelegramCommandDispatch as t };