openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
4 lines • 3.02 kB
JavaScript
import{t as formatErrorMessage}from"./errors-D37BJTfI.mjs";import{n as createRuntimeConfigReader}from"./runtime-snapshot-Bg5K4JNu.mjs";import{t as getSessionBindingService}from"./session-binding-service-TO5btU7E.mjs";import"./runtime-config-snapshot-N0DER8v5.mjs";import"./error-runtime-DW-sj0iq.mjs";import{a as mergeTelegramAccountConfig,n as listTelegramAccountIds}from"./account-selection-CSIzQZif.mjs";import"./session-binding-runtime-I5DrGzBm.mjs";import"./accounts-CHjEWHkL.mjs";import{n as parseTelegramTopicConversation}from"./topic-conversation-C3o7-j7e.mjs";import{t as loadTelegramSendModule}from"./send-runtime-D4dB3iVP.mjs";import{isDeepStrictEqual}from"node:util";
//!extensions/telegram/src/conversation-fork.ts
function topicUrl(e,t){const n=/^-100([1-9]\d*)$/.exec(e)?.[1];return n?`https://t.me/c/`+n+`/`+t:void 0}const prepareTelegramConversationFork=async e=>{const t=parseTelegramTopicConversation(e.source);if(!e.isGroup||!t||t.thread.scope!==`forum`||!/^-100[1-9]\d*$/.test(t.chatId))return{ok:false,reason:`/fork requires a Telegram forum topic, not a DM or an unthreaded group.`};const n=e.title??`Forked conversation`;if(Array.from(n).length>128)return{ok:false,reason:`Telegram topic titles must be at most 128 characters.`};const r=createRuntimeConfigReader(e.cfg);const i=r();const a=mergeTelegramAccountConfig(i,e.accountId);const assertCurrent=()=>{e.assertCurrent();e.signal?.throwIfAborted();const t=r();if(t.channels?.telegram?.enabled===false||!listTelegramAccountIds(t).includes(e.accountId)||!isDeepStrictEqual(mergeTelegramAccountConfig(t,e.accountId),a)||a.enabled===false)throw new Error(`Telegram account or policy changed; send a new /fork request.`)};assertCurrent();const o=getSessionBindingService();const s=o.getCapabilities({channel:`telegram`,accountId:e.accountId});if(!s.bindSupported||!s.placements.includes(`current`))return{ok:false,reason:`Telegram thread bindings are unavailable. Enable thread bindings for this account before using /fork.`};assertCurrent();return{ok:true,assertCurrent,formatRecap:e=>{const t=Math.max(0,...(e.match(/`+/g)??[]).map(e=>e.length));const n="`".repeat(Math.max(3,t+1));return{text:[n,e,n].join(`
`),channelData:{telegram:{linkPreview:false}}}},create:async r=>{const{createForumTopicTelegram:a}=await loadTelegramSendModule();e.signal?.throwIfAborted();assertCurrent();const s=await a(t.chatId,n,{cfg:i,accountId:e.accountId,assertPlatformSendAuthorized:assertCurrent});const c=s.chatId+`:topic:`+s.topicId;const l=topicUrl(s.chatId,s.topicId);try{e.signal?.throwIfAborted();assertCurrent();return{binding:await o.bind({targetSessionKey:r,targetKind:`session`,placement:`current`,conversation:{channel:`telegram`,accountId:e.accountId,conversationId:c,parentConversationId:s.chatId},metadata:{threadName:n,boundBy:e.senderId},assertCurrent}),url:l}}catch(e){throw new Error(`Telegram topic `+(l??c)+` was created, but its session binding was not confirmed: `+formatErrorMessage(e),{cause:e})}}}};export{prepareTelegramConversationFork};