openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 9.7 kB
JavaScript
import{r as createLazyRuntimeModule}from"./lazy-runtime-Ds6M7vp4.mjs";import{h as readRuntimeConfig}from"./runtime-snapshot-Bg5K4JNu.mjs";import{r as logVerbose}from"./globals-l9I53ZKA.mjs";import"./runtime-config-snapshot-N0DER8v5.mjs";import"./runtime-env-Qb8DG6sI.mjs";import{t as readChannelAllowFromStore}from"./pairing-store.read-MEshDJIA.mjs";import"./conversation-runtime-DYBWlVcc.mjs";import{i as resolveTelegramAccount}from"./accounts-CHjEWHkL.mjs";import{W as resolveTelegramDmAllow,X as withTelegramApiErrorLogging,_ as resolveTelegramGroupAllowFromContext,c as buildTelegramThreadParams,d as extractTelegramForumFlag,h as resolveTelegramForumFlag,m as resolveTelegramBotHasTopicsEnabled,q as resolveTelegramEffectiveDmPolicy,y as resolveTelegramMessageThreadSpec}from"./helpers-B2EnHAZw.mjs";import{n as getTelegramRuntime}from"./runtime-DSg1Eeoq.mjs";import{M as resolveTelegramTableMode,j as resolveTelegramRichMessages}from"./text-chunk-limit-YsOTt7TW.mjs";import{c as inspectTelegramConversationRouteAsync,f as resolveTelegramTargetSession,p as touchTelegramConversationRoute}from"./outbound-adapter-SWXqhFMV.mjs";import{J as evaluateTelegramGroupBaseAccess,Y as evaluateTelegramGroupPolicyAccess}from"./send-D3A72aGt.mjs";import{c as createTelegramIngressResolver,l as resolveTelegramCommandIngressAuthorization,m as telegramConversation}from"./progress-draft-preview-B7C4VdHl.mjs";import{n as resolveTelegramMessageTurnSettings}from"./bot-message-CGdHkrBN.mjs";
//!extensions/telegram/src/bot-native-command-dispatch.ts
const loadTelegramNativeCommandDeliveryRuntime=createLazyRuntimeModule(()=>import(`./delivery-DE2ZTn-I.mjs`));const loadTelegramNativeCommandRuntime=createLazyRuntimeModule(async()=>{const{ensureConfiguredBindingRouteReady:e,getAgentScopedMediaLocalRoots:t,getSessionEntryAsync:n,recordDeliveredCommandExchange:r,resolveChunkMode:i,resolveStorePath:a}=await import(`./bot-native-commands.runtime-lRToj3PW.mjs`);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{effectiveAllow:P}=await resolveTelegramDmAllow({cfg:r,allowFrom:A??e.allowFrom,accountId:i,senderId:d,storeAllowFrom:c?[]:O,dmPolicy:M});const F=!c&&!w.senderIsOwner&&!w.authorizedByConfig&&(M===`disabled`||M===`allowlist`&&!P.hasWildcard&&!P.entries.includes(d));const rejectNotAuthorized=async()=>{if(F){logVerbose(`Dropped telegram native command in DM ${s} (sender not admitted)`);return null}await withTelegramApiErrorLogging({operation:`sendMessage`,fn:()=>n.api.sendMessage(s,`You are not authorized to use this command.`,u??{})});return null};const I=evaluateTelegramGroupBaseAccess({...T,senderId:d,enforceAllowOverride:o,requireSenderForAllowOverride:true});if(!I.allowed){if(I.reason===`group-disabled`){logVerbose(`Blocked telegram command in group ${s} (group disabled)`);return null}if(I.reason===`topic-disabled`){logVerbose(`Blocked telegram command in topic ${s} (${E??`unknown`}) (topic disabled)`);return null}return await rejectNotAuthorized()}const L=evaluateTelegramGroupPolicyAccess({...T,isGroup:c,chatId:s,cfg:r,telegramCfg:a,senderId:d,resolveGroupPolicy:e.resolveGroupPolicy,enforceAllowlistAuthorization:o&&!w.authorizedByConfig,allowEmptyAllowlistEntries:true});if(!L.allowed){if(L.reason===`group-policy-disabled`){logVerbose(`Blocked telegram command (groupPolicy: disabled)`);return null}if(L.reason===`group-policy-allowlist-no-sender`||L.reason===`group-policy-allowlist-unauthorized`)return await rejectNotAuthorized();if(L.reason===`group-chat-not-allowed`){logVerbose(`Blocked telegram command in group ${s} (group not allowed)`);return null}}const{authorized:R,senderIsOwner:z,assertOwnerCurrent:B}=await resolveTelegramCommandIngressAuthorization({...g,dmPolicy:M,resolvedThreadId:E,effectiveDmAllow:P,effectiveGroupAllow:j,eventKind:`native-command`,ownerContext:C});if(o&&!R)return await rejectNotAuthorized();return{chatId:s,isGroup:c,senderId:d,threadSpec:l,threadParams:u,storeAllowFrom:O,commandAuthorized:R,senderIsOwner:z,assertOwnerCurrent:B,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}`)}}}export{resolveTelegramNativeCommandThreadContext as n,prepareTelegramCommandDispatch as t};