"use strict";exports.id=4578,exports.ids=[4578],exports.modules={54578:(a,b,c)=>{c.a(a,async(a,d)=>{try{c.d(b,{sendTelegram:()=>o,y:()=>r});var e=c(73024),f=c(76760),g=c.n(f),h=c(29163),i=c(17640),j=c(31900),k=c(16909),l=c(74342),m=a([j,k]);[j,k]=m.then?(await m)():m;let B=g().join((0,h._)(),"notify","telegram-state.json");function n(a,b){return`https://api.telegram.org/bot${a}/${b}`}async function o(a,b){let c=[];b.title&&c.push(`*${q(b.title)}*`),c.push(q(b.body)),b.link&&c.push(b.link),await p(a.botToken,a.chatId,c.join("\n\n"))}async function p(a,b,c){(await fetch(n(a,"sendMessage"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({chat_id:b,text:c.slice(0,4e3),parse_mode:"Markdown",disable_web_page_preview:!0}),signal:AbortSignal.timeout(15e3)})).ok||await fetch(n(a,"sendMessage"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({chat_id:b,text:c.slice(0,4e3)}),signal:AbortSignal.timeout(15e3)}).catch(()=>{})}function q(a){return a.replace(/([*_`\[])/g,"\\$1")}function r(){if(globalThis.__reflexTelegramPoller)return;let a={running:!1,stop:!1};globalThis.__reflexTelegramPoller=a,s(a)}async function s(a){if(a.running)return;a.running=!0;let b=await y();for(;!a.stop;){let a=null;try{a=(await (0,i.loadSettings)()).notify?.telegram??null}catch{}if(!a||!a.enabled||!a.botToken){await A(15e3);continue}try{for(let c of(await t(a.botToken,b)))b=c.update_id+1,await z(b),await u(a,c).catch(a=>{console.error("[telegram] handleUpdate:",a instanceof Error?a.message:a)})}catch(a){console.error("[telegram] getUpdates:",a instanceof Error?a.message:a),await A(5e3)}}a.running=!1}async function t(a,b){let c=`${n(a,"getUpdates")}?timeout=30&offset=${b}&allowed_updates=["message"]`,d=await fetch(c,{signal:AbortSignal.timeout(4e4)});if(!d.ok)throw Error(`getUpdates HTTP ${d.status}`);return(await d.json()).result??[]}async function u(a,b){let d=b.message?.text?.trim(),e=b.message?.chat?.id;if(!d||void 0===e||String(e)!==String(a.chatId))return;let{getDispatcherTopic:f}=await c.e(6142).then(c.bind(c,26142)),g=await f(),h=await v(g.rootId,g.rootPath,g.topicId,d);await p(a.botToken,a.chatId,h||"(no reply)")}async function v(a,b,c,d){let e=(await (0,l.readEvents)(b,c)).length,f=await (0,j.startOrchestratorTurn)({rootId:a,topicId:c,message:d,attachments:[]});if("error"in f)return`⚠️ ${f.error}`;let g=Date.now()+24e4;for(await A(400);Date.now()<g&&k.z.isActive(c);)await A(500);return await A(400),(await (0,l.readEvents)(b,c)).slice(e).filter(a=>"assistant-delta"===a.type).map(a=>a.text).join("").trim().replace(/<{1,2}reflex:[a-z-]+>{1,2}[\s\S]*?<{1,2}\/reflex:[a-z-]+>{1,2}/g,"").replace(/\n{3,}/g,"\n\n").trim()}async function w(){try{return JSON.parse(await e.promises.readFile(B,"utf8"))}catch{return{}}}async function x(a){await e.promises.mkdir(g().dirname(B),{recursive:!0}),await e.promises.writeFile(B,JSON.stringify(a,null,2),"utf8")}async function y(){return(await w()).offset??0}async function z(a){let b=await w();await x({...b,offset:a})}function A(a){return new Promise(b=>setTimeout(b,a))}d()}catch(a){d(a)}})}};