gramstax 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1,7 @@
1
- "use strict";var G=Object.create;var v=Object.defineProperty;var H=Object.getOwnPropertyDescriptor;var z=Object.getOwnPropertyNames;var q=Object.getPrototypeOf,X=Object.prototype.hasOwnProperty;var h=(u,t)=>v(u,"name",{value:t,configurable:!0});var Y=(u,t)=>{for(var e in t)v(u,e,{get:t[e],enumerable:!0})},A=(u,t,e,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of z(t))!X.call(u,s)&&s!==e&&v(u,s,{get:()=>t[s],enumerable:!(i=H(t,s))||i.enumerable});return u};var N=(u,t,e)=>(e=u!=null?G(q(u)):{},A(t||!u||!u.__esModule?v(e,"default",{value:u,enumerable:!0}):e,u)),Z=u=>A(v({},"__esModule",{value:!0}),u);var et={};Y(et,{CacheExternal:()=>I,Ctx:()=>x,Gramstax:()=>D,LoggingUtils:()=>T,TemplateEngine:()=>f,TemplateManager:()=>k,log:()=>tt});module.exports=Z(et);var K=N(require("logging-pretty"),1);var T=class extends K.default{static{h(this,"LoggingUtils")}constructor(t){super(t)}errorMake(t,e,i){this.error(`[${e?.name||e}:${i}] ${String(t)}`)}},tt=new T;var B=require("keyv"),$=N(require("@keyv/redis"),1);var I=class extends B.Keyv{constructor(e="memory",i,s,o){let n;e.startsWith("redis")&&(n=new $.default(e)),e==="memory"&&(n=new Map),o?.forceStore&&(n=o.forceStore);super({store:n,namespace:i,ttl:s});this.url=e}static{h(this,"CacheExternal")}};var E=require("timers/promises"),S=require("grammy");var x=class{static{h(this,"Ctx")}static template;static data;data=this.constructor.data;get userIsPremium(){return this.ct.from?.is_premium??!1}get userIsBot(){return this.ct.from?.is_bot??!1}get userId(){return this.ct.from?.id}get userName(){return this.ct.from?.username}get userFullName(){return`${this.ct.from?.first_name}${this.ct.from?.last_name}`}get userFirstName(){return this.ct.from?.first_name}get userLastName(){return this.ct.from?.last_name}get userLanguage(){return this.ct.from?.language_code}set userLanguage(t){this.ct.from.language_code=t}get msgText(){return this.ct.message?.text}get msgTextIntent(){return this.msgText&&this.msgText.indexOf(" ")!=-1?this.msgText.split(" ")[0]:""}get msgTextPayloads(){return this.msgText&&this.msgText.indexOf(" ")!=-1?this.msgText.split(" ").slice(1):[]}get msgTextPayload(){return this.msgTextPayloads[0]}get msgCaption(){return this.ct.message?.caption}get msgCaptionIntent(){return this.msgCaption&&this.msgCaption.indexOf(" ")!=-1?this.msgCaption.split(" ")[0]:""}get msgPhoto(){return this.ct.message?.photo}get msgVideo(){return this.ct.message?.video}get msgAudio(){return this.ct.message?.audio}get callbackData(){return this.ct.callbackQuery?.data}get callbackDataIntent(){return this.callbackData&&this.callbackData.indexOf(":")!=-1?this.callbackData.split(":")[0]:""}get callbackDataPayloads(){return this.callbackData&&this.callbackData.indexOf(":")!=-1?this.callbackData.split(":").slice(1):[]}get callbackDataPayload(){return this.callbackDataPayloads?.[0]}get callbackDataParts(){return this.callbackDataPayload&&this.callbackDataPayload.indexOf("+")!=-1?this.callbackDataPayload.split("+"):[]}set session(t){this.temp.session=t}get session(){return this.temp.session}ct;cacheSession;cacheKeyboard;templateManager;temp;constructor(t){this.ct=t.ct,this.cacheSession=t.cacheSession,this.cacheKeyboard=t.cacheKeyboard,this.templateManager=t.templateManager,this.temp=t.temp}async broadcast(t,e,i,s,o,n,a=!1){try{let d=e.map(c=>this.templateManager.getMessage(this.data.name,o,c,s));for(let c=0;c<t.length;c++){let l=t[c],p=d[c];await this.ct.api.sendMessage(l,p,{parse_mode:"HTML",reply_markup:i,...n}),c+1==30&&await(0,E.setTimeout)(1200)}}catch(r){if(a)throw r}}async reply(t,e,i,s){let o=this.templateManager.getMessage(this.data.name,i,this.userLanguage,e);return await this.ct.reply(o,{parse_mode:"HTML",reply_markup:t,...s})}async replyMedia(t,e,i,s,o){let n=this.templateManager.getMedia(this.data.name,s,this.userLanguage,i),a={parse_mode:"HTML",reply_markup:e,...o};if(t=="photo")return await this.ct.replyWithPhoto(n.src,{caption:n.caption,...a});if(t=="video")return await this.ct.replyWithVideo(n.src,{caption:n.caption,...a});if(t=="audio")return await this.ct.replyWithAudio(n.src,{caption:n.caption,...a});if(t=="document")return await this.ct.replyWithDocument(n.src,{caption:n.caption,...a});if(t=="animation")return await this.ct.replyWithAnimation(n.src,{caption:n.caption,...a});if(t=="voice")return await this.ct.replyWithVoice(n.src,a);if(t=="video_note")return await this.ct.replyWithVideoNote(n.src,a);if(t=="sticker")return await this.ct.replyWithSticker(n.src,a);if(t=="location")return await this.ct.replyWithLocation(parseInt(n.latitude),parseInt(n.longitude),a);if(t=="contact")return await this.ct.replyWithContact(n.phone,n.first_name,{vcard:n.vcard,...a})}async replyPhoto(t,e,i,s){return this.replyMedia("photo",t,e,i,s)}async replyVideo(t,e,i,s){return this.replyMedia("video",t,e,i,s)}async replyAudio(t,e,i,s){return this.replyMedia("audio",t,e,i,s)}async replyDocument(t,e,i,s){return this.replyMedia("document",t,e,i,s)}async replyAnimation(t,e,i,s){return this.replyMedia("animation",t,e,i,s)}async replyVoice(t,e,i,s){return this.replyMedia("voice",t,e,i,s)}async replyVideoNote(t,e,i,s){return this.replyMedia("video_note",t,e,i,s)}async replySticker(t,e,i,s){return this.replyMedia("sticker",t,e,i,s)}async replyLocation(t,e,i,s){return this.replyMedia("location",t,e,i,s)}async replyContact(t,e,i,s){return this.replyMedia("contact",t,e,i,s)}async replyAction(t){return await this.ct.replyWithChatAction(t)}async edit(t,e,i,s,o=!0){try{let n=this.templateManager.getMessage(this.data.name,i,this.userLanguage,e);return await this.ct.editMessageText(n,{parse_mode:"HTML",reply_markup:t,...s})}catch(n){if(o)await this.reply(t,e,i,s);else throw n}}async editMedia(t,e,i,s,o,n,a=!0){try{let{src:r,caption:d}=this.templateManager.getMedia(this.data.name,s,this.userLanguage,i);return t=="voice"?(await this.delete(),await this.replyVoice(e,i,s,o)):t=="video_note"?(await this.delete(),await this.replyVoice(e,i,s,o)):t=="sticker"?(await this.delete(),await this.replySticker(e,i,s,o)):t=="location"?(await this.delete(),await this.replyLocation(e,i,s,o)):t=="contact"?(await this.delete(),await this.replyContact(e,i,s,o)):await this.ct.editMessageMedia({media:r,caption:d,type:t,parse_mode:"HTML",...o},{reply_markup:e,...n})}catch(r){if(a===!0)await this.reply(e,i,s,n);else throw r}}async editPhoto(t,e,i,s,o){return await this.editMedia("photo",t,e,i,s,o)}async editVideo(t,e,i,s,o){return await this.editMedia("video",t,e,i,s,o)}async editAudio(t,e,i,s,o){return await this.editMedia("audio",t,e,i,s,o)}async editDocument(t,e,i,s,o){return await this.editMedia("document",t,e,i,s,o)}async editAnimation(t,e,i,s,o){return await this.editMedia("animation",t,e,i,s,o)}async editVoice(t,e,i,s){return await this.editMedia("voice",t,e,i,s)}async editVideoNote(t,e,i,s){return await this.editMedia("video_note",t,e,i,s)}async editSticker(t,e,i,s){return await this.editMedia("sticker",t,e,i,s)}async editLocation(t,e,i,s){return await this.editMedia("location",t,e,i,s)}async editContact(t,e,i,s){return await this.editMedia("contact",t,e,i,s)}async delete(){return await this.ct.deleteMessage()}async callbackQueryAnswer(t){try{return await this.ct.answerCallbackQuery(t)}catch{return null}}async sessionSet(t,e,i){try{let s={method:t,params:e,...i},o=await this.cacheSession.set(`${this.userId}`,s);return o&&(this.session=s),o}catch{return null}}async sessionGet(){try{let t=await this.cacheSession.get(`${this.userId}`);return t&&(this.session=t),t||null}catch{return null}}async sessionClear(){try{let t=await this.cacheSession.delete(`${this.userId}`);return t&&(this.session={}),t}catch{return null}}static buildData(t){return{name:"",callbackData:t?function(...e){let i=t(...e);return`${this.name}${i==""||i===void 0?"":`:${i}`}`}:function(){return this?.name}}}inlineKeyboard(t,e){let i=this.data.name;if(e?.cache===!0){let a=`${i}:${this.userLanguage}:i`,r=this.cacheKeyboard.get(a);if(!r){let d=new S.InlineKeyboard,c=this.templateManager.getKeyboard(i,e?.baseId,this.userLanguage,"inline",e?.data),l=t(d,c);return this.cacheKeyboard.set(a,l),l}return r}let s=new S.InlineKeyboard,o=this.templateManager.getKeyboard(i,e?.baseId,this.userLanguage,"inline",e?.data);return t(s,o)}bottomKeyboard(t,e){let i=this.data.name;if(e?.cache===!0){let a=`${i}:${this.userLanguage}:b`,r=this.cacheKeyboard.get(a);if(!r){let d=new S.Keyboard,c=this.templateManager.getKeyboard(i,e?.baseId,this.userLanguage,"bottom",e?.data),l=t(d,c);return l.resized(!0),this.cacheKeyboard.set(a,l),l}return r}let s=new S.Keyboard,o=this.templateManager.getKeyboard(i,e?.baseId,this.userLanguage,"bottom",e?.data),n=t(s,o);return n.resized(),n}validateSessionCallback(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionCallback(e)}validateSessionCallbackPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionCallbackPayload(e)}validateSessionText(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionText(e)}validateSessionTextPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionTextPayload(e)}validateSessionTextCommand(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionTextCommand(e)}validateSessionTextCommandPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionTextCommandPayload(e)}validateSessionTextFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionTextFree(e)}validateSessionPhotoCaption(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionPhotoCaption(e)}validateSessionPhotoCaptionPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionPhotoCaptionPayload(e)}validateSessionPhotoCaptionCommand(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionPhotoCaptionCommand(e)}validateSessionPhotoCaptionCommandPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionPhotoCaptionCommandPayload(e)}validateSessionPhotoFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionPhotoFree(e)}validateSessionVideoCaption(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoCaption(e)}validateSessionVideoCaptionPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoCaptionPayload(e)}validateSessionVideoCaptionCommand(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoCaptionCommand(e)}validateSessionVideoCaptionCommandPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoCaptionCommandPayload(e)}validateSessionVideoFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoFree(e)}validateSessionAudioCaption(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAudioCaption(e)}validateSessionAudioCaptionPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAudioCaptionPayload(e)}validateSessionAudioCaptionCommand(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAudioCaptionCommand(e)}validateSessionAudioCaptionCommandPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAudioCaptionCommandPayload(e)}validateSessionAudioFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAudioFree(e)}validateSessionDocumentCaption(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionDocumentCaption(e)}validateSessionDocumentCaptionPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionDocumentCaptionPayload(e)}validateSessionDocumentCaptionCommand(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionDocumentCaptionCommand(e)}validateSessionDocumentCaptionCommandPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionDocumentCaptionCommandPayload(e)}validateSessionDocumentFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionDocumentFree(e)}validateSessionAnimationCaption(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAnimationCaption(e)}validateSessionAnimationCaptionPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAnimationCaptionPayload(e)}validateSessionAnimationCaptionCommand(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAnimationCaptionCommand(e)}validateSessionAnimationCaptionCommandPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAnimationCaptionCommandPayload(e)}validateSessionAnimationFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAnimationFree(e)}validateSessionVoiceCaption(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVoiceCaption(e)}validateSessionVoiceCaptionPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVoiceCaptionPayload(e)}validateSessionVoiceCaptionCommand(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVoiceCaptionCommand(e)}validateSessionVoiceCaptionCommandPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVoiceCaptionCommandPayload(e)}validateSessionVoiceFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVoiceFree(e)}validateSessionVideoNoteCaption(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoNoteCaption(e)}validateSessionVideoNoteCaptionPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoNoteCaptionPayload(e)}validateSessionVideoNoteCaptionCommand(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoNoteCaptionCommand(e)}validateSessionVideoNoteCaptionCommandPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoNoteCaptionCommandPayload(e)}validateSessionVideoNoteFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoNoteFree(e)}validateSessionStickerFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionStickerFree(e)}validateSessionLocationFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionLocationFree(e)}validateSessionContactFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionContactFree(e)}sessionCallback(t,e){return this.sessionSet(this.methodSessionCallback(e),t)}sessionCallbackPayload(t,e){return this.sessionSet(this.methodSessionCallbackPayload(e),t)}sessionText(t,e){return this.sessionSet(this.methodSessionText(e),t)}sessionTextPayload(t,e){return this.sessionSet(this.methodSessionTextPayload(e),t)}sessionTextCommand(t,e){return this.sessionSet(this.methodSessionTextCommand(e),t)}sessionTextCommandPayload(t,e){return this.sessionSet(this.methodSessionTextCommandPayload(e),t)}sessionTextFree(t,e){return this.sessionSet(this.methodSessionTextFree(e),t)}sessionPhotoCaption(t,e){return this.sessionSet(this.methodSessionPhotoCaption(e),t)}sessionPhotoCaptionPayload(t,e){return this.sessionSet(this.methodSessionPhotoCaptionPayload(e),t)}sessionPhotoCaptionCommand(t,e){return this.sessionSet(this.methodSessionPhotoCaptionCommand(e),t)}sessionPhotoCaptionCommandPayload(t,e){return this.sessionSet(this.methodSessionPhotoCaptionCommandPayload(e),t)}sessionPhotoFree(t,e){return this.sessionSet(this.methodSessionPhotoFree(e),t)}sessionVideoCaption(t,e){return this.sessionSet(this.methodSessionVideoCaption(e),t)}sessionVideoCaptionPayload(t,e){return this.sessionSet(this.methodSessionVideoCaptionPayload(e),t)}sessionVideoCaptionCommand(t,e){return this.sessionSet(this.methodSessionVideoCaptionCommand(e),t)}sessionVideoCaptionCommandPayload(t,e){return this.sessionSet(this.methodSessionVideoCaptionCommandPayload(e),t)}sessionVideoFree(t,e){return this.sessionSet(this.methodSessionVideoFree(e),t)}sessionAudioCaption(t,e){return this.sessionSet(this.methodSessionAudioCaption(e),t)}sessionAudioCaptionPayload(t,e){return this.sessionSet(this.methodSessionAudioCaptionPayload(e),t)}sessionAudioCaptionCommand(t,e){return this.sessionSet(this.methodSessionAudioCaptionCommand(e),t)}sessionAudioCaptionCommandPayload(t,e){return this.sessionSet(this.methodSessionAudioCaptionCommandPayload(e),t)}sessionAudioFree(t,e){return this.sessionSet(this.methodSessionAudioFree(e),t)}sessionDocumentCaption(t,e){return this.sessionSet(this.methodSessionDocumentCaption(e),t)}sessionDocumentCaptionPayload(t,e){return this.sessionSet(this.methodSessionDocumentCaptionPayload(e),t)}sessionDocumentCaptionCommand(t,e){return this.sessionSet(this.methodSessionDocumentCaptionCommand(e),t)}sessionDocumentCaptionCommandPayload(t,e){return this.sessionSet(this.methodSessionDocumentCaptionCommandPayload(e),t)}sessionDocumentFree(t,e){return this.sessionSet(this.methodSessionDocumentFree(e),t)}sessionAnimationCaption(t,e){return this.sessionSet(this.methodSessionAnimationCaption(e),t)}sessionAnimationCaptionPayload(t,e){return this.sessionSet(this.methodSessionAnimationCaptionPayload(e),t)}sessionAnimationCaptionCommand(t,e){return this.sessionSet(this.methodSessionAnimationCaptionCommand(e),t)}sessionAnimationCaptionCommandPayload(t,e){return this.sessionSet(this.methodSessionAnimationCaptionCommandPayload(e),t)}sessionAnimationFree(t,e){return this.sessionSet(this.methodSessionAnimationFree(e),t)}sessionVoiceCaption(t,e){return this.sessionSet(this.methodSessionVoiceCaption(e),t)}sessionVoiceCaptionPayload(t,e){return this.sessionSet(this.methodSessionVoiceCaptionPayload(e),t)}sessionVoiceCaptionCommand(t,e){return this.sessionSet(this.methodSessionVoiceCaptionCommand(e),t)}sessionVoiceCaptionCommandPayload(t,e){return this.sessionSet(this.methodSessionVoiceCaptionCommandPayload(e),t)}sessionVoiceFree(t,e){return this.sessionSet(this.methodSessionVoiceFree(e),t)}sessionVideoNoteCaption(t,e){return this.sessionSet(this.methodSessionVideoNoteCaption(e),t)}sessionVideoNoteCaptionPayload(t,e){return this.sessionSet(this.methodSessionVideoNoteCaptionPayload(e),t)}sessionVideoNoteCaptionCommand(t,e){return this.sessionSet(this.methodSessionVideoNoteCaptionCommand(e),t)}sessionVideoNoteCaptionCommandPayload(t,e){return this.sessionSet(this.methodSessionVideoNoteCaptionCommandPayload(e),t)}sessionVideoNoteFree(t,e){return this.sessionSet(this.methodSessionVideoNoteFree(e),t)}sessionStickerFree(t,e){return this.sessionSet(this.methodSessionStickerFree(e),t)}sessionLocationFree(t,e){return this.sessionSet(this.methodSessionLocationFree(e),t)}sessionContactFree(t,e){return this.sessionSet(this.methodSessionContactFree(e),t)}sessionToCallback(t,e){return this.sessionCallback({...this.session.params,...t},e)}sessionToCallbackPayload(t,e){return this.sessionCallbackPayload({...this.session.params,...t},e)}sessionToText(t,e){return this.sessionText({...this.session.params,...t},e)}sessionToTextPayload(t,e){return this.sessionTextPayload({...this.session.params,...t},e)}sessionToTextCommand(t,e){return this.sessionTextCommand({...this.session.params,...t},e)}sessionToTextCommandPayload(t,e){return this.sessionTextCommandPayload({...this.session.params,...t},e)}sessionToTextFree(t,e){return this.sessionTextFree({...this.session.params,...t},e)}sessionToPhotoCaption(t,e){return this.sessionPhotoCaption({...this.session.params,...t},e)}sessionToPhotoCaptionPayload(t,e){return this.sessionPhotoCaptionPayload({...this.session.params,...t},e)}sessionToPhotoCaptionCommand(t,e){return this.sessionPhotoCaptionCommand({...this.session.params,...t},e)}sessionToPhotoCaptionCommandPayload(t,e){return this.sessionPhotoCaptionCommandPayload({...this.session.params,...t},e)}sessionToPhotoFree(t,e){return this.sessionPhotoFree({...this.session.params,...t},e)}sessionToVideoCaption(t,e){return this.sessionVideoCaption({...this.session.params,...t},e)}sessionToVideoCaptionPayload(t,e){return this.sessionVideoCaptionPayload({...this.session.params,...t},e)}sessionToVideoCaptionCommand(t,e){return this.sessionVideoCaptionCommand({...this.session.params,...t},e)}sessionToVideoCaptionCommandPayload(t,e){return this.sessionVideoCaptionCommandPayload({...this.session.params,...t},e)}sessionToVideoFree(t,e){return this.sessionVideoFree({...this.session.params,...t},e)}sessionToAudioCaption(t,e){return this.sessionAudioCaption({...this.session.params,...t},e)}sessionToAudioCaptionPayload(t,e){return this.sessionAudioCaptionPayload({...this.session.params,...t},e)}sessionToAudioCaptionCommand(t,e){return this.sessionAudioCaptionCommand({...this.session.params,...t},e)}sessionToAudioCaptionCommandPayload(t,e){return this.sessionAudioCaptionCommandPayload({...this.session.params,...t},e)}sessionToAudioFree(t,e){return this.sessionAudioFree({...this.session.params,...t},e)}sessionToDocumentCaption(t,e){return this.sessionDocumentCaption({...this.session.params,...t},e)}sessionToDocumentCaptionPayload(t,e){return this.sessionDocumentCaptionPayload({...this.session.params,...t},e)}sessionToDocumentCaptionCommand(t,e){return this.sessionDocumentCaptionCommand({...this.session.params,...t},e)}sessionToDocumentCaptionCommandPayload(t,e){return this.sessionDocumentCaptionCommandPayload({...this.session.params,...t},e)}sessionToDocumentFree(t,e){return this.sessionDocumentFree({...this.session.params,...t},e)}sessionToAnimationCaption(t,e){return this.sessionAnimationCaption({...this.session.params,...t},e)}sessionToAnimationCaptionPayload(t,e){return this.sessionAnimationCaptionPayload({...this.session.params,...t},e)}sessionToAnimationCaptionCommand(t,e){return this.sessionAnimationCaptionCommand({...this.session.params,...t},e)}sessionToAnimationCaptionCommandPayload(t,e){return this.sessionAnimationCaptionCommandPayload({...this.session.params,...t},e)}sessionToAnimationFree(t,e){return this.sessionAnimationFree({...this.session.params,...t},e)}sessionToVoiceCaption(t,e){return this.sessionVoiceCaption({...this.session.params,...t},e)}sessionToVoiceCaptionPayload(t,e){return this.sessionVoiceCaptionPayload({...this.session.params,...t},e)}sessionToVoiceCaptionCommand(t,e){return this.sessionVoiceCaptionCommand({...this.session.params,...t},e)}sessionToVoiceCaptionCommandPayload(t,e){return this.sessionVoiceCaptionCommandPayload({...this.session.params,...t},e)}sessionToVoiceFree(t,e){return this.sessionVoiceFree({...this.session.params,...t},e)}sessionToVideoNoteCaption(t,e){return this.sessionVideoNoteCaption({...this.session.params,...t},e)}sessionToVideoNoteCaptionPayload(t,e){return this.sessionVideoNoteCaptionPayload({...this.session.params,...t},e)}sessionToVideoNoteCaptionCommand(t,e){return this.sessionVideoNoteCaptionCommand({...this.session.params,...t},e)}sessionToVideoNoteCaptionCommandPayload(t,e){return this.sessionVideoNoteCaptionCommandPayload({...this.session.params,...t},e)}sessionToVideoNoteFree(t,e){return this.sessionVideoNoteFree({...this.session.params,...t},e)}sessionToStickerFree(t,e){return this.sessionStickerFree({...this.session.params,...t},e)}sessionToLocationFree(t,e){return this.sessionLocationFree({...this.session.params,...t},e)}sessionToContactFree(t,e){return this.sessionContactFree({...this.session.params,...t},e)}intent(t,e=!0){t||(t=e?this.data.callbackData():this.data.name);let i=e?":":" ";return t.indexOf(i)!=-1?t.split(i)[0]:t}methodSession(t,e){return`${t}-${e||this.data.name}`}methodSessionCallback(t){return this.methodSession("1",t)}methodSessionCallbackPayload(t){return this.methodSession("2",t)}methodSessionText(t){return this.methodSession("3",t)}methodSessionTextPayload(t){return this.methodSession("4",t)}methodSessionTextCommand(t){return this.methodSession("5",t)}methodSessionTextCommandPayload(t){return this.methodSession("6",t)}methodSessionTextFree(t){return this.methodSession("7",t)}methodSessionPhotoCaption(t){return this.methodSession("8",t)}methodSessionPhotoCaptionPayload(t){return this.methodSession("9",t)}methodSessionPhotoCaptionCommand(t){return this.methodSession("10",t)}methodSessionPhotoCaptionCommandPayload(t){return this.methodSession("11",t)}methodSessionPhotoFree(t){return this.methodSession("12",t)}methodSessionVideoCaption(t){return this.methodSession("13",t)}methodSessionVideoCaptionPayload(t){return this.methodSession("14",t)}methodSessionVideoCaptionCommand(t){return this.methodSession("15",t)}methodSessionVideoCaptionCommandPayload(t){return this.methodSession("16",t)}methodSessionVideoFree(t){return this.methodSession("17",t)}methodSessionAudioCaption(t){return this.methodSession("18",t)}methodSessionAudioCaptionPayload(t){return this.methodSession("19",t)}methodSessionAudioCaptionCommand(t){return this.methodSession("20",t)}methodSessionAudioCaptionCommandPayload(t){return this.methodSession("21",t)}methodSessionAudioFree(t){return this.methodSession("22",t)}methodSessionDocumentCaption(t){return this.methodSession("23",t)}methodSessionDocumentCaptionPayload(t){return this.methodSession("24",t)}methodSessionDocumentCaptionCommand(t){return this.methodSession("25",t)}methodSessionDocumentCaptionCommandPayload(t){return this.methodSession("26",t)}methodSessionDocumentFree(t){return this.methodSession("27",t)}methodSessionAnimationCaption(t){return this.methodSession("28",t)}methodSessionAnimationCaptionPayload(t){return this.methodSession("29",t)}methodSessionAnimationCaptionCommand(t){return this.methodSession("30",t)}methodSessionAnimationCaptionCommandPayload(t){return this.methodSession("31",t)}methodSessionAnimationFree(t){return this.methodSession("32",t)}methodSessionVoiceCaption(t){return this.methodSession("33",t)}methodSessionVoiceCaptionPayload(t){return this.methodSession("34",t)}methodSessionVoiceCaptionCommand(t){return this.methodSession("35",t)}methodSessionVoiceCaptionCommandPayload(t){return this.methodSession("36",t)}methodSessionVoiceFree(t){return this.methodSession("37",t)}methodSessionVideoNoteCaption(t){return this.methodSession("38",t)}methodSessionVideoNoteCaptionPayload(t){return this.methodSession("39",t)}methodSessionVideoNoteCaptionCommand(t){return this.methodSession("40",t)}methodSessionVideoNoteCaptionCommandPayload(t){return this.methodSession("41",t)}methodSessionVideoNoteFree(t){return this.methodSession("42",t)}methodSessionStickerFree(t){return this.methodSession("43",t)}methodSessionLocationFree(t){return this.methodSession("44",t)}methodSessionContactFree(t){return this.methodSession("45",t)}};var W=require("bun"),_=require("path");var V=require("path"),w=require("fs");var P=class{static{h(this,"PageManager")}static _getFile(t,e,i=!0,s=0){let n=Object.values(t)[s];if(n===void 0)throw new Error(`File ${e} must have export (not default) class page`);return i===!0&&(n.template=void 0),n}static getFilePath(){return(0,V.join)(process.cwd(),"src","pages")}static getFileSync(t,e=!0,i=0){t||(t=this.getFilePath());let s=require(t);return this._getFile(s,t,e,i)}static async getFile(t,e=!0,i=0){t||(t=this.getFilePath());let s=await import(`${t}?t=${Date.now()}`);return this._getFile(s,t,e,i)}static getFileManySync(t,e=!0,i=0){if(t||(t=this.getFilePath()),!(0,w.existsSync)(t))throw new Error(`Directory ${t} is not found, please make it manually`);return(0,w.readdirSync)(t).map(o=>{let n=(0,V.join)(t,o);return{path:n,page:this.getFileSync(n,e,i)}})}};var j=require("logging-pretty");var C=require("path"),b=require("fs"),L=require("chokidar");var f=class{static{h(this,"TemplateEngine")}static _escape(t){return t.replace(/\\/g,"\\\\").replace(/`/g,"\\`")}static _processVariables(t){return t.replace(/\{\{(\w+)\}\}/g,(e,i)=>`\${(data && data["${i}"]) ?? ""}`)}static _processBaseBlocks(t){let e=/<base\b[^>]*?(?:\sid=(["']?)([^"'\s>]+)\1)?[^>]*?>([\s\S]*?)<\/base>/g,i={};for(let s of t.matchAll(e)){let o=s[2]??"default",a=(s[3]??"").replace(/^[\t ]+/gm,"").replace(/^\n+/,"").replace(/\n+$/,"").trim();i[o]=a}return i}static _processScriptBlocks(t){return t.replace(/<script\b[^>]*>([\s\S]*?)<\/script>/gi,(e,i)=>{let s=i.trim();return s?`\${(() => { if (!data) return ''; try { with(data) { ${s} } } catch(e) { return '' } })()}`:""})}static _processSpaceBlocks(t){return t.replace(/^[ \t]*<space\s*\/>[ \t]*$/gim,"")}static _processDivBlocks(t){return t.replace(/<div>([\s\S]*?)<\/div>/gi,(e,i)=>i.replace(/^[\t ]+/gm,"").replace(/^\n+/,"").replace(/\n+$/,""))}static _processExec(t){return t.replace(/{~\s*([\s\S]*?)\s*~}/g,(e,i)=>`\${(() => { try { return ${i} } catch(e) { try { with(data || {}) { return ${i} } } catch(ee) { return '' } } })()}`)}static _processMapBlocks(t){let e=/<map(?:\s+([^>]*))?>([\s\S]*?)<\/map>/gi;return t.replace(e,(i,s,o)=>{let n={},a=/(\w+)="([^"]*)"/g;if(s)for(let g of s.matchAll(a))n[g[1]]=g[2];let r=n.from;if(!r)return"";let d=n.as||"e",c=n.index||"i",l=n.join??"\\n",p=o.replace(/^[\t ]+/gm,"").replace(/^\n+/,"").replace(/\n+$/,"").replace(/`/g,"\\`");p=p.replace(/\{\{([\w.]+)\}\}/g,(g,y)=>y===d||y.startsWith(`${d}.`)?`\${${y}}`:y===c?`\${${c}}`:g);let m=l.replace(/"/g,'\\"');return`\${(data && data["${r}"] || []).map((${d}, ${c}) => \`${p}\`).join("${m}")}`})}static _processMediaBlocks(t){let e=/<media(?:\s+([^>]*))?>([\s\S]*?)<\/media>/gi,i={};for(let s of t.matchAll(e)){let o=s[1],n=s[2],a={},r=/(\w+)="([^"]*)"/g;if(o)for(let m of o.matchAll(r))a[m[1]]=m[2];let d=a.lang||"default",c=a.type,l=n.replace(/^[\t ]+/gm,"").replace(/^\n+/,"").replace(/\n+$/,"").trim(),p={type:c};a.src&&(p.src=a.src),l&&c!=="video_note"&&c!=="sticker"&&(p.caption=l),c==="location"&&(a.latitude&&(p.latitude=a.latitude),a.longitude&&(p.longitude=a.longitude),l&&(p.caption=l)),c==="contact"&&(a.phone&&(p.phone=a.phone),a.first_name&&(p.first_name=a.first_name),a.last_name&&(p.last_name=a.last_name),a.vcard&&(p.vcard=a.vcard),l&&(p.caption=l)),i[d]=p}return i}static _processMessageBlocks(t){let e=/<message(?:\s+([^>]*))?>([\s\S]*?)<\/message>/gi,i={};for(let[,s,o]of t.matchAll(e)){let n={},a=/(\w+)="([^"]*)"/g;if(s)for(let c of s.matchAll(a))n[c[1]]=c[2];let r=n.lang||"default",d=o.replace(/^[\t ]+/gm,"").replace(/^\n+/,"").replace(/\n+$/,"").trim();i[r]=d}return i}static _processDescriptionBlocks(t){let e=/<description(?:\s+([^>]*))?>([\s\S]*?)<\/description>/gi,i={};for(let[,s,o]of t.matchAll(e)){let n={},a=/(\w+)="([^"]*)"/g;if(s)for(let l of s.matchAll(a))n[l[1]]=l[2];let r=n.lang||"default",d=n.scope||"default",c=o.replace(/^[\t ]+/gm,"").replace(/^\n+/,"").replace(/\n+$/,"").trim();i[r]||(i[r]={}),i[r][d]=c}return i}static _processKeyboardBlocks(t){let e=/<keyboard(?:\s+([^>]*))?>([\s\S]*?)<\/keyboard>/gi,i={};for(let[,s,o]of t.matchAll(e)){let n={},a=/(\w+)="([^"]*)"/g;if(s)for(let l of s.matchAll(a))n[l[1]]=l[2];let r=n.lang||"default",d=n.type==="bottom"?"bottom":"inline",c=o.trim();i[r]||(i[r]={}),i[r][d]=c}return i}static _compileToFunction(t,e){let i;e?i=t.map(n=>`\`${n}\``).join(","):i=`\`${t}\``;let s=i;if(s=this._processScriptBlocks(this._processDivBlocks(this._processSpaceBlocks(s))),e){let o=`return [${s}]`;return new Function("data",o)}else{let o=`return ${s}`;return new Function("data",o)}}static compileMedia(t){let e=this._processBaseBlocks(t),i={};for(let[s,o]of Object.entries(e)){let n=this._processMediaBlocks(o);i[s]={};for(let[a,r]of Object.entries(n)){let d={};for(let[c,l]of Object.entries(r)){if(c==="type"||typeof l!="string")continue;let p=this._processVariables(this._processExec(this._processMapBlocks(this._processScriptBlocks(l))));d[c]=this._compileToFunction(p,!1)}i[s][a]=c=>{let l={...r};for(let[p,m]of Object.entries(d))l[p]=m(c);return l}}}return i}static compileMessages(t){let e=this._processBaseBlocks(t),i={};for(let[s,o]of Object.entries(e)){let n=this._processMessageBlocks(o);i[s]={};for(let[a,r]of Object.entries(n)){let d=this._processVariables(this._processExec(this._processMapBlocks(this._processScriptBlocks(r))));i[s][a]=this._compileToFunction(d,!1)}}return i}static compileDescription(t){let e=this._processBaseBlocks(t),i={};for(let[s,o]of Object.entries(e)){let n=this._processDescriptionBlocks(o);i[s]={};for(let[a,r]of Object.entries(n))for(let[d,c]of Object.entries(r)){i[s][d]||(i[s][d]={});let l=this._processVariables(this._processExec(this._processMapBlocks(this._processScriptBlocks(c))));i[s][d][a]=this._compileToFunction(l,!1)}}return i}static compileKeyboards(t){let e=this._processBaseBlocks(t),i={};for(let[s,o]of Object.entries(e)){let n=this._processKeyboardBlocks(o);i[s]={};for(let[a,r]of Object.entries(n)){i[s][a]={};for(let[d,c]of Object.entries(r)){let p=c.split(/(?<!\\),\s*/).map(m=>m.replace(/\\,/g,",").trim()).map(m=>this._processVariables(this._processExec(this._processMapBlocks(this._processScriptBlocks(m)))));i[s][a][d]=this._compileToFunction(p,!0)}}}return i}};var k=class{static{h(this,"TemplateManager")}_map=new Map;_engine=f;_watcher=[];_supportedExtensions=new Set([".html",".ts"]);_path;_isWatchEnabled;constructor(t){let{path:e,enableWatch:i=!1}=t;this._path=e,this._isWatchEnabled=i,this._validatePath(),this._initializeWatcher(t.watchOptions),this._loadAllFiles()}_validatePath(){try{if(!this._path||!Array.isArray(this._path))throw new Error("Path must be set as string array");for(let t=0;t<this._path.length;t++){let e=this._path[t];if(!(0,b.existsSync)(e))throw new Error(`Template base path is not exists: ${e}`);if(!(0,b.statSync)(e).isDirectory())throw new Error(`Template base path is not a directory: ${e}`)}return!0}catch(t){throw new Error(`Failed to validate Template base path: ${String(t)}`)}}_initializeWatcher(t){if(this._isWatchEnabled){if(this._watcher.length>0)throw new Error("Watcher already enabled");for(let e=0;e<this._path.length;e++){let i=this._path[e],s=(0,L.watch)(i,{persistent:!0,ignoreInitial:!0,awaitWriteFinish:{stabilityThreshold:100,pollInterval:50},...t});this._watcher[e]=s,s.on("change",this._handleFileChange.bind(this)),s.on("add",this._handleFileAdd.bind(this)),s.on("unlink",this._handleFileDelete.bind(this)),s.on("error",this._handleWatchError.bind(this))}}}_handleFileChange(t){let e=(0,C.basename)(t);this._isValidFile(e)&&this._loadFile(e)}_handleFileAdd(t){let e=(0,C.basename)(t);this._isValidFile(e)&&this._loadFile(e)}_handleFileDelete(t){let e=(0,C.basename)(t),i=this._getKey(e);this._map.has(i)&&this._map.delete(i)}_handleWatchError(t){}_loadAllFiles(){for(let t=0;t<this._path.length;t++){let e=this._path[t],s=(0,b.readdirSync)(e,"utf8").filter(o=>this._isValidFile(o));for(let o of s)this._loadFile(o)}}_loadFile(t){for(let e=0;e<this._path.length;e++){let i=this._path[e],s=(0,C.join)(i,t),o=this._getExtension(t),n=this._getKey(t);if(o==".html"){let a=(0,b.readFileSync)(s,"utf8");this._compile(n,a)}else if(o==".ts"){let a=P.getFileSync(s,!1);a?.template!==void 0&&this._compile(n,a.template),a.template=void 0}else throw new Error(`File ${t} is not support`)}}_getExtension(t){return(0,C.extname)(t).toLowerCase()}_isValidFile(t){return this._supportedExtensions.has(this._getExtension(t))}_getKey(t){return t.replace(/\.[^/.]+$/,"")}_compile(t,e,i){let s=this._map.get(t),o=s||{},n=!1;(i===void 0||i?.media===!1||s?.media===void 0)&&(o.media=f.compileMedia(e),n=!0),(i===void 0||i?.message===!1||s?.message===void 0)&&(o.message=f.compileMessages(e),n=!0),(i===void 0||i?.keyboard===!1||s?.keyboard===void 0)&&(o.keyboard=f.compileKeyboards(e),n=!0),(i===void 0||i?.description===!1||s?.description===void 0)&&(o.description=f.compileDescription(e),n=!0),n&&this._map.set(t,o)}getMedia(t,e="default",i="default",s){let n=this._map.get(t)?.media?.[e],a=n?.[i]||n?.default;try{if(!a)throw new Error("Media function not found, please check your template");return a(s)}catch{return{}}}getMessage(t,e="default",i="default",s){try{let n=this._map.get(t)?.message?.[e],a=n?.[i]||n?.default;if(!a)throw"";return a(s)}catch{return""}}getDescription(t,e="default",i="default",s="default",o){try{let a=this._map.get(t)?.description?.[e]?.[s],r=a?.[i]||a?.default;if(!r)throw"";return r(o)}catch{return""}}getDescriptionLanguage(t,e="default",i="default"){try{let o=this._map.get(t)?.description?.[e]?.[i];return o?Object.keys(o):[]}catch{return[]}}getDescriptionScopes(t,e="default"){try{let s=this._map.get(t)?.description?.[e];return s?Object.keys(s):[]}catch{return[]}}getKeyboard(t,e="default",i="default",s="inline",o){try{let a=this._map.get(t)?.keyboard?.[e],r=a?.[i]?.[s]||a?.default?.[s];if(!r)throw[];return r(o)}catch{return[]}}async write(t,e){if(this._getExtension(t)!=".html")return null;for(let i=0;i<this._path.length;i++){let s=(0,C.join)(this._path[i],t);if((0,b.existsSync)(s))return(0,b.writeFileSync)(s,e,"utf8"),this._loadFile(t),!0}return null}has(t){return this._map.has(t)}async destroy(){if(this._watcher)for(let t of this._watcher)await t.close();this._map.clear()}};var M=require("grammy");var D=class{static{h(this,"Gramstax")}templateManager;cacheKeyboard;cacheSession;pages;log;bot;handlerBotUpdate;constructor(t){if(!t?.token||t?.token?.length==0)throw new Error("Token is not valid or empty");if(this.log=t.log===!0?new j.LoggingPretty:t.log?t.log:void 0,this.cacheSession=t.cacheSession||new I("memory","session"),this.cacheKeyboard=t.cacheKeyboard||new Map,this.templateManager=t.templateManager||new k({path:[(0,_.join)(process.cwd(),"src","pages")]}),this.bot=this.createBot(t.token),this.bot.catch(this.onCatch),this.registerOnCallbackQueryData(),this.registerOnMessageText(),this.registerOnMessagePhoto(),this.registerOnMessageVideo(),this.registerOnMessageAudio(),this.registerOnMessageDocument(),this.registerOnMessageAnimation(),this.registerOnMessageVoice(),this.registerOnMessageVideoNote(),this.registerOnMessageSticker(),this.registerOnMessageLocation(),this.registerOnMessageContact(),this.handlerBotUpdate=this.bot.handleUpdate,this.pages=this.pageLoads(),process.once("SIGINT",()=>this.bot.stop()),process.once("SIGTERM",()=>this.bot.stop()),t.deploy.startsWith("polling"))this.runPolling();else if(t.deploy.startsWith("webhook:"))this.runWebhook(t.deploy.split("webhook:")[1]);else if(t.deploy.startsWith("serverless:"))this.runServerless(t.deploy.split("serverless:")[1]);else if(!t.deploy.startsWith("test"))throw new Error("Params deploy is not valid, expected: polling, webhook:<public-url>, serverless:<your-adapter>")}onCatch(t){this.log?.error?.(`[Bot.catch]: ${JSON.stringify(t,null,2)}`)}async onStart(t,e,i){await this.executeSyncBlocksDescription(this.pages);let{username:s}=t;this.log?.success?.(`Telegram bot polling started: ${s} ${e=="webhook"||e=="serverless"?`(${i})`:""}`)}createBot(t){return new M.Bot(t)}createCtx(t){return new x({ct:t,cacheSession:this.cacheSession,cacheKeyboard:this.cacheKeyboard,templateManager:this.templateManager,temp:{session:{},data:{}}})}runPolling(){this.bot.start({onStart:h(t=>{this.onStart(t,"polling",null)},"onStart")})}async runWebhook(t){let e=(0,M.webhookCallback)(this.bot,"bun");this.handlerBotUpdate=e;let i=new URL(t).pathname;(0,W.serve)({fetch(n){let{pathname:a}=new URL(n.url);if(n.method=="POST"&&a==i)return e(n);if(n.method=="GET"&&a=="/"){let r=new Date().toISOString();return new Response(JSON.stringify({status:"OK",timestamp:r}),{status:200,headers:{"Content-Type":"application/json"}})}return new Response("Not found",{status:404})}}),(await this.bot.api.getWebhookInfo()).url!=t&&await this.bot.api.setWebhook(t);let o=await this.bot.api.getMe();await this.onStart(o,"webhook",t)}async runServerless(t){this.handlerBotUpdate=(0,M.webhookCallback)(this.bot,t);let e=await this.bot.api.getMe();await this.onStart(e,"serverless",t)}pageBuildData(t,e){t?.data===void 0&&(t.data=t.buildData()),t.data.name=(0,_.parse)(e).name}pageSorts(t,e){let i=t.data.name,s=t.prototype,o="Payload",n="Command",a="Caption",r="Free",d=e.routeDynamic,c=e.routeDynamicSpesific,l=h(g=>g.charAt(0).toUpperCase()+g.slice(1),"upFirst"),p=h(g=>g.replace(o,"").replace(n,"").replace(a,"").replace(r,""),"getPureRouteName"),m=h((g,y)=>s[`methodSession${l(g)}`]?.(y),"methodSession");e.all[i]=t;for(let g=0;g<e.lenListRouteStaticSession;g++){let y=e.listRouteStaticSession[g],Q=e.routeStatic[y],U=e.routeStaticIntent[y],O=e.routeStaticSession[y],F={name:i,routeName:y},R=Q||U;if(R!==void 0){let J=y.includes(n)?`/${i}`:i;R[s.intent(J,y.startsWith("callback"))]=F}O!==void 0&&(O[m(y,i)]=F),y.toLowerCase().endsWith("free")&&c[p(y)].push(F)}s.free&&d.push(i)}async executeSyncBlocksDescription(t){let e=new Map,i=t.routeStatic.textCommand;for(let[,s]of Object.entries(i)){let o=s.name,n=this.templateManager.getDescriptionScopes(o);for(let a of n){e.has(a)||e.set(a,new Set);let r=this.templateManager.getDescriptionLanguage(o,"default",a);for(let d of r)e.get(a).add(d)}}for(let[s,o]of e)for(let n of o){let a=[];for(let[,c]of Object.entries(i)){let l=c.name,p=this.templateManager.getDescription(l,"default",n,s);p&&a.push({command:l,description:p})}if(a.length===0)continue;let r={type:s},d=n==="default"?void 0:n;try{let c=await this.bot.api.getMyCommands({scope:r,language_code:d}),l=JSON.stringify(c.map(m=>({command:m.command,description:m.description})).sort((m,g)=>m.command.localeCompare(g.command))),p=JSON.stringify(a.sort((m,g)=>m.command.localeCompare(g.command)));l!==p&&(await this.bot.api.setMyCommands(a,{scope:r,language_code:d}),this.log?.info?.(`Synced descriptions for scope: ${s},${d?` lang: ${d},`:""} total (${a.length})`))}catch(c){this.log?.error?.(`Failed to sync descriptions for scope: ${s},${d?` lang: ${d},`:""} error: ${String(c)}`)}}}pageLoads(){this.log?.info?.("Load pages..");let t=["callback","text","textCommand","photoCaption","photoCaptionCommand","videoCaption","videoCaptionCommand","audioCaption","audioCaptionCommand","documentCaption","documentCaptionCommand","animationCaption","animationCaptionCommand","voiceCaption","voiceCaptionCommand","videoNoteCaption","videoNoteCaptionCommand"],e=["callbackPayload","textPayload","textCommandPayload","photoCaptionPayload","photoCaptionCommandPayload","videoCaptionPayload","videoCaptionCommandPayload","audioCaptionPayload","audioCaptionCommandPayload","documentCaptionPayload","documentCaptionCommandPayload","animationCaptionPayload","animationCaptionCommandPayload","voiceCaptionPayload","voiceCaptionCommandPayload","videoNoteCaptionPayload","videoNoteCaptionCommandPayload"],i=[...t,...e,"textFree","photoFree","videoFree","audioFree","documentFree","animationFree","voiceFree","videoNoteFree","stickerFree","locationFree","contactFree"],s=h((a,r)=>Object.fromEntries(a.map(d=>[d,r()])),"makeObject"),o=h(()=>[],"initDynamicSpesific"),n={all:{},routeDynamic:[],routeDynamicSpesific:{text:o(),photo:o(),video:o(),audio:o(),document:o(),animation:o(),voice:o(),videoNote:o(),sticker:o(),location:o(),contact:o()},routeStatic:s(t,()=>({})),routeStaticIntent:s(e,()=>({})),routeStaticSession:s(i,()=>({})),listRouteStatic:t,listRouteStaticIntent:e,listRouteStaticSession:i,lenListRouteStatic:t.length,lenListRouteStaticIntent:e.length,lenListRouteStaticSession:i.length};return P.getFileManySync(void 0,!0).forEach(({path:a,page:r})=>{this.pageBuildData(r,a),this.pageSorts(r,n)}),this.log?.info?.(`Finish load pages with total (${Object.keys(n.all).length})`),n}async pageRoutes(t,e){let i,s;if(i===void 0){let n=t.callbackData,a=t.msgText,r=t.msgCaption,d=this.pages.routeStatic;for(let c=0;c<this.pages.lenListRouteStatic;c++){let l=this.pages.listRouteStatic[c];if(l.startsWith(e)&&(s=d[l][n||a||r]),s)break}s!==void 0&&(i=this.pages.all[s.name])}if(i===void 0){let n=await t.sessionGet();if(n){let a=n.method,r=this.pages.routeStaticSession;for(let d=0;d<this.pages.lenListRouteStaticSession;d++){let c=this.pages.listRouteStaticSession[d];if(c.startsWith(e)&&(s=r[c][a]),s)break}s!==void 0&&(i=this.pages.all[s.name])}}if(i===void 0){let n=t.callbackDataIntent,a=t.msgTextIntent,r=t.msgCaptionIntent,d=this.pages.routeStaticIntent;for(let c=0;c<this.pages.lenListRouteStaticIntent;c++){let l=this.pages.listRouteStaticIntent[c];if(l.startsWith(e)&&(s=d[l][n||a||r]),s)break}s!==void 0&&(i=this.pages.all[s.name])}if(i!==void 0&&s!==void 0)return i.prototype[s.routeName]?await new i(t)[s.routeName]?.():void 0;let o=this.pages.routeDynamicSpesific[e]||[];for(let n=0;n<o.length;n++){let a=o[n];if(a===void 0)continue;let r=this.pages.all[a.name];if(r===void 0||!r.prototype[a.routeName])continue;let c=await new r(t)[a.routeName]?.();if(c!==null)return c}for(let n=0;n<this.pages.routeDynamic.length;n++){let a=this.pages.routeDynamic[n];if(a===void 0)continue;let r=this.pages.all[a];if(r===void 0||!r.prototype.free)continue;let c=await new r(t).free?.();if(c!==null)return c}return null}async hookBeforeRoute(t,e){return!!(t||e)}async hookErrorPage(t,e,i,s){}async hookErrorInputNotFoundPage(t){}async onMessage(t,e){let i=this.createCtx(t);try{if(await this.hookBeforeRoute(i,e)===!1)return;await this.pageRoutes(i,e)===null&&await this.hookErrorInputNotFoundPage(i)}catch(s){await this.hookErrorPage(i,e,s,!1)}}async onCallbackQueryData(t){let e=this.createCtx(t);try{if(await e.callbackQueryAnswer(),await this.hookBeforeRoute(e,"onCallbackQueryData")===!1)return;await this.pageRoutes(e,"callback")}catch(i){await this.hookErrorPage(e,"onCallbackQueryData",i,!0)}}async onMessageText(t){await this.onMessage(t,"text")}async onMessagePhoto(t){await this.onMessage(t,"photo")}async onMessageVideo(t){await this.onMessage(t,"video")}async onMessageAudio(t){await this.onMessage(t,"audio")}async onMessageDocument(t){await this.onMessage(t,"document")}async onMessageAnimation(t){await this.onMessage(t,"animation")}async onMessageVoice(t){await this.onMessage(t,"voice")}async onMessageVideoNote(t){await this.onMessage(t,"videoNote")}async onMessageSticker(t){await this.onMessage(t,"sticker")}async onMessageLocation(t){await this.onMessage(t,"location")}async onMessageContact(t){await this.onMessage(t,"contact")}registerOnCallbackQueryData(){this.bot.on("callback_query:data",this.onCallbackQueryData.bind(this))}registerOnMessageText(){this.bot.on("message:text",this.onMessageText.bind(this))}registerOnMessagePhoto(){this.bot.on("message:photo",this.onMessagePhoto.bind(this))}registerOnMessageVideo(){this.bot.on("message:video",this.onMessageVideo.bind(this))}registerOnMessageAudio(){this.bot.on("message:audio",this.onMessageAudio.bind(this))}registerOnMessageDocument(){this.bot.on("message:document",this.onMessageDocument.bind(this))}registerOnMessageAnimation(){this.bot.on("message:animation",this.onMessageAnimation.bind(this))}registerOnMessageVoice(){this.bot.on("message:voice",this.onMessageVoice.bind(this))}registerOnMessageVideoNote(){this.bot.on("message:video_note",this.onMessageVideoNote.bind(this))}registerOnMessageSticker(){this.bot.on("message:sticker",this.onMessageSticker.bind(this))}registerOnMessageLocation(){this.bot.on("message:location",this.onMessageLocation.bind(this))}registerOnMessageContact(){this.bot.on("message:contact",this.onMessageContact.bind(this))}};0&&(module.exports={CacheExternal,Ctx,Gramstax,LoggingUtils,TemplateEngine,TemplateManager,log});
1
+ "use strict";var G=Object.create;var k=Object.defineProperty;var H=Object.getOwnPropertyDescriptor;var z=Object.getOwnPropertyNames;var q=Object.getPrototypeOf,X=Object.prototype.hasOwnProperty;var m=(h,t)=>k(h,"name",{value:t,configurable:!0});var Y=(h,t)=>{for(var e in t)k(h,e,{get:t[e],enumerable:!0})},B=(h,t,e,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of z(t))!X.call(h,s)&&s!==e&&k(h,s,{get:()=>t[s],enumerable:!(i=H(t,s))||i.enumerable});return h};var K=(h,t,e)=>(e=h!=null?G(q(h)):{},B(t||!h||!h.__esModule?k(e,"default",{value:h,enumerable:!0}):e,h)),Z=h=>B(k({},"__esModule",{value:!0}),h);var et={};Y(et,{CacheExternal:()=>I,Ctx:()=>x,Gramstax:()=>A,LoggingUtils:()=>w,TemplateEngine:()=>C,TemplateManager:()=>M,log:()=>tt});module.exports=Z(et);var $=K(require("logging-pretty"),1);var w=class extends $.default{static{m(this,"LoggingUtils")}constructor(t){super(t)}errorMake(t,e,i){this.error(`[${e?.name||e}:${i}] ${String(t)}`)}},tt=new w;var E=require("keyv"),L=K(require("@keyv/redis"),1);var I=class extends E.Keyv{constructor(e="memory",i,s,a){let n;e.startsWith("redis")&&(n=new L.default(e)),e==="memory"&&(n=new Map),a?.forceStore&&(n=a.forceStore);super({store:n,namespace:i,ttl:s});this.url=e}static{m(this,"CacheExternal")}};var W=require("timers/promises"),P=require("grammy");var x=class{static{m(this,"Ctx")}static template;static data;data=this.constructor.data;get userIsPremium(){return this.ct.from?.is_premium??!1}get userIsBot(){return this.ct.from?.is_bot??!1}get userId(){return this.ct.from?.id}get userName(){return this.ct.from?.username}get userFullName(){return`${this.ct.from?.first_name}${this.ct.from?.last_name}`}get userFirstName(){return this.ct.from?.first_name}get userLastName(){return this.ct.from?.last_name}get userLanguage(){return this.ct.from?.language_code}set userLanguage(t){this.ct.from.language_code=t}get msgText(){return this.ct.message?.text}get msgTextIntent(){return this.msgText&&this.msgText.indexOf(" ")!=-1?this.msgText.split(" ")[0]:""}get msgTextPayloads(){return this.msgText&&this.msgText.indexOf(" ")!=-1?this.msgText.split(" ").slice(1):[]}get msgTextPayload(){return this.msgTextPayloads[0]}get msgCaption(){return this.ct.message?.caption}get msgCaptionIntent(){return this.msgCaption&&this.msgCaption.indexOf(" ")!=-1?this.msgCaption.split(" ")[0]:""}get msgPhoto(){return this.ct.message?.photo}get msgVideo(){return this.ct.message?.video}get msgAudio(){return this.ct.message?.audio}get callbackData(){return this.ct.callbackQuery?.data}get callbackDataIntent(){return this.callbackData&&this.callbackData.indexOf(":")!=-1?this.callbackData.split(":")[0]:""}get callbackDataPayloads(){return this.callbackData&&this.callbackData.indexOf(":")!=-1?this.callbackData.split(":").slice(1):[]}get callbackDataPayload(){return this.callbackDataPayloads?.[0]}get callbackDataParts(){return this.callbackDataPayload&&this.callbackDataPayload.indexOf("+")!=-1?this.callbackDataPayload.split("+"):[]}set session(t){this.temp.session=t}get session(){return this.temp.session}ct;cacheSession;cacheKeyboard;templateManager;temp;constructor(t){this.ct=t.ct,this.cacheSession=t.cacheSession,this.cacheKeyboard=t.cacheKeyboard,this.templateManager=t.templateManager,this.temp=t.temp}async broadcast(t,e,i,s,a,n,o=!1){try{let d=e.map(c=>this.templateManager.getMessage(this.data.name,a,c,s));for(let c=0;c<t.length;c++){let l=t[c],p=d[c];await this.ct.api.sendMessage(l,p,{parse_mode:"HTML",reply_markup:i,...n}),c+1==30&&await(0,W.setTimeout)(1200)}}catch(r){if(o)throw r}}async reply(t,e,i,s){let a=this.templateManager.getMessage(this.data.name,i,this.userLanguage,e);return await this.ct.reply(a,{parse_mode:"HTML",reply_markup:t,...s})}async replyMedia(t,e,i,s,a){let n=this.templateManager.getMedia(this.data.name,s,this.userLanguage,i),o={parse_mode:"HTML",reply_markup:e,...a};if(t=="photo")return await this.ct.replyWithPhoto(n.src,{caption:n.caption,...o});if(t=="video")return await this.ct.replyWithVideo(n.src,{caption:n.caption,...o});if(t=="audio")return await this.ct.replyWithAudio(n.src,{caption:n.caption,...o});if(t=="document")return await this.ct.replyWithDocument(n.src,{caption:n.caption,...o});if(t=="animation")return await this.ct.replyWithAnimation(n.src,{caption:n.caption,...o});if(t=="voice")return await this.ct.replyWithVoice(n.src,o);if(t=="video_note")return await this.ct.replyWithVideoNote(n.src,o);if(t=="sticker")return await this.ct.replyWithSticker(n.src,o);if(t=="location")return await this.ct.replyWithLocation(parseInt(n.latitude),parseInt(n.longitude),o);if(t=="contact")return await this.ct.replyWithContact(n.phone,n.first_name,{vcard:n.vcard,...o})}async replyPhoto(t,e,i,s){return this.replyMedia("photo",t,e,i,s)}async replyVideo(t,e,i,s){return this.replyMedia("video",t,e,i,s)}async replyAudio(t,e,i,s){return this.replyMedia("audio",t,e,i,s)}async replyDocument(t,e,i,s){return this.replyMedia("document",t,e,i,s)}async replyAnimation(t,e,i,s){return this.replyMedia("animation",t,e,i,s)}async replyVoice(t,e,i,s){return this.replyMedia("voice",t,e,i,s)}async replyVideoNote(t,e,i,s){return this.replyMedia("video_note",t,e,i,s)}async replySticker(t,e,i,s){return this.replyMedia("sticker",t,e,i,s)}async replyLocation(t,e,i,s){return this.replyMedia("location",t,e,i,s)}async replyContact(t,e,i,s){return this.replyMedia("contact",t,e,i,s)}async replyAction(t){return await this.ct.replyWithChatAction(t)}async edit(t,e,i,s,a=!0){try{let n=this.templateManager.getMessage(this.data.name,i,this.userLanguage,e);return await this.ct.editMessageText(n,{parse_mode:"HTML",reply_markup:t,...s})}catch(n){if(a)await this.reply(t,e,i,s);else throw n}}async editMedia(t,e,i,s,a,n,o=!0){try{let{src:r,caption:d}=this.templateManager.getMedia(this.data.name,s,this.userLanguage,i);return t=="voice"?(await this.delete(),await this.replyVoice(e,i,s,a)):t=="video_note"?(await this.delete(),await this.replyVoice(e,i,s,a)):t=="sticker"?(await this.delete(),await this.replySticker(e,i,s,a)):t=="location"?(await this.delete(),await this.replyLocation(e,i,s,a)):t=="contact"?(await this.delete(),await this.replyContact(e,i,s,a)):await this.ct.editMessageMedia({media:r,caption:d,type:t,parse_mode:"HTML",...a},{reply_markup:e,...n})}catch(r){if(o===!0)await this.reply(e,i,s,n);else throw r}}async editPhoto(t,e,i,s,a){return await this.editMedia("photo",t,e,i,s,a)}async editVideo(t,e,i,s,a){return await this.editMedia("video",t,e,i,s,a)}async editAudio(t,e,i,s,a){return await this.editMedia("audio",t,e,i,s,a)}async editDocument(t,e,i,s,a){return await this.editMedia("document",t,e,i,s,a)}async editAnimation(t,e,i,s,a){return await this.editMedia("animation",t,e,i,s,a)}async editVoice(t,e,i,s){return await this.editMedia("voice",t,e,i,s)}async editVideoNote(t,e,i,s){return await this.editMedia("video_note",t,e,i,s)}async editSticker(t,e,i,s){return await this.editMedia("sticker",t,e,i,s)}async editLocation(t,e,i,s){return await this.editMedia("location",t,e,i,s)}async editContact(t,e,i,s){return await this.editMedia("contact",t,e,i,s)}async delete(){return await this.ct.deleteMessage()}async callbackQueryAnswer(t){try{return await this.ct.answerCallbackQuery(t)}catch{return null}}async sessionSet(t,e,i){try{let s={method:t,params:e,...i},a=await this.cacheSession.set(`${this.userId}`,s);return a&&(this.session=s),a}catch{return null}}async sessionGet(){try{let t=await this.cacheSession.get(`${this.userId}`);return t&&(this.session=t),t||null}catch{return null}}async sessionClear(){try{let t=await this.cacheSession.delete(`${this.userId}`);return t&&(this.session={}),t}catch{return null}}static buildData(t){return{name:"",callbackData:t?function(...e){let i=t(...e);return`${this.name}${i==""||i===void 0?"":`:${i}`}`}:function(){return this?.name}}}inlineKeyboard(t,e){let i=this.data.name;if(e?.cache===!0){let o=`${i}:${this.userLanguage}:i`,r=this.cacheKeyboard.get(o);if(!r){let d=new P.InlineKeyboard,c=this.templateManager.getKeyboard(i,e?.baseId,this.userLanguage,"inline",e?.data),l=t(d,c);return this.cacheKeyboard.set(o,l),l}return r}let s=new P.InlineKeyboard,a=this.templateManager.getKeyboard(i,e?.baseId,this.userLanguage,"inline",e?.data);return t(s,a)}bottomKeyboard(t,e){let i=this.data.name;if(e?.cache===!0){let o=`${i}:${this.userLanguage}:b`,r=this.cacheKeyboard.get(o);if(!r){let d=new P.Keyboard,c=this.templateManager.getKeyboard(i,e?.baseId,this.userLanguage,"bottom",e?.data),l=t(d,c);return l.resized(!0),this.cacheKeyboard.set(o,l),l}return r}let s=new P.Keyboard,a=this.templateManager.getKeyboard(i,e?.baseId,this.userLanguage,"bottom",e?.data),n=t(s,a);return n.resized(),n}validateSessionCallback(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionCallback(e)}validateSessionCallbackPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionCallbackPayload(e)}validateSessionText(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionText(e)}validateSessionTextPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionTextPayload(e)}validateSessionTextCommand(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionTextCommand(e)}validateSessionTextCommandPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionTextCommandPayload(e)}validateSessionTextFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionTextFree(e)}validateSessionPhotoCaption(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionPhotoCaption(e)}validateSessionPhotoCaptionPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionPhotoCaptionPayload(e)}validateSessionPhotoCaptionCommand(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionPhotoCaptionCommand(e)}validateSessionPhotoCaptionCommandPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionPhotoCaptionCommandPayload(e)}validateSessionPhotoFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionPhotoFree(e)}validateSessionVideoCaption(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoCaption(e)}validateSessionVideoCaptionPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoCaptionPayload(e)}validateSessionVideoCaptionCommand(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoCaptionCommand(e)}validateSessionVideoCaptionCommandPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoCaptionCommandPayload(e)}validateSessionVideoFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoFree(e)}validateSessionAudioCaption(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAudioCaption(e)}validateSessionAudioCaptionPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAudioCaptionPayload(e)}validateSessionAudioCaptionCommand(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAudioCaptionCommand(e)}validateSessionAudioCaptionCommandPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAudioCaptionCommandPayload(e)}validateSessionAudioFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAudioFree(e)}validateSessionDocumentCaption(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionDocumentCaption(e)}validateSessionDocumentCaptionPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionDocumentCaptionPayload(e)}validateSessionDocumentCaptionCommand(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionDocumentCaptionCommand(e)}validateSessionDocumentCaptionCommandPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionDocumentCaptionCommandPayload(e)}validateSessionDocumentFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionDocumentFree(e)}validateSessionAnimationCaption(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAnimationCaption(e)}validateSessionAnimationCaptionPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAnimationCaptionPayload(e)}validateSessionAnimationCaptionCommand(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAnimationCaptionCommand(e)}validateSessionAnimationCaptionCommandPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAnimationCaptionCommandPayload(e)}validateSessionAnimationFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAnimationFree(e)}validateSessionVoiceCaption(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVoiceCaption(e)}validateSessionVoiceCaptionPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVoiceCaptionPayload(e)}validateSessionVoiceCaptionCommand(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVoiceCaptionCommand(e)}validateSessionVoiceCaptionCommandPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVoiceCaptionCommandPayload(e)}validateSessionVoiceFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVoiceFree(e)}validateSessionVideoNoteCaption(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoNoteCaption(e)}validateSessionVideoNoteCaptionPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoNoteCaptionPayload(e)}validateSessionVideoNoteCaptionCommand(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoNoteCaptionCommand(e)}validateSessionVideoNoteCaptionCommandPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoNoteCaptionCommandPayload(e)}validateSessionVideoNoteFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoNoteFree(e)}validateSessionStickerFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionStickerFree(e)}validateSessionLocationFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionLocationFree(e)}validateSessionContactFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionContactFree(e)}sessionCallback(t,e){return this.sessionSet(this.methodSessionCallback(e),t)}sessionCallbackPayload(t,e){return this.sessionSet(this.methodSessionCallbackPayload(e),t)}sessionText(t,e){return this.sessionSet(this.methodSessionText(e),t)}sessionTextPayload(t,e){return this.sessionSet(this.methodSessionTextPayload(e),t)}sessionTextCommand(t,e){return this.sessionSet(this.methodSessionTextCommand(e),t)}sessionTextCommandPayload(t,e){return this.sessionSet(this.methodSessionTextCommandPayload(e),t)}sessionTextFree(t,e){return this.sessionSet(this.methodSessionTextFree(e),t)}sessionPhotoCaption(t,e){return this.sessionSet(this.methodSessionPhotoCaption(e),t)}sessionPhotoCaptionPayload(t,e){return this.sessionSet(this.methodSessionPhotoCaptionPayload(e),t)}sessionPhotoCaptionCommand(t,e){return this.sessionSet(this.methodSessionPhotoCaptionCommand(e),t)}sessionPhotoCaptionCommandPayload(t,e){return this.sessionSet(this.methodSessionPhotoCaptionCommandPayload(e),t)}sessionPhotoFree(t,e){return this.sessionSet(this.methodSessionPhotoFree(e),t)}sessionVideoCaption(t,e){return this.sessionSet(this.methodSessionVideoCaption(e),t)}sessionVideoCaptionPayload(t,e){return this.sessionSet(this.methodSessionVideoCaptionPayload(e),t)}sessionVideoCaptionCommand(t,e){return this.sessionSet(this.methodSessionVideoCaptionCommand(e),t)}sessionVideoCaptionCommandPayload(t,e){return this.sessionSet(this.methodSessionVideoCaptionCommandPayload(e),t)}sessionVideoFree(t,e){return this.sessionSet(this.methodSessionVideoFree(e),t)}sessionAudioCaption(t,e){return this.sessionSet(this.methodSessionAudioCaption(e),t)}sessionAudioCaptionPayload(t,e){return this.sessionSet(this.methodSessionAudioCaptionPayload(e),t)}sessionAudioCaptionCommand(t,e){return this.sessionSet(this.methodSessionAudioCaptionCommand(e),t)}sessionAudioCaptionCommandPayload(t,e){return this.sessionSet(this.methodSessionAudioCaptionCommandPayload(e),t)}sessionAudioFree(t,e){return this.sessionSet(this.methodSessionAudioFree(e),t)}sessionDocumentCaption(t,e){return this.sessionSet(this.methodSessionDocumentCaption(e),t)}sessionDocumentCaptionPayload(t,e){return this.sessionSet(this.methodSessionDocumentCaptionPayload(e),t)}sessionDocumentCaptionCommand(t,e){return this.sessionSet(this.methodSessionDocumentCaptionCommand(e),t)}sessionDocumentCaptionCommandPayload(t,e){return this.sessionSet(this.methodSessionDocumentCaptionCommandPayload(e),t)}sessionDocumentFree(t,e){return this.sessionSet(this.methodSessionDocumentFree(e),t)}sessionAnimationCaption(t,e){return this.sessionSet(this.methodSessionAnimationCaption(e),t)}sessionAnimationCaptionPayload(t,e){return this.sessionSet(this.methodSessionAnimationCaptionPayload(e),t)}sessionAnimationCaptionCommand(t,e){return this.sessionSet(this.methodSessionAnimationCaptionCommand(e),t)}sessionAnimationCaptionCommandPayload(t,e){return this.sessionSet(this.methodSessionAnimationCaptionCommandPayload(e),t)}sessionAnimationFree(t,e){return this.sessionSet(this.methodSessionAnimationFree(e),t)}sessionVoiceCaption(t,e){return this.sessionSet(this.methodSessionVoiceCaption(e),t)}sessionVoiceCaptionPayload(t,e){return this.sessionSet(this.methodSessionVoiceCaptionPayload(e),t)}sessionVoiceCaptionCommand(t,e){return this.sessionSet(this.methodSessionVoiceCaptionCommand(e),t)}sessionVoiceCaptionCommandPayload(t,e){return this.sessionSet(this.methodSessionVoiceCaptionCommandPayload(e),t)}sessionVoiceFree(t,e){return this.sessionSet(this.methodSessionVoiceFree(e),t)}sessionVideoNoteCaption(t,e){return this.sessionSet(this.methodSessionVideoNoteCaption(e),t)}sessionVideoNoteCaptionPayload(t,e){return this.sessionSet(this.methodSessionVideoNoteCaptionPayload(e),t)}sessionVideoNoteCaptionCommand(t,e){return this.sessionSet(this.methodSessionVideoNoteCaptionCommand(e),t)}sessionVideoNoteCaptionCommandPayload(t,e){return this.sessionSet(this.methodSessionVideoNoteCaptionCommandPayload(e),t)}sessionVideoNoteFree(t,e){return this.sessionSet(this.methodSessionVideoNoteFree(e),t)}sessionStickerFree(t,e){return this.sessionSet(this.methodSessionStickerFree(e),t)}sessionLocationFree(t,e){return this.sessionSet(this.methodSessionLocationFree(e),t)}sessionContactFree(t,e){return this.sessionSet(this.methodSessionContactFree(e),t)}sessionToCallback(t,e){return this.sessionCallback({...this.session.params,...t},e)}sessionToCallbackPayload(t,e){return this.sessionCallbackPayload({...this.session.params,...t},e)}sessionToText(t,e){return this.sessionText({...this.session.params,...t},e)}sessionToTextPayload(t,e){return this.sessionTextPayload({...this.session.params,...t},e)}sessionToTextCommand(t,e){return this.sessionTextCommand({...this.session.params,...t},e)}sessionToTextCommandPayload(t,e){return this.sessionTextCommandPayload({...this.session.params,...t},e)}sessionToTextFree(t,e){return this.sessionTextFree({...this.session.params,...t},e)}sessionToPhotoCaption(t,e){return this.sessionPhotoCaption({...this.session.params,...t},e)}sessionToPhotoCaptionPayload(t,e){return this.sessionPhotoCaptionPayload({...this.session.params,...t},e)}sessionToPhotoCaptionCommand(t,e){return this.sessionPhotoCaptionCommand({...this.session.params,...t},e)}sessionToPhotoCaptionCommandPayload(t,e){return this.sessionPhotoCaptionCommandPayload({...this.session.params,...t},e)}sessionToPhotoFree(t,e){return this.sessionPhotoFree({...this.session.params,...t},e)}sessionToVideoCaption(t,e){return this.sessionVideoCaption({...this.session.params,...t},e)}sessionToVideoCaptionPayload(t,e){return this.sessionVideoCaptionPayload({...this.session.params,...t},e)}sessionToVideoCaptionCommand(t,e){return this.sessionVideoCaptionCommand({...this.session.params,...t},e)}sessionToVideoCaptionCommandPayload(t,e){return this.sessionVideoCaptionCommandPayload({...this.session.params,...t},e)}sessionToVideoFree(t,e){return this.sessionVideoFree({...this.session.params,...t},e)}sessionToAudioCaption(t,e){return this.sessionAudioCaption({...this.session.params,...t},e)}sessionToAudioCaptionPayload(t,e){return this.sessionAudioCaptionPayload({...this.session.params,...t},e)}sessionToAudioCaptionCommand(t,e){return this.sessionAudioCaptionCommand({...this.session.params,...t},e)}sessionToAudioCaptionCommandPayload(t,e){return this.sessionAudioCaptionCommandPayload({...this.session.params,...t},e)}sessionToAudioFree(t,e){return this.sessionAudioFree({...this.session.params,...t},e)}sessionToDocumentCaption(t,e){return this.sessionDocumentCaption({...this.session.params,...t},e)}sessionToDocumentCaptionPayload(t,e){return this.sessionDocumentCaptionPayload({...this.session.params,...t},e)}sessionToDocumentCaptionCommand(t,e){return this.sessionDocumentCaptionCommand({...this.session.params,...t},e)}sessionToDocumentCaptionCommandPayload(t,e){return this.sessionDocumentCaptionCommandPayload({...this.session.params,...t},e)}sessionToDocumentFree(t,e){return this.sessionDocumentFree({...this.session.params,...t},e)}sessionToAnimationCaption(t,e){return this.sessionAnimationCaption({...this.session.params,...t},e)}sessionToAnimationCaptionPayload(t,e){return this.sessionAnimationCaptionPayload({...this.session.params,...t},e)}sessionToAnimationCaptionCommand(t,e){return this.sessionAnimationCaptionCommand({...this.session.params,...t},e)}sessionToAnimationCaptionCommandPayload(t,e){return this.sessionAnimationCaptionCommandPayload({...this.session.params,...t},e)}sessionToAnimationFree(t,e){return this.sessionAnimationFree({...this.session.params,...t},e)}sessionToVoiceCaption(t,e){return this.sessionVoiceCaption({...this.session.params,...t},e)}sessionToVoiceCaptionPayload(t,e){return this.sessionVoiceCaptionPayload({...this.session.params,...t},e)}sessionToVoiceCaptionCommand(t,e){return this.sessionVoiceCaptionCommand({...this.session.params,...t},e)}sessionToVoiceCaptionCommandPayload(t,e){return this.sessionVoiceCaptionCommandPayload({...this.session.params,...t},e)}sessionToVoiceFree(t,e){return this.sessionVoiceFree({...this.session.params,...t},e)}sessionToVideoNoteCaption(t,e){return this.sessionVideoNoteCaption({...this.session.params,...t},e)}sessionToVideoNoteCaptionPayload(t,e){return this.sessionVideoNoteCaptionPayload({...this.session.params,...t},e)}sessionToVideoNoteCaptionCommand(t,e){return this.sessionVideoNoteCaptionCommand({...this.session.params,...t},e)}sessionToVideoNoteCaptionCommandPayload(t,e){return this.sessionVideoNoteCaptionCommandPayload({...this.session.params,...t},e)}sessionToVideoNoteFree(t,e){return this.sessionVideoNoteFree({...this.session.params,...t},e)}sessionToStickerFree(t,e){return this.sessionStickerFree({...this.session.params,...t},e)}sessionToLocationFree(t,e){return this.sessionLocationFree({...this.session.params,...t},e)}sessionToContactFree(t,e){return this.sessionContactFree({...this.session.params,...t},e)}intent(t,e=!0){t||(t=e?this.data.callbackData():this.data.name);let i=e?":":" ";return t.indexOf(i)!=-1?t.split(i)[0]:t}methodSession(t,e){return`${t}-${e||this.data.name}`}methodSessionCallback(t){return this.methodSession("1",t)}methodSessionCallbackPayload(t){return this.methodSession("2",t)}methodSessionText(t){return this.methodSession("3",t)}methodSessionTextPayload(t){return this.methodSession("4",t)}methodSessionTextCommand(t){return this.methodSession("5",t)}methodSessionTextCommandPayload(t){return this.methodSession("6",t)}methodSessionTextFree(t){return this.methodSession("7",t)}methodSessionPhotoCaption(t){return this.methodSession("8",t)}methodSessionPhotoCaptionPayload(t){return this.methodSession("9",t)}methodSessionPhotoCaptionCommand(t){return this.methodSession("10",t)}methodSessionPhotoCaptionCommandPayload(t){return this.methodSession("11",t)}methodSessionPhotoFree(t){return this.methodSession("12",t)}methodSessionVideoCaption(t){return this.methodSession("13",t)}methodSessionVideoCaptionPayload(t){return this.methodSession("14",t)}methodSessionVideoCaptionCommand(t){return this.methodSession("15",t)}methodSessionVideoCaptionCommandPayload(t){return this.methodSession("16",t)}methodSessionVideoFree(t){return this.methodSession("17",t)}methodSessionAudioCaption(t){return this.methodSession("18",t)}methodSessionAudioCaptionPayload(t){return this.methodSession("19",t)}methodSessionAudioCaptionCommand(t){return this.methodSession("20",t)}methodSessionAudioCaptionCommandPayload(t){return this.methodSession("21",t)}methodSessionAudioFree(t){return this.methodSession("22",t)}methodSessionDocumentCaption(t){return this.methodSession("23",t)}methodSessionDocumentCaptionPayload(t){return this.methodSession("24",t)}methodSessionDocumentCaptionCommand(t){return this.methodSession("25",t)}methodSessionDocumentCaptionCommandPayload(t){return this.methodSession("26",t)}methodSessionDocumentFree(t){return this.methodSession("27",t)}methodSessionAnimationCaption(t){return this.methodSession("28",t)}methodSessionAnimationCaptionPayload(t){return this.methodSession("29",t)}methodSessionAnimationCaptionCommand(t){return this.methodSession("30",t)}methodSessionAnimationCaptionCommandPayload(t){return this.methodSession("31",t)}methodSessionAnimationFree(t){return this.methodSession("32",t)}methodSessionVoiceCaption(t){return this.methodSession("33",t)}methodSessionVoiceCaptionPayload(t){return this.methodSession("34",t)}methodSessionVoiceCaptionCommand(t){return this.methodSession("35",t)}methodSessionVoiceCaptionCommandPayload(t){return this.methodSession("36",t)}methodSessionVoiceFree(t){return this.methodSession("37",t)}methodSessionVideoNoteCaption(t){return this.methodSession("38",t)}methodSessionVideoNoteCaptionPayload(t){return this.methodSession("39",t)}methodSessionVideoNoteCaptionCommand(t){return this.methodSession("40",t)}methodSessionVideoNoteCaptionCommandPayload(t){return this.methodSession("41",t)}methodSessionVideoNoteFree(t){return this.methodSession("42",t)}methodSessionStickerFree(t){return this.methodSession("43",t)}methodSessionLocationFree(t){return this.methodSession("44",t)}methodSessionContactFree(t){return this.methodSession("45",t)}};var Q=require("bun"),V=require("path");var O=require("path"),F=require("fs");var S=class{static{m(this,"PageManager")}static _getFile(t,e,i=!0,s=0){let n=Object.values(t)[s];if(n===void 0)throw new Error(`File ${e} must have export (not default) class page`);return i===!0&&(n.template=void 0),n}static getFilePath(){return(0,O.join)(process.cwd(),"src","pages")}static getFileSync(t,e=!0,i=0){t||(t=this.getFilePath());let s=require(t);return this._getFile(s,t,e,i)}static async getFile(t,e=!0,i=0){t||(t=this.getFilePath());let s=await import(`${t}?t=${Date.now()}`);return this._getFile(s,t,e,i)}static getFileManySync(t,e=!0,i=0){if(t||(t=this.getFilePath()),!(0,F.existsSync)(t))throw new Error(`Directory ${t} is not found, please make it manually`);return(0,F.readdirSync)(t).map(a=>{let n=(0,O.join)(t,a);return{path:n,page:this.getFileSync(n,e,i)}})}};var U=require("logging-pretty");var f=require("path"),y=require("fs"),j=require("chokidar");var C=class{static{m(this,"TemplateEngine")}static _escape(t){return t.replace(/\\/g,"\\\\").replace(/`/g,"\\`")}static _processVariables(t){return t.replace(/\{\{(\w+)\}\}/g,(e,i)=>`\${(data && data["${i}"]) ?? ""}`)}static _processBaseBlocks(t){let e=/<base\b[^>]*?(?:\sid=(["']?)([^"'\s>]+)\1)?[^>]*?>([\s\S]*?)<\/base>/g,i={};for(let s of t.matchAll(e)){let a=s[2]??"default",o=(s[3]??"").replace(/^[\t ]+/gm,"").replace(/^\n+/,"").replace(/\n+$/,"").trim();i[a]=o}return i}static _processScriptBlocks(t){return t.replace(/<script\b[^>]*>([\s\S]*?)<\/script>/gi,(e,i)=>{let s=i.trim();return s?`\${(() => { if (!data) return ''; try { with(data) { ${s} } } catch(e) { return '' } })()}`:""})}static _processSpaceBlocks(t){return t.replace(/^[ \t]*<space\s*\/>[ \t]*$/gim,"")}static _processDivBlocks(t){return t.replace(/<div>([\s\S]*?)<\/div>/gi,(e,i)=>i.replace(/^[\t ]+/gm,"").replace(/^\n+/,"").replace(/\n+$/,""))}static _processExec(t){return t.replace(/{~\s*([\s\S]*?)\s*~}/g,(e,i)=>`\${(() => { try { return ${i} } catch(e) { try { with(data || {}) { return ${i} } } catch(ee) { return '' } } })()}`)}static _processMapBlocks(t){let e=/<map(?:\s+([^>]*))?>([\s\S]*?)<\/map>/gi;return t.replace(e,(i,s,a)=>{let n={},o=/(\w+)="([^"]*)"/g;if(s)for(let p of s.matchAll(o))n[p[1]]=p[2];let r=n.src;if(!r)return"";let d=n.join??"\\n",c=a.replace(/^[\t ]+/gm,"").replace(/^\n+/,"").replace(/\n+$/,"").replace(/`/g,"\\`");c=c.replace(/\{\{([\w.]+)\}\}/g,(p,u)=>u==="e"||u.startsWith("e.")?`\${${u}}`:u==="i"?"${i}":p);let l=d.replace(/"/g,'\\"');return`\${(data && data["${r}"] || []).map((e, i) => \`${c}\`).join("${l}")}`})}static _processMediaBlocks(t){let e=/<media(?:\s+([^>]*))?>([\s\S]*?)<\/media>/gi,i={};for(let s of t.matchAll(e)){let a=s[1],n=s[2],o={},r=/(\w+)="([^"]*)"/g;if(a)for(let u of a.matchAll(r))o[u[1]]=u[2];let d=o.lang||"default",c=o.type,l=n.replace(/^[\t ]+/gm,"").replace(/^\n+/,"").replace(/\n+$/,"").trim(),p={type:c};o.src&&(p.src=o.src),l&&c!=="video_note"&&c!=="sticker"&&(p.caption=l),c==="location"&&(o.latitude&&(p.latitude=o.latitude),o.longitude&&(p.longitude=o.longitude),l&&(p.caption=l)),c==="contact"&&(o.phone&&(p.phone=o.phone),o.first_name&&(p.first_name=o.first_name),o.last_name&&(p.last_name=o.last_name),o.vcard&&(p.vcard=o.vcard),l&&(p.caption=l)),i[d]=p}return i}static _processMessageBlocks(t){let e=/<message(?:\s+([^>]*))?>([\s\S]*?)<\/message>/gi,i={};for(let[,s,a]of t.matchAll(e)){let n={},o=/(\w+)="([^"]*)"/g;if(s)for(let c of s.matchAll(o))n[c[1]]=c[2];let r=n.lang||"default",d=a.replace(/^[\t ]+/gm,"").replace(/^\n+/,"").replace(/\n+$/,"").trim();i[r]=d}return i}static _processDescriptionBlocks(t){let e=/<description(?:\s+([^>]*))?>([\s\S]*?)<\/description>/gi,i={};for(let[,s,a]of t.matchAll(e)){let n={},o=/(\w+)="([^"]*)"/g;if(s)for(let l of s.matchAll(o))n[l[1]]=l[2];let r=n.lang||"default",d=n.scope||"default",c=a.replace(/^[\t ]+/gm,"").replace(/^\n+/,"").replace(/\n+$/,"").trim();i[r]||(i[r]={}),i[r][d]=c}return i}static _processKeyboardBlocks(t){let e=/<keyboard(?:\s+([^>]*))?>([\s\S]*?)<\/keyboard>/gi,i={};for(let[,s,a]of t.matchAll(e)){let n={},o=/(\w+)="([^"]*)"/g;if(s)for(let l of s.matchAll(o))n[l[1]]=l[2];let r=n.lang||"default",d=n.type==="bottom"?"bottom":"inline",c=a.trim();i[r]||(i[r]={}),i[r][d]=c}return i}static _compileToFunction(t,e){let i;e?i=t.map(n=>`\`${n}\``).join(","):i=`\`${t}\``;let s=i;if(s=this._processScriptBlocks(this._processDivBlocks(this._processSpaceBlocks(s))),e){let a=`return [${s}]`;return new Function("data",a)}else{let a=`return ${s}`;return new Function("data",a)}}static compileMedia(t){let e=this._processBaseBlocks(t),i={};for(let[s,a]of Object.entries(e)){let n=this._processMediaBlocks(a);i[s]={};for(let[o,r]of Object.entries(n)){let d={};for(let[c,l]of Object.entries(r)){if(c==="type"||typeof l!="string")continue;let p=this._processVariables(this._processExec(this._processMapBlocks(this._processScriptBlocks(l))));d[c]=this._compileToFunction(p,!1)}i[s][o]=c=>{let l={...r};for(let[p,u]of Object.entries(d))l[p]=u(c);return l}}}return i}static compileMessages(t){let e=this._processBaseBlocks(t),i={};for(let[s,a]of Object.entries(e)){let n=this._processMessageBlocks(a);i[s]={};for(let[o,r]of Object.entries(n)){let d=this._processVariables(this._processExec(this._processMapBlocks(this._processScriptBlocks(r))));i[s][o]=this._compileToFunction(d,!1)}}return i}static compileDescription(t){let e=this._processBaseBlocks(t),i={};for(let[s,a]of Object.entries(e)){let n=this._processDescriptionBlocks(a);i[s]={};for(let[o,r]of Object.entries(n))for(let[d,c]of Object.entries(r)){i[s][d]||(i[s][d]={});let l=this._processVariables(this._processExec(this._processMapBlocks(this._processScriptBlocks(c))));i[s][d][o]=this._compileToFunction(l,!1)}}return i}static compileKeyboards(t){let e=this._processBaseBlocks(t),i={};for(let[s,a]of Object.entries(e)){let n=this._processKeyboardBlocks(a);i[s]={};for(let[o,r]of Object.entries(n)){i[s][o]={};for(let[d,c]of Object.entries(r)){let p=c.split(/(?<!\\),\s*/).map(u=>u.replace(/\\,/g,",").trim()).map(u=>this._processVariables(this._processExec(this._processMapBlocks(this._processScriptBlocks(u)))));i[s][o][d]=this._compileToFunction(p,!0)}}}return i}static _extractBlock(t,e,i="default",s="all"){let a=/<base\b[^>]*?(?:\sid=(["']?)([^"'\s>]+)\1)?[^>]*?>([\s\S]*?)<\/base>/g,n=t,o=!1;for(let r of t.matchAll(a))if((r[2]??"default")===i){n=r[3]??"",o=!0;break}if(!o){let r=/<base\b/.test(t);if(!(i==="default"&&!r))return""}if(e){if(e==="all")return n;let r=Array.isArray(e)?e:[e],d=[];for(let l of r){let p=new RegExp(`([ \\t]*)<${l}(?:\\s+([^>]*))?>([\\s\\S]*?)<\\/${l}>`,"gi");for(let u of n.matchAll(p))d.push({start:u.index,content:u[0],innerContent:u[3]??""})}d.sort((l,p)=>l.start-p.start);let c=m(l=>{let p=l.split(`
2
+ `);if(p.length<=1)return l.trim();let u=p.filter(g=>g.trim().length>0);if(u.length===0)return"";let b=u.reduce((g,D)=>{let _=D.match(/^([ \t]*)/),v=_?_[1].length:0;return v<g?v:g},1/0);return p.map(g=>g.length>=b?g.slice(b):g.trimStart()).join(`
3
+ `).trim()},"processResult");return s==="all"?d.map(p=>c(p.content)).join(`
4
+ `):d.map(p=>c(p.innerContent)).join(`
5
+ `)}return n}static _processImportBlocks(t){let e=/([ \t]*)<import(?:\s+([^>]*))?\/?>/gi,i=[];return{processed:t.replace(e,(a,n,o)=>{let r={},d=/(\w+)="([^"]*)"/g;if(o)for(let u of o.matchAll(d))r[u[1]]=u[2];let c=r.src;if(!c)return"";let l;r.block&&(r.block==="all"?l="all":r.block.includes(",")?l=r.block.split(",").map(u=>u.trim()).filter(u=>u):l=r.block);let p={src:c,blocks:l,base:r.base||"default",section:r.section||"all",indent:n||""};return i.push(p),`${n}__IMPORT_${i.length-1}__`}),imports:i}}static processImportBlocks(t,e){let{processed:i,imports:s}=this._processImportBlocks(t),a=i;for(let n=0;n<s.length;n++){let o=s[n],r=e(o.src),c=(r?this._extractBlock(r,o.blocks,o.base,o.section):"").split(`
6
+ `).map((l,p)=>p===0?l:o.indent+l).join(`
7
+ `);a=a.replace(`__IMPORT_${n}__`,c)}return a}};var M=class{static{m(this,"TemplateManager")}_map=new Map;_engine=C;_watcher=[];_supportedExtensions=new Set([".html",".ts"]);_path;_isWatchEnabled;constructor(t){let{path:e,enableWatch:i=!1}=t;this._path=e,this._isWatchEnabled=i,this._validatePath(),this._initializeWatcher(t.watchOptions),this._loadAllFiles()}_validatePath(){try{if(!this._path||!Array.isArray(this._path))throw new Error("Path must be set as string array");for(let t=0;t<this._path.length;t++){let e=this._path[t];if(!(0,y.existsSync)(e))throw new Error(`Template base path is not exists: ${e}`);if(!(0,y.statSync)(e).isDirectory())throw new Error(`Template base path is not a directory: ${e}`)}return!0}catch(t){throw new Error(`Failed to validate Template base path: ${String(t)}`)}}_initializeWatcher(t){if(this._isWatchEnabled){if(this._watcher.length>0)throw new Error("Watcher already enabled");for(let e=0;e<this._path.length;e++){let i=this._path[e],s=(0,j.watch)(i,{persistent:!0,ignoreInitial:!0,awaitWriteFinish:{stabilityThreshold:100,pollInterval:50},...t});this._watcher[e]=s,s.on("change",this._handleFileChange.bind(this)),s.on("add",this._handleFileAdd.bind(this)),s.on("unlink",this._handleFileDelete.bind(this)),s.on("error",this._handleWatchError.bind(this))}}}_handleFileChange(t){let e=(0,f.basename)(t);this._isValidFile(e)&&this._loadFile(e)}_handleFileAdd(t){let e=(0,f.basename)(t);this._isValidFile(e)&&this._loadFile(e)}_handleFileDelete(t){let e=(0,f.basename)(t),i=this._getKey(e);this._map.has(i)&&this._map.delete(i)}_handleWatchError(t){}_loadAllFiles(){for(let t=0;t<this._path.length;t++){let e=this._path[t],s=(0,y.readdirSync)(e,"utf8").filter(a=>this._isValidFile(a));for(let a of s)this._loadFile(a)}}_loadFile(t){for(let e=0;e<this._path.length;e++){let i=this._path[e],s=(0,f.join)(i,t),a=this._getExtension(t),n=this._getKey(t);if(a==".html"){let o=(0,y.readFileSync)(s,"utf8");this._compile(n,o)}else if(a==".ts"){let o=S.getFileSync(s,!1);o?.template!==void 0&&this._compile(n,o.template),o.template=void 0}else throw new Error(`File ${t} is not support`)}}_getExtension(t){return(0,f.extname)(t).toLowerCase()}_readTemplateFile(t){for(let e of this._path){let i=(0,f.extname)(t).toLowerCase();if(i){let n=(0,f.join)(e,t);if((0,y.existsSync)(n)&&(0,y.statSync)(n).isFile()){if(i===".html")return(0,y.readFileSync)(n,"utf8");if(i===".ts")try{return S.getFileSync(n,!1)?.template??null}catch{return null}}continue}let s=(0,f.join)(e,`${t}.ts`);if((0,y.existsSync)(s)&&(0,y.statSync)(s).isFile())try{return S.getFileSync(s,!1)?.template??null}catch{}let a=(0,f.join)(e,`${t}.html`);if((0,y.existsSync)(a)&&(0,y.statSync)(a).isFile())return(0,y.readFileSync)(a,"utf8")}return null}_isValidFile(t){return this._supportedExtensions.has(this._getExtension(t))}_getKey(t){return t.replace(/\.[^/.]+$/,"")}_compile(t,e,i){let s=this._map.get(t),a=s||{},n=!1,o=C.processImportBlocks(e,r=>this._readTemplateFile(r));(i===void 0||i?.media===!1||s?.media===void 0)&&(a.media=C.compileMedia(o),n=!0),(i===void 0||i?.message===!1||s?.message===void 0)&&(a.message=C.compileMessages(o),n=!0),(i===void 0||i?.keyboard===!1||s?.keyboard===void 0)&&(a.keyboard=C.compileKeyboards(o),n=!0),(i===void 0||i?.description===!1||s?.description===void 0)&&(a.description=C.compileDescription(o),n=!0),n&&this._map.set(t,a)}getMedia(t,e="default",i="default",s){let n=this._map.get(t)?.media?.[e],o=n?.[i]||n?.default;try{if(!o)throw new Error("Media function not found, please check your template");return o(s)}catch{return{}}}getMessage(t,e="default",i="default",s){try{let n=this._map.get(t)?.message?.[e],o=n?.[i]||n?.default;if(!o)throw"";return o(s)}catch{return""}}getDescription(t,e="default",i="default",s="default",a){try{let o=this._map.get(t)?.description?.[e]?.[s],r=o?.[i]||o?.default;if(!r)throw"";return r(a)}catch{return""}}getDescriptionLanguage(t,e="default",i="default"){try{let a=this._map.get(t)?.description?.[e]?.[i];return a?Object.keys(a):[]}catch{return[]}}getDescriptionScopes(t,e="default"){try{let s=this._map.get(t)?.description?.[e];return s?Object.keys(s):[]}catch{return[]}}getKeyboard(t,e="default",i="default",s="inline",a){try{let o=this._map.get(t)?.keyboard?.[e],r=o?.[i]?.[s]||o?.default?.[s];if(!r)throw[];return r(a)}catch{return[]}}async write(t,e){if(this._getExtension(t)!=".html")return null;for(let i=0;i<this._path.length;i++){let s=(0,f.join)(this._path[i],t);if((0,y.existsSync)(s))return(0,y.writeFileSync)(s,e,"utf8"),this._loadFile(t),!0}return null}has(t){return this._map.has(t)}async destroy(){if(this._watcher)for(let t of this._watcher)await t.close();this._map.clear()}};var T=require("grammy");var A=class{static{m(this,"Gramstax")}templateManager;cacheKeyboard;cacheSession;pages;log;bot;handlerBotUpdate;constructor(t){if(!t?.token||t?.token?.length==0)throw new Error("Token is not valid or empty");if(this.log=t.log===!0?new U.LoggingPretty:t.log?t.log:void 0,this.cacheSession=t.cacheSession||new I("memory","session"),this.cacheKeyboard=t.cacheKeyboard||new Map,this.templateManager=t.templateManager||new M({path:[(0,V.join)(process.cwd(),"src","pages")]}),this.bot=this.createBot(t.token),this.bot.catch(this.onCatch),this.registerOnCallbackQueryData(),this.registerOnMessageText(),this.registerOnMessagePhoto(),this.registerOnMessageVideo(),this.registerOnMessageAudio(),this.registerOnMessageDocument(),this.registerOnMessageAnimation(),this.registerOnMessageVoice(),this.registerOnMessageVideoNote(),this.registerOnMessageSticker(),this.registerOnMessageLocation(),this.registerOnMessageContact(),this.handlerBotUpdate=this.bot.handleUpdate,this.pages=this.pageLoads(),process.once("SIGINT",()=>this.bot.stop()),process.once("SIGTERM",()=>this.bot.stop()),t.deploy.startsWith("polling"))this.runPolling();else if(t.deploy.startsWith("webhook:"))this.runWebhook(t.deploy.split("webhook:")[1]);else if(t.deploy.startsWith("serverless:"))this.runServerless(t.deploy.split("serverless:")[1]);else if(!t.deploy.startsWith("test"))throw new Error("Params deploy is not valid, expected: polling, webhook:<public-url>, serverless:<your-adapter>")}onCatch(t){this.log?.error?.(`[Bot.catch]: ${JSON.stringify(t,null,2)}`)}async onStart(t,e,i){await this.executeSyncBlocksDescription(this.pages);let{username:s}=t;this.log?.success?.(`Telegram bot polling started: ${s} ${e=="webhook"||e=="serverless"?`(${i})`:""}`)}createBot(t){return new T.Bot(t)}createCtx(t){return new x({ct:t,cacheSession:this.cacheSession,cacheKeyboard:this.cacheKeyboard,templateManager:this.templateManager,temp:{session:{},data:{}}})}runPolling(){this.bot.start({onStart:m(t=>{this.onStart(t,"polling",null)},"onStart")})}async runWebhook(t){let e=(0,T.webhookCallback)(this.bot,"bun");this.handlerBotUpdate=e;let i=new URL(t).pathname;(0,Q.serve)({fetch(n){let{pathname:o}=new URL(n.url);if(n.method=="POST"&&o==i)return e(n);if(n.method=="GET"&&o=="/"){let r=new Date().toISOString();return new Response(JSON.stringify({status:"OK",timestamp:r}),{status:200,headers:{"Content-Type":"application/json"}})}return new Response("Not found",{status:404})}}),(await this.bot.api.getWebhookInfo()).url!=t&&await this.bot.api.setWebhook(t);let a=await this.bot.api.getMe();await this.onStart(a,"webhook",t)}async runServerless(t){this.handlerBotUpdate=(0,T.webhookCallback)(this.bot,t);let e=await this.bot.api.getMe();await this.onStart(e,"serverless",t)}pageBuildData(t,e){t?.data===void 0&&(t.data=t.buildData()),t.data.name=(0,V.parse)(e).name}pageSorts(t,e){let i=t.data.name,s=t.prototype,a="Payload",n="Command",o="Caption",r="Free",d=e.routeDynamic,c=e.routeDynamicSpesific,l=m(b=>b.charAt(0).toUpperCase()+b.slice(1),"upFirst"),p=m(b=>b.replace(a,"").replace(n,"").replace(o,"").replace(r,""),"getPureRouteName"),u=m((b,g)=>s[`methodSession${l(b)}`]?.(g),"methodSession");e.all[i]=t;for(let b=0;b<e.lenListRouteStaticSession;b++){let g=e.listRouteStaticSession[b],D=e.routeStatic[g],_=e.routeStaticIntent[g],v=e.routeStaticSession[g],R={name:i,routeName:g},N=D||_;if(N!==void 0){let J=g.includes(n)?`/${i}`:i;N[s.intent(J,g.startsWith("callback"))]=R}v!==void 0&&(v[u(g,i)]=R),g.toLowerCase().endsWith("free")&&c[p(g)].push(R)}s.free&&d.push(i)}async executeSyncBlocksDescription(t){let e=new Map,i=t.routeStatic.textCommand;for(let[,s]of Object.entries(i)){let a=s.name,n=this.templateManager.getDescriptionScopes(a);for(let o of n){e.has(o)||e.set(o,new Set);let r=this.templateManager.getDescriptionLanguage(a,"default",o);for(let d of r)e.get(o).add(d)}}for(let[s,a]of e)for(let n of a){let o=[];for(let[,c]of Object.entries(i)){let l=c.name,p=this.templateManager.getDescription(l,"default",n,s);p&&o.push({command:l,description:p})}if(o.length===0)continue;let r={type:s},d=n==="default"?void 0:n;try{let c=await this.bot.api.getMyCommands({scope:r,language_code:d}),l=JSON.stringify(c.map(u=>({command:u.command,description:u.description})).sort((u,b)=>u.command.localeCompare(b.command))),p=JSON.stringify(o.sort((u,b)=>u.command.localeCompare(b.command)));l!==p&&(await this.bot.api.setMyCommands(o,{scope:r,language_code:d}),this.log?.info?.(`Synced descriptions for scope: ${s},${d?` lang: ${d},`:""} total (${o.length})`))}catch(c){this.log?.error?.(`Failed to sync descriptions for scope: ${s},${d?` lang: ${d},`:""} error: ${String(c)}`)}}}pageLoads(){this.log?.info?.("Load pages..");let t=["callback","text","textCommand","photoCaption","photoCaptionCommand","videoCaption","videoCaptionCommand","audioCaption","audioCaptionCommand","documentCaption","documentCaptionCommand","animationCaption","animationCaptionCommand","voiceCaption","voiceCaptionCommand","videoNoteCaption","videoNoteCaptionCommand"],e=["callbackPayload","textPayload","textCommandPayload","photoCaptionPayload","photoCaptionCommandPayload","videoCaptionPayload","videoCaptionCommandPayload","audioCaptionPayload","audioCaptionCommandPayload","documentCaptionPayload","documentCaptionCommandPayload","animationCaptionPayload","animationCaptionCommandPayload","voiceCaptionPayload","voiceCaptionCommandPayload","videoNoteCaptionPayload","videoNoteCaptionCommandPayload"],i=[...t,...e,"textFree","photoFree","videoFree","audioFree","documentFree","animationFree","voiceFree","videoNoteFree","stickerFree","locationFree","contactFree"],s=m((o,r)=>Object.fromEntries(o.map(d=>[d,r()])),"makeObject"),a=m(()=>[],"initDynamicSpesific"),n={all:{},routeDynamic:[],routeDynamicSpesific:{text:a(),photo:a(),video:a(),audio:a(),document:a(),animation:a(),voice:a(),videoNote:a(),sticker:a(),location:a(),contact:a()},routeStatic:s(t,()=>({})),routeStaticIntent:s(e,()=>({})),routeStaticSession:s(i,()=>({})),listRouteStatic:t,listRouteStaticIntent:e,listRouteStaticSession:i,lenListRouteStatic:t.length,lenListRouteStaticIntent:e.length,lenListRouteStaticSession:i.length};return S.getFileManySync(void 0,!0).forEach(({path:o,page:r})=>{this.pageBuildData(r,o),this.pageSorts(r,n)}),this.log?.info?.(`Finish load pages with total (${Object.keys(n.all).length})`),n}async pageRoutes(t,e){let i,s;if(i===void 0){let n=t.callbackData,o=t.msgText,r=t.msgCaption,d=this.pages.routeStatic;for(let c=0;c<this.pages.lenListRouteStatic;c++){let l=this.pages.listRouteStatic[c];if(l.startsWith(e)&&(s=d[l][n||o||r]),s)break}s!==void 0&&(i=this.pages.all[s.name])}if(i===void 0){let n=await t.sessionGet();if(n){let o=n.method,r=this.pages.routeStaticSession;for(let d=0;d<this.pages.lenListRouteStaticSession;d++){let c=this.pages.listRouteStaticSession[d];if(c.startsWith(e)&&(s=r[c][o]),s)break}s!==void 0&&(i=this.pages.all[s.name])}}if(i===void 0){let n=t.callbackDataIntent,o=t.msgTextIntent,r=t.msgCaptionIntent,d=this.pages.routeStaticIntent;for(let c=0;c<this.pages.lenListRouteStaticIntent;c++){let l=this.pages.listRouteStaticIntent[c];if(l.startsWith(e)&&(s=d[l][n||o||r]),s)break}s!==void 0&&(i=this.pages.all[s.name])}if(i!==void 0&&s!==void 0)return i.prototype[s.routeName]?await new i(t)[s.routeName]?.():void 0;let a=this.pages.routeDynamicSpesific[e]||[];for(let n=0;n<a.length;n++){let o=a[n];if(o===void 0)continue;let r=this.pages.all[o.name];if(r===void 0||!r.prototype[o.routeName])continue;let c=await new r(t)[o.routeName]?.();if(c!==null)return c}for(let n=0;n<this.pages.routeDynamic.length;n++){let o=this.pages.routeDynamic[n];if(o===void 0)continue;let r=this.pages.all[o];if(r===void 0||!r.prototype.free)continue;let c=await new r(t).free?.();if(c!==null)return c}return null}async hookBeforeRoute(t,e){return!!(t||e)}async hookErrorPage(t,e,i,s){}async hookErrorInputNotFoundPage(t){}async onMessage(t,e){let i=this.createCtx(t);try{if(await this.hookBeforeRoute(i,e)===!1)return;await this.pageRoutes(i,e)===null&&await this.hookErrorInputNotFoundPage(i)}catch(s){await this.hookErrorPage(i,e,s,!1)}}async onCallbackQueryData(t){let e=this.createCtx(t);try{if(await e.callbackQueryAnswer(),await this.hookBeforeRoute(e,"onCallbackQueryData")===!1)return;await this.pageRoutes(e,"callback")}catch(i){await this.hookErrorPage(e,"onCallbackQueryData",i,!0)}}async onMessageText(t){await this.onMessage(t,"text")}async onMessagePhoto(t){await this.onMessage(t,"photo")}async onMessageVideo(t){await this.onMessage(t,"video")}async onMessageAudio(t){await this.onMessage(t,"audio")}async onMessageDocument(t){await this.onMessage(t,"document")}async onMessageAnimation(t){await this.onMessage(t,"animation")}async onMessageVoice(t){await this.onMessage(t,"voice")}async onMessageVideoNote(t){await this.onMessage(t,"videoNote")}async onMessageSticker(t){await this.onMessage(t,"sticker")}async onMessageLocation(t){await this.onMessage(t,"location")}async onMessageContact(t){await this.onMessage(t,"contact")}registerOnCallbackQueryData(){this.bot.on("callback_query:data",this.onCallbackQueryData.bind(this))}registerOnMessageText(){this.bot.on("message:text",this.onMessageText.bind(this))}registerOnMessagePhoto(){this.bot.on("message:photo",this.onMessagePhoto.bind(this))}registerOnMessageVideo(){this.bot.on("message:video",this.onMessageVideo.bind(this))}registerOnMessageAudio(){this.bot.on("message:audio",this.onMessageAudio.bind(this))}registerOnMessageDocument(){this.bot.on("message:document",this.onMessageDocument.bind(this))}registerOnMessageAnimation(){this.bot.on("message:animation",this.onMessageAnimation.bind(this))}registerOnMessageVoice(){this.bot.on("message:voice",this.onMessageVoice.bind(this))}registerOnMessageVideoNote(){this.bot.on("message:video_note",this.onMessageVideoNote.bind(this))}registerOnMessageSticker(){this.bot.on("message:sticker",this.onMessageSticker.bind(this))}registerOnMessageLocation(){this.bot.on("message:location",this.onMessageLocation.bind(this))}registerOnMessageContact(){this.bot.on("message:contact",this.onMessageContact.bind(this))}};0&&(module.exports={CacheExternal,Ctx,Gramstax,LoggingUtils,TemplateEngine,TemplateManager,log});
@@ -85,6 +85,18 @@ declare class TemplateEngine {
85
85
  };
86
86
  };
87
87
  };
88
+ static _extractBlock(content: string, blocks?: string | string[], baseId?: string, section?: `all` | `value`): string;
89
+ static _processImportBlocks(template: string): {
90
+ processed: string;
91
+ imports: {
92
+ src: string;
93
+ blocks?: string | string[];
94
+ base: string;
95
+ section: `all` | `value`;
96
+ indent: string;
97
+ }[];
98
+ };
99
+ static processImportBlocks(template: string, resolver: (src: string) => string | null): string;
88
100
  }
89
101
 
90
102
  /** Template file interface for better type safety */
@@ -129,6 +141,7 @@ declare class TemplateManager {
129
141
  /** Load and compile a single template file */
130
142
  _loadFile(filename: string): void;
131
143
  _getExtension(filename: string): string;
144
+ _readTemplateFile(filename: string): string | null;
132
145
  _isValidFile(filename: string): boolean;
133
146
  _getKey(filename: string): string;
134
147
  _compile(key: string, template: string, keepIfExist?: Partial<{
@@ -85,6 +85,18 @@ declare class TemplateEngine {
85
85
  };
86
86
  };
87
87
  };
88
+ static _extractBlock(content: string, blocks?: string | string[], baseId?: string, section?: `all` | `value`): string;
89
+ static _processImportBlocks(template: string): {
90
+ processed: string;
91
+ imports: {
92
+ src: string;
93
+ blocks?: string | string[];
94
+ base: string;
95
+ section: `all` | `value`;
96
+ indent: string;
97
+ }[];
98
+ };
99
+ static processImportBlocks(template: string, resolver: (src: string) => string | null): string;
88
100
  }
89
101
 
90
102
  /** Template file interface for better type safety */
@@ -129,6 +141,7 @@ declare class TemplateManager {
129
141
  /** Load and compile a single template file */
130
142
  _loadFile(filename: string): void;
131
143
  _getExtension(filename: string): string;
144
+ _readTemplateFile(filename: string): string | null;
132
145
  _isValidFile(filename: string): boolean;
133
146
  _getKey(filename: string): string;
134
147
  _compile(key: string, template: string, keepIfExist?: Partial<{
package/dist/src/index.js CHANGED
@@ -1 +1,7 @@
1
- var B=Object.defineProperty;var u=(y,t)=>B(y,"name",{value:t,configurable:!0}),T=(y=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(y,{get:(t,e)=>(typeof require<"u"?require:t)[e]}):y)(function(y){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+y+'" is not supported')});import $ from"logging-pretty";var I=class extends ${static{u(this,"LoggingUtils")}constructor(t){super(t)}errorMake(t,e,i){this.error(`[${e?.name||e}:${i}] ${String(t)}`)}},ot=new I;import{Keyv as E}from"keyv";import L from"@keyv/redis";var C=class extends E{constructor(e="memory",i,s,o){let n;e.startsWith("redis")&&(n=new L(e)),e==="memory"&&(n=new Map),o?.forceStore&&(n=o.forceStore);super({store:n,namespace:i,ttl:s});this.url=e}static{u(this,"CacheExternal")}};import{setTimeout as W}from"timers/promises";import{InlineKeyboard as w,Keyboard as _}from"grammy";var S=class{static{u(this,"Ctx")}static template;static data;data=this.constructor.data;get userIsPremium(){return this.ct.from?.is_premium??!1}get userIsBot(){return this.ct.from?.is_bot??!1}get userId(){return this.ct.from?.id}get userName(){return this.ct.from?.username}get userFullName(){return`${this.ct.from?.first_name}${this.ct.from?.last_name}`}get userFirstName(){return this.ct.from?.first_name}get userLastName(){return this.ct.from?.last_name}get userLanguage(){return this.ct.from?.language_code}set userLanguage(t){this.ct.from.language_code=t}get msgText(){return this.ct.message?.text}get msgTextIntent(){return this.msgText&&this.msgText.indexOf(" ")!=-1?this.msgText.split(" ")[0]:""}get msgTextPayloads(){return this.msgText&&this.msgText.indexOf(" ")!=-1?this.msgText.split(" ").slice(1):[]}get msgTextPayload(){return this.msgTextPayloads[0]}get msgCaption(){return this.ct.message?.caption}get msgCaptionIntent(){return this.msgCaption&&this.msgCaption.indexOf(" ")!=-1?this.msgCaption.split(" ")[0]:""}get msgPhoto(){return this.ct.message?.photo}get msgVideo(){return this.ct.message?.video}get msgAudio(){return this.ct.message?.audio}get callbackData(){return this.ct.callbackQuery?.data}get callbackDataIntent(){return this.callbackData&&this.callbackData.indexOf(":")!=-1?this.callbackData.split(":")[0]:""}get callbackDataPayloads(){return this.callbackData&&this.callbackData.indexOf(":")!=-1?this.callbackData.split(":").slice(1):[]}get callbackDataPayload(){return this.callbackDataPayloads?.[0]}get callbackDataParts(){return this.callbackDataPayload&&this.callbackDataPayload.indexOf("+")!=-1?this.callbackDataPayload.split("+"):[]}set session(t){this.temp.session=t}get session(){return this.temp.session}ct;cacheSession;cacheKeyboard;templateManager;temp;constructor(t){this.ct=t.ct,this.cacheSession=t.cacheSession,this.cacheKeyboard=t.cacheKeyboard,this.templateManager=t.templateManager,this.temp=t.temp}async broadcast(t,e,i,s,o,n,a=!1){try{let d=e.map(c=>this.templateManager.getMessage(this.data.name,o,c,s));for(let c=0;c<t.length;c++){let l=t[c],p=d[c];await this.ct.api.sendMessage(l,p,{parse_mode:"HTML",reply_markup:i,...n}),c+1==30&&await W(1200)}}catch(r){if(a)throw r}}async reply(t,e,i,s){let o=this.templateManager.getMessage(this.data.name,i,this.userLanguage,e);return await this.ct.reply(o,{parse_mode:"HTML",reply_markup:t,...s})}async replyMedia(t,e,i,s,o){let n=this.templateManager.getMedia(this.data.name,s,this.userLanguage,i),a={parse_mode:"HTML",reply_markup:e,...o};if(t=="photo")return await this.ct.replyWithPhoto(n.src,{caption:n.caption,...a});if(t=="video")return await this.ct.replyWithVideo(n.src,{caption:n.caption,...a});if(t=="audio")return await this.ct.replyWithAudio(n.src,{caption:n.caption,...a});if(t=="document")return await this.ct.replyWithDocument(n.src,{caption:n.caption,...a});if(t=="animation")return await this.ct.replyWithAnimation(n.src,{caption:n.caption,...a});if(t=="voice")return await this.ct.replyWithVoice(n.src,a);if(t=="video_note")return await this.ct.replyWithVideoNote(n.src,a);if(t=="sticker")return await this.ct.replyWithSticker(n.src,a);if(t=="location")return await this.ct.replyWithLocation(parseInt(n.latitude),parseInt(n.longitude),a);if(t=="contact")return await this.ct.replyWithContact(n.phone,n.first_name,{vcard:n.vcard,...a})}async replyPhoto(t,e,i,s){return this.replyMedia("photo",t,e,i,s)}async replyVideo(t,e,i,s){return this.replyMedia("video",t,e,i,s)}async replyAudio(t,e,i,s){return this.replyMedia("audio",t,e,i,s)}async replyDocument(t,e,i,s){return this.replyMedia("document",t,e,i,s)}async replyAnimation(t,e,i,s){return this.replyMedia("animation",t,e,i,s)}async replyVoice(t,e,i,s){return this.replyMedia("voice",t,e,i,s)}async replyVideoNote(t,e,i,s){return this.replyMedia("video_note",t,e,i,s)}async replySticker(t,e,i,s){return this.replyMedia("sticker",t,e,i,s)}async replyLocation(t,e,i,s){return this.replyMedia("location",t,e,i,s)}async replyContact(t,e,i,s){return this.replyMedia("contact",t,e,i,s)}async replyAction(t){return await this.ct.replyWithChatAction(t)}async edit(t,e,i,s,o=!0){try{let n=this.templateManager.getMessage(this.data.name,i,this.userLanguage,e);return await this.ct.editMessageText(n,{parse_mode:"HTML",reply_markup:t,...s})}catch(n){if(o)await this.reply(t,e,i,s);else throw n}}async editMedia(t,e,i,s,o,n,a=!0){try{let{src:r,caption:d}=this.templateManager.getMedia(this.data.name,s,this.userLanguage,i);return t=="voice"?(await this.delete(),await this.replyVoice(e,i,s,o)):t=="video_note"?(await this.delete(),await this.replyVoice(e,i,s,o)):t=="sticker"?(await this.delete(),await this.replySticker(e,i,s,o)):t=="location"?(await this.delete(),await this.replyLocation(e,i,s,o)):t=="contact"?(await this.delete(),await this.replyContact(e,i,s,o)):await this.ct.editMessageMedia({media:r,caption:d,type:t,parse_mode:"HTML",...o},{reply_markup:e,...n})}catch(r){if(a===!0)await this.reply(e,i,s,n);else throw r}}async editPhoto(t,e,i,s,o){return await this.editMedia("photo",t,e,i,s,o)}async editVideo(t,e,i,s,o){return await this.editMedia("video",t,e,i,s,o)}async editAudio(t,e,i,s,o){return await this.editMedia("audio",t,e,i,s,o)}async editDocument(t,e,i,s,o){return await this.editMedia("document",t,e,i,s,o)}async editAnimation(t,e,i,s,o){return await this.editMedia("animation",t,e,i,s,o)}async editVoice(t,e,i,s){return await this.editMedia("voice",t,e,i,s)}async editVideoNote(t,e,i,s){return await this.editMedia("video_note",t,e,i,s)}async editSticker(t,e,i,s){return await this.editMedia("sticker",t,e,i,s)}async editLocation(t,e,i,s){return await this.editMedia("location",t,e,i,s)}async editContact(t,e,i,s){return await this.editMedia("contact",t,e,i,s)}async delete(){return await this.ct.deleteMessage()}async callbackQueryAnswer(t){try{return await this.ct.answerCallbackQuery(t)}catch{return null}}async sessionSet(t,e,i){try{let s={method:t,params:e,...i},o=await this.cacheSession.set(`${this.userId}`,s);return o&&(this.session=s),o}catch{return null}}async sessionGet(){try{let t=await this.cacheSession.get(`${this.userId}`);return t&&(this.session=t),t||null}catch{return null}}async sessionClear(){try{let t=await this.cacheSession.delete(`${this.userId}`);return t&&(this.session={}),t}catch{return null}}static buildData(t){return{name:"",callbackData:t?function(...e){let i=t(...e);return`${this.name}${i==""||i===void 0?"":`:${i}`}`}:function(){return this?.name}}}inlineKeyboard(t,e){let i=this.data.name;if(e?.cache===!0){let a=`${i}:${this.userLanguage}:i`,r=this.cacheKeyboard.get(a);if(!r){let d=new w,c=this.templateManager.getKeyboard(i,e?.baseId,this.userLanguage,"inline",e?.data),l=t(d,c);return this.cacheKeyboard.set(a,l),l}return r}let s=new w,o=this.templateManager.getKeyboard(i,e?.baseId,this.userLanguage,"inline",e?.data);return t(s,o)}bottomKeyboard(t,e){let i=this.data.name;if(e?.cache===!0){let a=`${i}:${this.userLanguage}:b`,r=this.cacheKeyboard.get(a);if(!r){let d=new _,c=this.templateManager.getKeyboard(i,e?.baseId,this.userLanguage,"bottom",e?.data),l=t(d,c);return l.resized(!0),this.cacheKeyboard.set(a,l),l}return r}let s=new _,o=this.templateManager.getKeyboard(i,e?.baseId,this.userLanguage,"bottom",e?.data),n=t(s,o);return n.resized(),n}validateSessionCallback(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionCallback(e)}validateSessionCallbackPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionCallbackPayload(e)}validateSessionText(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionText(e)}validateSessionTextPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionTextPayload(e)}validateSessionTextCommand(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionTextCommand(e)}validateSessionTextCommandPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionTextCommandPayload(e)}validateSessionTextFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionTextFree(e)}validateSessionPhotoCaption(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionPhotoCaption(e)}validateSessionPhotoCaptionPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionPhotoCaptionPayload(e)}validateSessionPhotoCaptionCommand(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionPhotoCaptionCommand(e)}validateSessionPhotoCaptionCommandPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionPhotoCaptionCommandPayload(e)}validateSessionPhotoFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionPhotoFree(e)}validateSessionVideoCaption(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoCaption(e)}validateSessionVideoCaptionPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoCaptionPayload(e)}validateSessionVideoCaptionCommand(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoCaptionCommand(e)}validateSessionVideoCaptionCommandPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoCaptionCommandPayload(e)}validateSessionVideoFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoFree(e)}validateSessionAudioCaption(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAudioCaption(e)}validateSessionAudioCaptionPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAudioCaptionPayload(e)}validateSessionAudioCaptionCommand(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAudioCaptionCommand(e)}validateSessionAudioCaptionCommandPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAudioCaptionCommandPayload(e)}validateSessionAudioFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAudioFree(e)}validateSessionDocumentCaption(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionDocumentCaption(e)}validateSessionDocumentCaptionPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionDocumentCaptionPayload(e)}validateSessionDocumentCaptionCommand(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionDocumentCaptionCommand(e)}validateSessionDocumentCaptionCommandPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionDocumentCaptionCommandPayload(e)}validateSessionDocumentFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionDocumentFree(e)}validateSessionAnimationCaption(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAnimationCaption(e)}validateSessionAnimationCaptionPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAnimationCaptionPayload(e)}validateSessionAnimationCaptionCommand(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAnimationCaptionCommand(e)}validateSessionAnimationCaptionCommandPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAnimationCaptionCommandPayload(e)}validateSessionAnimationFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAnimationFree(e)}validateSessionVoiceCaption(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVoiceCaption(e)}validateSessionVoiceCaptionPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVoiceCaptionPayload(e)}validateSessionVoiceCaptionCommand(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVoiceCaptionCommand(e)}validateSessionVoiceCaptionCommandPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVoiceCaptionCommandPayload(e)}validateSessionVoiceFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVoiceFree(e)}validateSessionVideoNoteCaption(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoNoteCaption(e)}validateSessionVideoNoteCaptionPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoNoteCaptionPayload(e)}validateSessionVideoNoteCaptionCommand(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoNoteCaptionCommand(e)}validateSessionVideoNoteCaptionCommandPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoNoteCaptionCommandPayload(e)}validateSessionVideoNoteFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoNoteFree(e)}validateSessionStickerFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionStickerFree(e)}validateSessionLocationFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionLocationFree(e)}validateSessionContactFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionContactFree(e)}sessionCallback(t,e){return this.sessionSet(this.methodSessionCallback(e),t)}sessionCallbackPayload(t,e){return this.sessionSet(this.methodSessionCallbackPayload(e),t)}sessionText(t,e){return this.sessionSet(this.methodSessionText(e),t)}sessionTextPayload(t,e){return this.sessionSet(this.methodSessionTextPayload(e),t)}sessionTextCommand(t,e){return this.sessionSet(this.methodSessionTextCommand(e),t)}sessionTextCommandPayload(t,e){return this.sessionSet(this.methodSessionTextCommandPayload(e),t)}sessionTextFree(t,e){return this.sessionSet(this.methodSessionTextFree(e),t)}sessionPhotoCaption(t,e){return this.sessionSet(this.methodSessionPhotoCaption(e),t)}sessionPhotoCaptionPayload(t,e){return this.sessionSet(this.methodSessionPhotoCaptionPayload(e),t)}sessionPhotoCaptionCommand(t,e){return this.sessionSet(this.methodSessionPhotoCaptionCommand(e),t)}sessionPhotoCaptionCommandPayload(t,e){return this.sessionSet(this.methodSessionPhotoCaptionCommandPayload(e),t)}sessionPhotoFree(t,e){return this.sessionSet(this.methodSessionPhotoFree(e),t)}sessionVideoCaption(t,e){return this.sessionSet(this.methodSessionVideoCaption(e),t)}sessionVideoCaptionPayload(t,e){return this.sessionSet(this.methodSessionVideoCaptionPayload(e),t)}sessionVideoCaptionCommand(t,e){return this.sessionSet(this.methodSessionVideoCaptionCommand(e),t)}sessionVideoCaptionCommandPayload(t,e){return this.sessionSet(this.methodSessionVideoCaptionCommandPayload(e),t)}sessionVideoFree(t,e){return this.sessionSet(this.methodSessionVideoFree(e),t)}sessionAudioCaption(t,e){return this.sessionSet(this.methodSessionAudioCaption(e),t)}sessionAudioCaptionPayload(t,e){return this.sessionSet(this.methodSessionAudioCaptionPayload(e),t)}sessionAudioCaptionCommand(t,e){return this.sessionSet(this.methodSessionAudioCaptionCommand(e),t)}sessionAudioCaptionCommandPayload(t,e){return this.sessionSet(this.methodSessionAudioCaptionCommandPayload(e),t)}sessionAudioFree(t,e){return this.sessionSet(this.methodSessionAudioFree(e),t)}sessionDocumentCaption(t,e){return this.sessionSet(this.methodSessionDocumentCaption(e),t)}sessionDocumentCaptionPayload(t,e){return this.sessionSet(this.methodSessionDocumentCaptionPayload(e),t)}sessionDocumentCaptionCommand(t,e){return this.sessionSet(this.methodSessionDocumentCaptionCommand(e),t)}sessionDocumentCaptionCommandPayload(t,e){return this.sessionSet(this.methodSessionDocumentCaptionCommandPayload(e),t)}sessionDocumentFree(t,e){return this.sessionSet(this.methodSessionDocumentFree(e),t)}sessionAnimationCaption(t,e){return this.sessionSet(this.methodSessionAnimationCaption(e),t)}sessionAnimationCaptionPayload(t,e){return this.sessionSet(this.methodSessionAnimationCaptionPayload(e),t)}sessionAnimationCaptionCommand(t,e){return this.sessionSet(this.methodSessionAnimationCaptionCommand(e),t)}sessionAnimationCaptionCommandPayload(t,e){return this.sessionSet(this.methodSessionAnimationCaptionCommandPayload(e),t)}sessionAnimationFree(t,e){return this.sessionSet(this.methodSessionAnimationFree(e),t)}sessionVoiceCaption(t,e){return this.sessionSet(this.methodSessionVoiceCaption(e),t)}sessionVoiceCaptionPayload(t,e){return this.sessionSet(this.methodSessionVoiceCaptionPayload(e),t)}sessionVoiceCaptionCommand(t,e){return this.sessionSet(this.methodSessionVoiceCaptionCommand(e),t)}sessionVoiceCaptionCommandPayload(t,e){return this.sessionSet(this.methodSessionVoiceCaptionCommandPayload(e),t)}sessionVoiceFree(t,e){return this.sessionSet(this.methodSessionVoiceFree(e),t)}sessionVideoNoteCaption(t,e){return this.sessionSet(this.methodSessionVideoNoteCaption(e),t)}sessionVideoNoteCaptionPayload(t,e){return this.sessionSet(this.methodSessionVideoNoteCaptionPayload(e),t)}sessionVideoNoteCaptionCommand(t,e){return this.sessionSet(this.methodSessionVideoNoteCaptionCommand(e),t)}sessionVideoNoteCaptionCommandPayload(t,e){return this.sessionSet(this.methodSessionVideoNoteCaptionCommandPayload(e),t)}sessionVideoNoteFree(t,e){return this.sessionSet(this.methodSessionVideoNoteFree(e),t)}sessionStickerFree(t,e){return this.sessionSet(this.methodSessionStickerFree(e),t)}sessionLocationFree(t,e){return this.sessionSet(this.methodSessionLocationFree(e),t)}sessionContactFree(t,e){return this.sessionSet(this.methodSessionContactFree(e),t)}sessionToCallback(t,e){return this.sessionCallback({...this.session.params,...t},e)}sessionToCallbackPayload(t,e){return this.sessionCallbackPayload({...this.session.params,...t},e)}sessionToText(t,e){return this.sessionText({...this.session.params,...t},e)}sessionToTextPayload(t,e){return this.sessionTextPayload({...this.session.params,...t},e)}sessionToTextCommand(t,e){return this.sessionTextCommand({...this.session.params,...t},e)}sessionToTextCommandPayload(t,e){return this.sessionTextCommandPayload({...this.session.params,...t},e)}sessionToTextFree(t,e){return this.sessionTextFree({...this.session.params,...t},e)}sessionToPhotoCaption(t,e){return this.sessionPhotoCaption({...this.session.params,...t},e)}sessionToPhotoCaptionPayload(t,e){return this.sessionPhotoCaptionPayload({...this.session.params,...t},e)}sessionToPhotoCaptionCommand(t,e){return this.sessionPhotoCaptionCommand({...this.session.params,...t},e)}sessionToPhotoCaptionCommandPayload(t,e){return this.sessionPhotoCaptionCommandPayload({...this.session.params,...t},e)}sessionToPhotoFree(t,e){return this.sessionPhotoFree({...this.session.params,...t},e)}sessionToVideoCaption(t,e){return this.sessionVideoCaption({...this.session.params,...t},e)}sessionToVideoCaptionPayload(t,e){return this.sessionVideoCaptionPayload({...this.session.params,...t},e)}sessionToVideoCaptionCommand(t,e){return this.sessionVideoCaptionCommand({...this.session.params,...t},e)}sessionToVideoCaptionCommandPayload(t,e){return this.sessionVideoCaptionCommandPayload({...this.session.params,...t},e)}sessionToVideoFree(t,e){return this.sessionVideoFree({...this.session.params,...t},e)}sessionToAudioCaption(t,e){return this.sessionAudioCaption({...this.session.params,...t},e)}sessionToAudioCaptionPayload(t,e){return this.sessionAudioCaptionPayload({...this.session.params,...t},e)}sessionToAudioCaptionCommand(t,e){return this.sessionAudioCaptionCommand({...this.session.params,...t},e)}sessionToAudioCaptionCommandPayload(t,e){return this.sessionAudioCaptionCommandPayload({...this.session.params,...t},e)}sessionToAudioFree(t,e){return this.sessionAudioFree({...this.session.params,...t},e)}sessionToDocumentCaption(t,e){return this.sessionDocumentCaption({...this.session.params,...t},e)}sessionToDocumentCaptionPayload(t,e){return this.sessionDocumentCaptionPayload({...this.session.params,...t},e)}sessionToDocumentCaptionCommand(t,e){return this.sessionDocumentCaptionCommand({...this.session.params,...t},e)}sessionToDocumentCaptionCommandPayload(t,e){return this.sessionDocumentCaptionCommandPayload({...this.session.params,...t},e)}sessionToDocumentFree(t,e){return this.sessionDocumentFree({...this.session.params,...t},e)}sessionToAnimationCaption(t,e){return this.sessionAnimationCaption({...this.session.params,...t},e)}sessionToAnimationCaptionPayload(t,e){return this.sessionAnimationCaptionPayload({...this.session.params,...t},e)}sessionToAnimationCaptionCommand(t,e){return this.sessionAnimationCaptionCommand({...this.session.params,...t},e)}sessionToAnimationCaptionCommandPayload(t,e){return this.sessionAnimationCaptionCommandPayload({...this.session.params,...t},e)}sessionToAnimationFree(t,e){return this.sessionAnimationFree({...this.session.params,...t},e)}sessionToVoiceCaption(t,e){return this.sessionVoiceCaption({...this.session.params,...t},e)}sessionToVoiceCaptionPayload(t,e){return this.sessionVoiceCaptionPayload({...this.session.params,...t},e)}sessionToVoiceCaptionCommand(t,e){return this.sessionVoiceCaptionCommand({...this.session.params,...t},e)}sessionToVoiceCaptionCommandPayload(t,e){return this.sessionVoiceCaptionCommandPayload({...this.session.params,...t},e)}sessionToVoiceFree(t,e){return this.sessionVoiceFree({...this.session.params,...t},e)}sessionToVideoNoteCaption(t,e){return this.sessionVideoNoteCaption({...this.session.params,...t},e)}sessionToVideoNoteCaptionPayload(t,e){return this.sessionVideoNoteCaptionPayload({...this.session.params,...t},e)}sessionToVideoNoteCaptionCommand(t,e){return this.sessionVideoNoteCaptionCommand({...this.session.params,...t},e)}sessionToVideoNoteCaptionCommandPayload(t,e){return this.sessionVideoNoteCaptionCommandPayload({...this.session.params,...t},e)}sessionToVideoNoteFree(t,e){return this.sessionVideoNoteFree({...this.session.params,...t},e)}sessionToStickerFree(t,e){return this.sessionStickerFree({...this.session.params,...t},e)}sessionToLocationFree(t,e){return this.sessionLocationFree({...this.session.params,...t},e)}sessionToContactFree(t,e){return this.sessionContactFree({...this.session.params,...t},e)}intent(t,e=!0){t||(t=e?this.data.callbackData():this.data.name);let i=e?":":" ";return t.indexOf(i)!=-1?t.split(i)[0]:t}methodSession(t,e){return`${t}-${e||this.data.name}`}methodSessionCallback(t){return this.methodSession("1",t)}methodSessionCallbackPayload(t){return this.methodSession("2",t)}methodSessionText(t){return this.methodSession("3",t)}methodSessionTextPayload(t){return this.methodSession("4",t)}methodSessionTextCommand(t){return this.methodSession("5",t)}methodSessionTextCommandPayload(t){return this.methodSession("6",t)}methodSessionTextFree(t){return this.methodSession("7",t)}methodSessionPhotoCaption(t){return this.methodSession("8",t)}methodSessionPhotoCaptionPayload(t){return this.methodSession("9",t)}methodSessionPhotoCaptionCommand(t){return this.methodSession("10",t)}methodSessionPhotoCaptionCommandPayload(t){return this.methodSession("11",t)}methodSessionPhotoFree(t){return this.methodSession("12",t)}methodSessionVideoCaption(t){return this.methodSession("13",t)}methodSessionVideoCaptionPayload(t){return this.methodSession("14",t)}methodSessionVideoCaptionCommand(t){return this.methodSession("15",t)}methodSessionVideoCaptionCommandPayload(t){return this.methodSession("16",t)}methodSessionVideoFree(t){return this.methodSession("17",t)}methodSessionAudioCaption(t){return this.methodSession("18",t)}methodSessionAudioCaptionPayload(t){return this.methodSession("19",t)}methodSessionAudioCaptionCommand(t){return this.methodSession("20",t)}methodSessionAudioCaptionCommandPayload(t){return this.methodSession("21",t)}methodSessionAudioFree(t){return this.methodSession("22",t)}methodSessionDocumentCaption(t){return this.methodSession("23",t)}methodSessionDocumentCaptionPayload(t){return this.methodSession("24",t)}methodSessionDocumentCaptionCommand(t){return this.methodSession("25",t)}methodSessionDocumentCaptionCommandPayload(t){return this.methodSession("26",t)}methodSessionDocumentFree(t){return this.methodSession("27",t)}methodSessionAnimationCaption(t){return this.methodSession("28",t)}methodSessionAnimationCaptionPayload(t){return this.methodSession("29",t)}methodSessionAnimationCaptionCommand(t){return this.methodSession("30",t)}methodSessionAnimationCaptionCommandPayload(t){return this.methodSession("31",t)}methodSessionAnimationFree(t){return this.methodSession("32",t)}methodSessionVoiceCaption(t){return this.methodSession("33",t)}methodSessionVoiceCaptionPayload(t){return this.methodSession("34",t)}methodSessionVoiceCaptionCommand(t){return this.methodSession("35",t)}methodSessionVoiceCaptionCommandPayload(t){return this.methodSession("36",t)}methodSessionVoiceFree(t){return this.methodSession("37",t)}methodSessionVideoNoteCaption(t){return this.methodSession("38",t)}methodSessionVideoNoteCaptionPayload(t){return this.methodSession("39",t)}methodSessionVideoNoteCaptionCommand(t){return this.methodSession("40",t)}methodSessionVideoNoteCaptionCommandPayload(t){return this.methodSession("41",t)}methodSessionVideoNoteFree(t){return this.methodSession("42",t)}methodSessionStickerFree(t){return this.methodSession("43",t)}methodSessionLocationFree(t){return this.methodSession("44",t)}methodSessionContactFree(t){return this.methodSession("45",t)}};import{serve as X}from"bun";import{join as Y,parse as Z}from"path";import{join as F}from"path";import{existsSync as j,readdirSync as Q}from"fs";var f=class{static{u(this,"PageManager")}static _getFile(t,e,i=!0,s=0){let n=Object.values(t)[s];if(n===void 0)throw new Error(`File ${e} must have export (not default) class page`);return i===!0&&(n.template=void 0),n}static getFilePath(){return F(process.cwd(),"src","pages")}static getFileSync(t,e=!0,i=0){t||(t=this.getFilePath());let s=T(t);return this._getFile(s,t,e,i)}static async getFile(t,e=!0,i=0){t||(t=this.getFilePath());let s=await import(`${t}?t=${Date.now()}`);return this._getFile(s,t,e,i)}static getFileManySync(t,e=!0,i=0){if(t||(t=this.getFilePath()),!j(t))throw new Error(`Directory ${t} is not found, please make it manually`);return Q(t).map(o=>{let n=F(t,o);return{path:n,page:this.getFileSync(n,e,i)}})}};import{LoggingPretty as tt}from"logging-pretty";import{basename as x,join as V,extname as U}from"path";import{readdirSync as J,readFileSync as G,existsSync as D,statSync as H,writeFileSync as z}from"fs";import{watch as q}from"chokidar";var b=class{static{u(this,"TemplateEngine")}static _escape(t){return t.replace(/\\/g,"\\\\").replace(/`/g,"\\`")}static _processVariables(t){return t.replace(/\{\{(\w+)\}\}/g,(e,i)=>`\${(data && data["${i}"]) ?? ""}`)}static _processBaseBlocks(t){let e=/<base\b[^>]*?(?:\sid=(["']?)([^"'\s>]+)\1)?[^>]*?>([\s\S]*?)<\/base>/g,i={};for(let s of t.matchAll(e)){let o=s[2]??"default",a=(s[3]??"").replace(/^[\t ]+/gm,"").replace(/^\n+/,"").replace(/\n+$/,"").trim();i[o]=a}return i}static _processScriptBlocks(t){return t.replace(/<script\b[^>]*>([\s\S]*?)<\/script>/gi,(e,i)=>{let s=i.trim();return s?`\${(() => { if (!data) return ''; try { with(data) { ${s} } } catch(e) { return '' } })()}`:""})}static _processSpaceBlocks(t){return t.replace(/^[ \t]*<space\s*\/>[ \t]*$/gim,"")}static _processDivBlocks(t){return t.replace(/<div>([\s\S]*?)<\/div>/gi,(e,i)=>i.replace(/^[\t ]+/gm,"").replace(/^\n+/,"").replace(/\n+$/,""))}static _processExec(t){return t.replace(/{~\s*([\s\S]*?)\s*~}/g,(e,i)=>`\${(() => { try { return ${i} } catch(e) { try { with(data || {}) { return ${i} } } catch(ee) { return '' } } })()}`)}static _processMapBlocks(t){let e=/<map(?:\s+([^>]*))?>([\s\S]*?)<\/map>/gi;return t.replace(e,(i,s,o)=>{let n={},a=/(\w+)="([^"]*)"/g;if(s)for(let m of s.matchAll(a))n[m[1]]=m[2];let r=n.from;if(!r)return"";let d=n.as||"e",c=n.index||"i",l=n.join??"\\n",p=o.replace(/^[\t ]+/gm,"").replace(/^\n+/,"").replace(/\n+$/,"").replace(/`/g,"\\`");p=p.replace(/\{\{([\w.]+)\}\}/g,(m,g)=>g===d||g.startsWith(`${d}.`)?`\${${g}}`:g===c?`\${${c}}`:m);let h=l.replace(/"/g,'\\"');return`\${(data && data["${r}"] || []).map((${d}, ${c}) => \`${p}\`).join("${h}")}`})}static _processMediaBlocks(t){let e=/<media(?:\s+([^>]*))?>([\s\S]*?)<\/media>/gi,i={};for(let s of t.matchAll(e)){let o=s[1],n=s[2],a={},r=/(\w+)="([^"]*)"/g;if(o)for(let h of o.matchAll(r))a[h[1]]=h[2];let d=a.lang||"default",c=a.type,l=n.replace(/^[\t ]+/gm,"").replace(/^\n+/,"").replace(/\n+$/,"").trim(),p={type:c};a.src&&(p.src=a.src),l&&c!=="video_note"&&c!=="sticker"&&(p.caption=l),c==="location"&&(a.latitude&&(p.latitude=a.latitude),a.longitude&&(p.longitude=a.longitude),l&&(p.caption=l)),c==="contact"&&(a.phone&&(p.phone=a.phone),a.first_name&&(p.first_name=a.first_name),a.last_name&&(p.last_name=a.last_name),a.vcard&&(p.vcard=a.vcard),l&&(p.caption=l)),i[d]=p}return i}static _processMessageBlocks(t){let e=/<message(?:\s+([^>]*))?>([\s\S]*?)<\/message>/gi,i={};for(let[,s,o]of t.matchAll(e)){let n={},a=/(\w+)="([^"]*)"/g;if(s)for(let c of s.matchAll(a))n[c[1]]=c[2];let r=n.lang||"default",d=o.replace(/^[\t ]+/gm,"").replace(/^\n+/,"").replace(/\n+$/,"").trim();i[r]=d}return i}static _processDescriptionBlocks(t){let e=/<description(?:\s+([^>]*))?>([\s\S]*?)<\/description>/gi,i={};for(let[,s,o]of t.matchAll(e)){let n={},a=/(\w+)="([^"]*)"/g;if(s)for(let l of s.matchAll(a))n[l[1]]=l[2];let r=n.lang||"default",d=n.scope||"default",c=o.replace(/^[\t ]+/gm,"").replace(/^\n+/,"").replace(/\n+$/,"").trim();i[r]||(i[r]={}),i[r][d]=c}return i}static _processKeyboardBlocks(t){let e=/<keyboard(?:\s+([^>]*))?>([\s\S]*?)<\/keyboard>/gi,i={};for(let[,s,o]of t.matchAll(e)){let n={},a=/(\w+)="([^"]*)"/g;if(s)for(let l of s.matchAll(a))n[l[1]]=l[2];let r=n.lang||"default",d=n.type==="bottom"?"bottom":"inline",c=o.trim();i[r]||(i[r]={}),i[r][d]=c}return i}static _compileToFunction(t,e){let i;e?i=t.map(n=>`\`${n}\``).join(","):i=`\`${t}\``;let s=i;if(s=this._processScriptBlocks(this._processDivBlocks(this._processSpaceBlocks(s))),e){let o=`return [${s}]`;return new Function("data",o)}else{let o=`return ${s}`;return new Function("data",o)}}static compileMedia(t){let e=this._processBaseBlocks(t),i={};for(let[s,o]of Object.entries(e)){let n=this._processMediaBlocks(o);i[s]={};for(let[a,r]of Object.entries(n)){let d={};for(let[c,l]of Object.entries(r)){if(c==="type"||typeof l!="string")continue;let p=this._processVariables(this._processExec(this._processMapBlocks(this._processScriptBlocks(l))));d[c]=this._compileToFunction(p,!1)}i[s][a]=c=>{let l={...r};for(let[p,h]of Object.entries(d))l[p]=h(c);return l}}}return i}static compileMessages(t){let e=this._processBaseBlocks(t),i={};for(let[s,o]of Object.entries(e)){let n=this._processMessageBlocks(o);i[s]={};for(let[a,r]of Object.entries(n)){let d=this._processVariables(this._processExec(this._processMapBlocks(this._processScriptBlocks(r))));i[s][a]=this._compileToFunction(d,!1)}}return i}static compileDescription(t){let e=this._processBaseBlocks(t),i={};for(let[s,o]of Object.entries(e)){let n=this._processDescriptionBlocks(o);i[s]={};for(let[a,r]of Object.entries(n))for(let[d,c]of Object.entries(r)){i[s][d]||(i[s][d]={});let l=this._processVariables(this._processExec(this._processMapBlocks(this._processScriptBlocks(c))));i[s][d][a]=this._compileToFunction(l,!1)}}return i}static compileKeyboards(t){let e=this._processBaseBlocks(t),i={};for(let[s,o]of Object.entries(e)){let n=this._processKeyboardBlocks(o);i[s]={};for(let[a,r]of Object.entries(n)){i[s][a]={};for(let[d,c]of Object.entries(r)){let p=c.split(/(?<!\\),\s*/).map(h=>h.replace(/\\,/g,",").trim()).map(h=>this._processVariables(this._processExec(this._processMapBlocks(this._processScriptBlocks(h)))));i[s][a][d]=this._compileToFunction(p,!0)}}}return i}};var P=class{static{u(this,"TemplateManager")}_map=new Map;_engine=b;_watcher=[];_supportedExtensions=new Set([".html",".ts"]);_path;_isWatchEnabled;constructor(t){let{path:e,enableWatch:i=!1}=t;this._path=e,this._isWatchEnabled=i,this._validatePath(),this._initializeWatcher(t.watchOptions),this._loadAllFiles()}_validatePath(){try{if(!this._path||!Array.isArray(this._path))throw new Error("Path must be set as string array");for(let t=0;t<this._path.length;t++){let e=this._path[t];if(!D(e))throw new Error(`Template base path is not exists: ${e}`);if(!H(e).isDirectory())throw new Error(`Template base path is not a directory: ${e}`)}return!0}catch(t){throw new Error(`Failed to validate Template base path: ${String(t)}`)}}_initializeWatcher(t){if(this._isWatchEnabled){if(this._watcher.length>0)throw new Error("Watcher already enabled");for(let e=0;e<this._path.length;e++){let i=this._path[e],s=q(i,{persistent:!0,ignoreInitial:!0,awaitWriteFinish:{stabilityThreshold:100,pollInterval:50},...t});this._watcher[e]=s,s.on("change",this._handleFileChange.bind(this)),s.on("add",this._handleFileAdd.bind(this)),s.on("unlink",this._handleFileDelete.bind(this)),s.on("error",this._handleWatchError.bind(this))}}}_handleFileChange(t){let e=x(t);this._isValidFile(e)&&this._loadFile(e)}_handleFileAdd(t){let e=x(t);this._isValidFile(e)&&this._loadFile(e)}_handleFileDelete(t){let e=x(t),i=this._getKey(e);this._map.has(i)&&this._map.delete(i)}_handleWatchError(t){}_loadAllFiles(){for(let t=0;t<this._path.length;t++){let e=this._path[t],s=J(e,"utf8").filter(o=>this._isValidFile(o));for(let o of s)this._loadFile(o)}}_loadFile(t){for(let e=0;e<this._path.length;e++){let i=this._path[e],s=V(i,t),o=this._getExtension(t),n=this._getKey(t);if(o==".html"){let a=G(s,"utf8");this._compile(n,a)}else if(o==".ts"){let a=f.getFileSync(s,!1);a?.template!==void 0&&this._compile(n,a.template),a.template=void 0}else throw new Error(`File ${t} is not support`)}}_getExtension(t){return U(t).toLowerCase()}_isValidFile(t){return this._supportedExtensions.has(this._getExtension(t))}_getKey(t){return t.replace(/\.[^/.]+$/,"")}_compile(t,e,i){let s=this._map.get(t),o=s||{},n=!1;(i===void 0||i?.media===!1||s?.media===void 0)&&(o.media=b.compileMedia(e),n=!0),(i===void 0||i?.message===!1||s?.message===void 0)&&(o.message=b.compileMessages(e),n=!0),(i===void 0||i?.keyboard===!1||s?.keyboard===void 0)&&(o.keyboard=b.compileKeyboards(e),n=!0),(i===void 0||i?.description===!1||s?.description===void 0)&&(o.description=b.compileDescription(e),n=!0),n&&this._map.set(t,o)}getMedia(t,e="default",i="default",s){let n=this._map.get(t)?.media?.[e],a=n?.[i]||n?.default;try{if(!a)throw new Error("Media function not found, please check your template");return a(s)}catch{return{}}}getMessage(t,e="default",i="default",s){try{let n=this._map.get(t)?.message?.[e],a=n?.[i]||n?.default;if(!a)throw"";return a(s)}catch{return""}}getDescription(t,e="default",i="default",s="default",o){try{let a=this._map.get(t)?.description?.[e]?.[s],r=a?.[i]||a?.default;if(!r)throw"";return r(o)}catch{return""}}getDescriptionLanguage(t,e="default",i="default"){try{let o=this._map.get(t)?.description?.[e]?.[i];return o?Object.keys(o):[]}catch{return[]}}getDescriptionScopes(t,e="default"){try{let s=this._map.get(t)?.description?.[e];return s?Object.keys(s):[]}catch{return[]}}getKeyboard(t,e="default",i="default",s="inline",o){try{let a=this._map.get(t)?.keyboard?.[e],r=a?.[i]?.[s]||a?.default?.[s];if(!r)throw[];return r(o)}catch{return[]}}async write(t,e){if(this._getExtension(t)!=".html")return null;for(let i=0;i<this._path.length;i++){let s=V(this._path[i],t);if(D(s))return z(s,e,"utf8"),this._loadFile(t),!0}return null}has(t){return this._map.has(t)}async destroy(){if(this._watcher)for(let t of this._watcher)await t.close();this._map.clear()}};import{Bot as et,webhookCallback as O}from"grammy";var R=class{static{u(this,"Gramstax")}templateManager;cacheKeyboard;cacheSession;pages;log;bot;handlerBotUpdate;constructor(t){if(!t?.token||t?.token?.length==0)throw new Error("Token is not valid or empty");if(this.log=t.log===!0?new tt:t.log?t.log:void 0,this.cacheSession=t.cacheSession||new C("memory","session"),this.cacheKeyboard=t.cacheKeyboard||new Map,this.templateManager=t.templateManager||new P({path:[Y(process.cwd(),"src","pages")]}),this.bot=this.createBot(t.token),this.bot.catch(this.onCatch),this.registerOnCallbackQueryData(),this.registerOnMessageText(),this.registerOnMessagePhoto(),this.registerOnMessageVideo(),this.registerOnMessageAudio(),this.registerOnMessageDocument(),this.registerOnMessageAnimation(),this.registerOnMessageVoice(),this.registerOnMessageVideoNote(),this.registerOnMessageSticker(),this.registerOnMessageLocation(),this.registerOnMessageContact(),this.handlerBotUpdate=this.bot.handleUpdate,this.pages=this.pageLoads(),process.once("SIGINT",()=>this.bot.stop()),process.once("SIGTERM",()=>this.bot.stop()),t.deploy.startsWith("polling"))this.runPolling();else if(t.deploy.startsWith("webhook:"))this.runWebhook(t.deploy.split("webhook:")[1]);else if(t.deploy.startsWith("serverless:"))this.runServerless(t.deploy.split("serverless:")[1]);else if(!t.deploy.startsWith("test"))throw new Error("Params deploy is not valid, expected: polling, webhook:<public-url>, serverless:<your-adapter>")}onCatch(t){this.log?.error?.(`[Bot.catch]: ${JSON.stringify(t,null,2)}`)}async onStart(t,e,i){await this.executeSyncBlocksDescription(this.pages);let{username:s}=t;this.log?.success?.(`Telegram bot polling started: ${s} ${e=="webhook"||e=="serverless"?`(${i})`:""}`)}createBot(t){return new et(t)}createCtx(t){return new S({ct:t,cacheSession:this.cacheSession,cacheKeyboard:this.cacheKeyboard,templateManager:this.templateManager,temp:{session:{},data:{}}})}runPolling(){this.bot.start({onStart:u(t=>{this.onStart(t,"polling",null)},"onStart")})}async runWebhook(t){let e=O(this.bot,"bun");this.handlerBotUpdate=e;let i=new URL(t).pathname;X({fetch(n){let{pathname:a}=new URL(n.url);if(n.method=="POST"&&a==i)return e(n);if(n.method=="GET"&&a=="/"){let r=new Date().toISOString();return new Response(JSON.stringify({status:"OK",timestamp:r}),{status:200,headers:{"Content-Type":"application/json"}})}return new Response("Not found",{status:404})}}),(await this.bot.api.getWebhookInfo()).url!=t&&await this.bot.api.setWebhook(t);let o=await this.bot.api.getMe();await this.onStart(o,"webhook",t)}async runServerless(t){this.handlerBotUpdate=O(this.bot,t);let e=await this.bot.api.getMe();await this.onStart(e,"serverless",t)}pageBuildData(t,e){t?.data===void 0&&(t.data=t.buildData()),t.data.name=Z(e).name}pageSorts(t,e){let i=t.data.name,s=t.prototype,o="Payload",n="Command",a="Caption",r="Free",d=e.routeDynamic,c=e.routeDynamicSpesific,l=u(m=>m.charAt(0).toUpperCase()+m.slice(1),"upFirst"),p=u(m=>m.replace(o,"").replace(n,"").replace(a,"").replace(r,""),"getPureRouteName"),h=u((m,g)=>s[`methodSession${l(m)}`]?.(g),"methodSession");e.all[i]=t;for(let m=0;m<e.lenListRouteStaticSession;m++){let g=e.listRouteStaticSession[m],A=e.routeStatic[g],N=e.routeStaticIntent[g],k=e.routeStaticSession[g],v={name:i,routeName:g},M=A||N;if(M!==void 0){let K=g.includes(n)?`/${i}`:i;M[s.intent(K,g.startsWith("callback"))]=v}k!==void 0&&(k[h(g,i)]=v),g.toLowerCase().endsWith("free")&&c[p(g)].push(v)}s.free&&d.push(i)}async executeSyncBlocksDescription(t){let e=new Map,i=t.routeStatic.textCommand;for(let[,s]of Object.entries(i)){let o=s.name,n=this.templateManager.getDescriptionScopes(o);for(let a of n){e.has(a)||e.set(a,new Set);let r=this.templateManager.getDescriptionLanguage(o,"default",a);for(let d of r)e.get(a).add(d)}}for(let[s,o]of e)for(let n of o){let a=[];for(let[,c]of Object.entries(i)){let l=c.name,p=this.templateManager.getDescription(l,"default",n,s);p&&a.push({command:l,description:p})}if(a.length===0)continue;let r={type:s},d=n==="default"?void 0:n;try{let c=await this.bot.api.getMyCommands({scope:r,language_code:d}),l=JSON.stringify(c.map(h=>({command:h.command,description:h.description})).sort((h,m)=>h.command.localeCompare(m.command))),p=JSON.stringify(a.sort((h,m)=>h.command.localeCompare(m.command)));l!==p&&(await this.bot.api.setMyCommands(a,{scope:r,language_code:d}),this.log?.info?.(`Synced descriptions for scope: ${s},${d?` lang: ${d},`:""} total (${a.length})`))}catch(c){this.log?.error?.(`Failed to sync descriptions for scope: ${s},${d?` lang: ${d},`:""} error: ${String(c)}`)}}}pageLoads(){this.log?.info?.("Load pages..");let t=["callback","text","textCommand","photoCaption","photoCaptionCommand","videoCaption","videoCaptionCommand","audioCaption","audioCaptionCommand","documentCaption","documentCaptionCommand","animationCaption","animationCaptionCommand","voiceCaption","voiceCaptionCommand","videoNoteCaption","videoNoteCaptionCommand"],e=["callbackPayload","textPayload","textCommandPayload","photoCaptionPayload","photoCaptionCommandPayload","videoCaptionPayload","videoCaptionCommandPayload","audioCaptionPayload","audioCaptionCommandPayload","documentCaptionPayload","documentCaptionCommandPayload","animationCaptionPayload","animationCaptionCommandPayload","voiceCaptionPayload","voiceCaptionCommandPayload","videoNoteCaptionPayload","videoNoteCaptionCommandPayload"],i=[...t,...e,"textFree","photoFree","videoFree","audioFree","documentFree","animationFree","voiceFree","videoNoteFree","stickerFree","locationFree","contactFree"],s=u((a,r)=>Object.fromEntries(a.map(d=>[d,r()])),"makeObject"),o=u(()=>[],"initDynamicSpesific"),n={all:{},routeDynamic:[],routeDynamicSpesific:{text:o(),photo:o(),video:o(),audio:o(),document:o(),animation:o(),voice:o(),videoNote:o(),sticker:o(),location:o(),contact:o()},routeStatic:s(t,()=>({})),routeStaticIntent:s(e,()=>({})),routeStaticSession:s(i,()=>({})),listRouteStatic:t,listRouteStaticIntent:e,listRouteStaticSession:i,lenListRouteStatic:t.length,lenListRouteStaticIntent:e.length,lenListRouteStaticSession:i.length};return f.getFileManySync(void 0,!0).forEach(({path:a,page:r})=>{this.pageBuildData(r,a),this.pageSorts(r,n)}),this.log?.info?.(`Finish load pages with total (${Object.keys(n.all).length})`),n}async pageRoutes(t,e){let i,s;if(i===void 0){let n=t.callbackData,a=t.msgText,r=t.msgCaption,d=this.pages.routeStatic;for(let c=0;c<this.pages.lenListRouteStatic;c++){let l=this.pages.listRouteStatic[c];if(l.startsWith(e)&&(s=d[l][n||a||r]),s)break}s!==void 0&&(i=this.pages.all[s.name])}if(i===void 0){let n=await t.sessionGet();if(n){let a=n.method,r=this.pages.routeStaticSession;for(let d=0;d<this.pages.lenListRouteStaticSession;d++){let c=this.pages.listRouteStaticSession[d];if(c.startsWith(e)&&(s=r[c][a]),s)break}s!==void 0&&(i=this.pages.all[s.name])}}if(i===void 0){let n=t.callbackDataIntent,a=t.msgTextIntent,r=t.msgCaptionIntent,d=this.pages.routeStaticIntent;for(let c=0;c<this.pages.lenListRouteStaticIntent;c++){let l=this.pages.listRouteStaticIntent[c];if(l.startsWith(e)&&(s=d[l][n||a||r]),s)break}s!==void 0&&(i=this.pages.all[s.name])}if(i!==void 0&&s!==void 0)return i.prototype[s.routeName]?await new i(t)[s.routeName]?.():void 0;let o=this.pages.routeDynamicSpesific[e]||[];for(let n=0;n<o.length;n++){let a=o[n];if(a===void 0)continue;let r=this.pages.all[a.name];if(r===void 0||!r.prototype[a.routeName])continue;let c=await new r(t)[a.routeName]?.();if(c!==null)return c}for(let n=0;n<this.pages.routeDynamic.length;n++){let a=this.pages.routeDynamic[n];if(a===void 0)continue;let r=this.pages.all[a];if(r===void 0||!r.prototype.free)continue;let c=await new r(t).free?.();if(c!==null)return c}return null}async hookBeforeRoute(t,e){return!!(t||e)}async hookErrorPage(t,e,i,s){}async hookErrorInputNotFoundPage(t){}async onMessage(t,e){let i=this.createCtx(t);try{if(await this.hookBeforeRoute(i,e)===!1)return;await this.pageRoutes(i,e)===null&&await this.hookErrorInputNotFoundPage(i)}catch(s){await this.hookErrorPage(i,e,s,!1)}}async onCallbackQueryData(t){let e=this.createCtx(t);try{if(await e.callbackQueryAnswer(),await this.hookBeforeRoute(e,"onCallbackQueryData")===!1)return;await this.pageRoutes(e,"callback")}catch(i){await this.hookErrorPage(e,"onCallbackQueryData",i,!0)}}async onMessageText(t){await this.onMessage(t,"text")}async onMessagePhoto(t){await this.onMessage(t,"photo")}async onMessageVideo(t){await this.onMessage(t,"video")}async onMessageAudio(t){await this.onMessage(t,"audio")}async onMessageDocument(t){await this.onMessage(t,"document")}async onMessageAnimation(t){await this.onMessage(t,"animation")}async onMessageVoice(t){await this.onMessage(t,"voice")}async onMessageVideoNote(t){await this.onMessage(t,"videoNote")}async onMessageSticker(t){await this.onMessage(t,"sticker")}async onMessageLocation(t){await this.onMessage(t,"location")}async onMessageContact(t){await this.onMessage(t,"contact")}registerOnCallbackQueryData(){this.bot.on("callback_query:data",this.onCallbackQueryData.bind(this))}registerOnMessageText(){this.bot.on("message:text",this.onMessageText.bind(this))}registerOnMessagePhoto(){this.bot.on("message:photo",this.onMessagePhoto.bind(this))}registerOnMessageVideo(){this.bot.on("message:video",this.onMessageVideo.bind(this))}registerOnMessageAudio(){this.bot.on("message:audio",this.onMessageAudio.bind(this))}registerOnMessageDocument(){this.bot.on("message:document",this.onMessageDocument.bind(this))}registerOnMessageAnimation(){this.bot.on("message:animation",this.onMessageAnimation.bind(this))}registerOnMessageVoice(){this.bot.on("message:voice",this.onMessageVoice.bind(this))}registerOnMessageVideoNote(){this.bot.on("message:video_note",this.onMessageVideoNote.bind(this))}registerOnMessageSticker(){this.bot.on("message:sticker",this.onMessageSticker.bind(this))}registerOnMessageLocation(){this.bot.on("message:location",this.onMessageLocation.bind(this))}registerOnMessageContact(){this.bot.on("message:contact",this.onMessageContact.bind(this))}};export{C as CacheExternal,S as Ctx,R as Gramstax,I as LoggingUtils,b as TemplateEngine,P as TemplateManager,ot as log};
1
+ var L=Object.defineProperty;var h=(y,t)=>L(y,"name",{value:t,configurable:!0}),R=(y=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(y,{get:(t,e)=>(typeof require<"u"?require:t)[e]}):y)(function(y){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+y+'" is not supported')});import W from"logging-pretty";var w=class extends W{static{h(this,"LoggingUtils")}constructor(t){super(t)}errorMake(t,e,i){this.error(`[${e?.name||e}:${i}] ${String(t)}`)}},ot=new w;import{Keyv as j}from"keyv";import Q from"@keyv/redis";var k=class extends j{constructor(e="memory",i,s,a){let n;e.startsWith("redis")&&(n=new Q(e)),e==="memory"&&(n=new Map),a?.forceStore&&(n=a.forceStore);super({store:n,namespace:i,ttl:s});this.url=e}static{h(this,"CacheExternal")}};import{setTimeout as U}from"timers/promises";import{InlineKeyboard as O,Keyboard as A}from"grammy";var I=class{static{h(this,"Ctx")}static template;static data;data=this.constructor.data;get userIsPremium(){return this.ct.from?.is_premium??!1}get userIsBot(){return this.ct.from?.is_bot??!1}get userId(){return this.ct.from?.id}get userName(){return this.ct.from?.username}get userFullName(){return`${this.ct.from?.first_name}${this.ct.from?.last_name}`}get userFirstName(){return this.ct.from?.first_name}get userLastName(){return this.ct.from?.last_name}get userLanguage(){return this.ct.from?.language_code}set userLanguage(t){this.ct.from.language_code=t}get msgText(){return this.ct.message?.text}get msgTextIntent(){return this.msgText&&this.msgText.indexOf(" ")!=-1?this.msgText.split(" ")[0]:""}get msgTextPayloads(){return this.msgText&&this.msgText.indexOf(" ")!=-1?this.msgText.split(" ").slice(1):[]}get msgTextPayload(){return this.msgTextPayloads[0]}get msgCaption(){return this.ct.message?.caption}get msgCaptionIntent(){return this.msgCaption&&this.msgCaption.indexOf(" ")!=-1?this.msgCaption.split(" ")[0]:""}get msgPhoto(){return this.ct.message?.photo}get msgVideo(){return this.ct.message?.video}get msgAudio(){return this.ct.message?.audio}get callbackData(){return this.ct.callbackQuery?.data}get callbackDataIntent(){return this.callbackData&&this.callbackData.indexOf(":")!=-1?this.callbackData.split(":")[0]:""}get callbackDataPayloads(){return this.callbackData&&this.callbackData.indexOf(":")!=-1?this.callbackData.split(":").slice(1):[]}get callbackDataPayload(){return this.callbackDataPayloads?.[0]}get callbackDataParts(){return this.callbackDataPayload&&this.callbackDataPayload.indexOf("+")!=-1?this.callbackDataPayload.split("+"):[]}set session(t){this.temp.session=t}get session(){return this.temp.session}ct;cacheSession;cacheKeyboard;templateManager;temp;constructor(t){this.ct=t.ct,this.cacheSession=t.cacheSession,this.cacheKeyboard=t.cacheKeyboard,this.templateManager=t.templateManager,this.temp=t.temp}async broadcast(t,e,i,s,a,n,o=!1){try{let d=e.map(c=>this.templateManager.getMessage(this.data.name,a,c,s));for(let c=0;c<t.length;c++){let l=t[c],p=d[c];await this.ct.api.sendMessage(l,p,{parse_mode:"HTML",reply_markup:i,...n}),c+1==30&&await U(1200)}}catch(r){if(o)throw r}}async reply(t,e,i,s){let a=this.templateManager.getMessage(this.data.name,i,this.userLanguage,e);return await this.ct.reply(a,{parse_mode:"HTML",reply_markup:t,...s})}async replyMedia(t,e,i,s,a){let n=this.templateManager.getMedia(this.data.name,s,this.userLanguage,i),o={parse_mode:"HTML",reply_markup:e,...a};if(t=="photo")return await this.ct.replyWithPhoto(n.src,{caption:n.caption,...o});if(t=="video")return await this.ct.replyWithVideo(n.src,{caption:n.caption,...o});if(t=="audio")return await this.ct.replyWithAudio(n.src,{caption:n.caption,...o});if(t=="document")return await this.ct.replyWithDocument(n.src,{caption:n.caption,...o});if(t=="animation")return await this.ct.replyWithAnimation(n.src,{caption:n.caption,...o});if(t=="voice")return await this.ct.replyWithVoice(n.src,o);if(t=="video_note")return await this.ct.replyWithVideoNote(n.src,o);if(t=="sticker")return await this.ct.replyWithSticker(n.src,o);if(t=="location")return await this.ct.replyWithLocation(parseInt(n.latitude),parseInt(n.longitude),o);if(t=="contact")return await this.ct.replyWithContact(n.phone,n.first_name,{vcard:n.vcard,...o})}async replyPhoto(t,e,i,s){return this.replyMedia("photo",t,e,i,s)}async replyVideo(t,e,i,s){return this.replyMedia("video",t,e,i,s)}async replyAudio(t,e,i,s){return this.replyMedia("audio",t,e,i,s)}async replyDocument(t,e,i,s){return this.replyMedia("document",t,e,i,s)}async replyAnimation(t,e,i,s){return this.replyMedia("animation",t,e,i,s)}async replyVoice(t,e,i,s){return this.replyMedia("voice",t,e,i,s)}async replyVideoNote(t,e,i,s){return this.replyMedia("video_note",t,e,i,s)}async replySticker(t,e,i,s){return this.replyMedia("sticker",t,e,i,s)}async replyLocation(t,e,i,s){return this.replyMedia("location",t,e,i,s)}async replyContact(t,e,i,s){return this.replyMedia("contact",t,e,i,s)}async replyAction(t){return await this.ct.replyWithChatAction(t)}async edit(t,e,i,s,a=!0){try{let n=this.templateManager.getMessage(this.data.name,i,this.userLanguage,e);return await this.ct.editMessageText(n,{parse_mode:"HTML",reply_markup:t,...s})}catch(n){if(a)await this.reply(t,e,i,s);else throw n}}async editMedia(t,e,i,s,a,n,o=!0){try{let{src:r,caption:d}=this.templateManager.getMedia(this.data.name,s,this.userLanguage,i);return t=="voice"?(await this.delete(),await this.replyVoice(e,i,s,a)):t=="video_note"?(await this.delete(),await this.replyVoice(e,i,s,a)):t=="sticker"?(await this.delete(),await this.replySticker(e,i,s,a)):t=="location"?(await this.delete(),await this.replyLocation(e,i,s,a)):t=="contact"?(await this.delete(),await this.replyContact(e,i,s,a)):await this.ct.editMessageMedia({media:r,caption:d,type:t,parse_mode:"HTML",...a},{reply_markup:e,...n})}catch(r){if(o===!0)await this.reply(e,i,s,n);else throw r}}async editPhoto(t,e,i,s,a){return await this.editMedia("photo",t,e,i,s,a)}async editVideo(t,e,i,s,a){return await this.editMedia("video",t,e,i,s,a)}async editAudio(t,e,i,s,a){return await this.editMedia("audio",t,e,i,s,a)}async editDocument(t,e,i,s,a){return await this.editMedia("document",t,e,i,s,a)}async editAnimation(t,e,i,s,a){return await this.editMedia("animation",t,e,i,s,a)}async editVoice(t,e,i,s){return await this.editMedia("voice",t,e,i,s)}async editVideoNote(t,e,i,s){return await this.editMedia("video_note",t,e,i,s)}async editSticker(t,e,i,s){return await this.editMedia("sticker",t,e,i,s)}async editLocation(t,e,i,s){return await this.editMedia("location",t,e,i,s)}async editContact(t,e,i,s){return await this.editMedia("contact",t,e,i,s)}async delete(){return await this.ct.deleteMessage()}async callbackQueryAnswer(t){try{return await this.ct.answerCallbackQuery(t)}catch{return null}}async sessionSet(t,e,i){try{let s={method:t,params:e,...i},a=await this.cacheSession.set(`${this.userId}`,s);return a&&(this.session=s),a}catch{return null}}async sessionGet(){try{let t=await this.cacheSession.get(`${this.userId}`);return t&&(this.session=t),t||null}catch{return null}}async sessionClear(){try{let t=await this.cacheSession.delete(`${this.userId}`);return t&&(this.session={}),t}catch{return null}}static buildData(t){return{name:"",callbackData:t?function(...e){let i=t(...e);return`${this.name}${i==""||i===void 0?"":`:${i}`}`}:function(){return this?.name}}}inlineKeyboard(t,e){let i=this.data.name;if(e?.cache===!0){let o=`${i}:${this.userLanguage}:i`,r=this.cacheKeyboard.get(o);if(!r){let d=new O,c=this.templateManager.getKeyboard(i,e?.baseId,this.userLanguage,"inline",e?.data),l=t(d,c);return this.cacheKeyboard.set(o,l),l}return r}let s=new O,a=this.templateManager.getKeyboard(i,e?.baseId,this.userLanguage,"inline",e?.data);return t(s,a)}bottomKeyboard(t,e){let i=this.data.name;if(e?.cache===!0){let o=`${i}:${this.userLanguage}:b`,r=this.cacheKeyboard.get(o);if(!r){let d=new A,c=this.templateManager.getKeyboard(i,e?.baseId,this.userLanguage,"bottom",e?.data),l=t(d,c);return l.resized(!0),this.cacheKeyboard.set(o,l),l}return r}let s=new A,a=this.templateManager.getKeyboard(i,e?.baseId,this.userLanguage,"bottom",e?.data),n=t(s,a);return n.resized(),n}validateSessionCallback(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionCallback(e)}validateSessionCallbackPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionCallbackPayload(e)}validateSessionText(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionText(e)}validateSessionTextPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionTextPayload(e)}validateSessionTextCommand(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionTextCommand(e)}validateSessionTextCommandPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionTextCommandPayload(e)}validateSessionTextFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionTextFree(e)}validateSessionPhotoCaption(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionPhotoCaption(e)}validateSessionPhotoCaptionPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionPhotoCaptionPayload(e)}validateSessionPhotoCaptionCommand(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionPhotoCaptionCommand(e)}validateSessionPhotoCaptionCommandPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionPhotoCaptionCommandPayload(e)}validateSessionPhotoFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionPhotoFree(e)}validateSessionVideoCaption(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoCaption(e)}validateSessionVideoCaptionPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoCaptionPayload(e)}validateSessionVideoCaptionCommand(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoCaptionCommand(e)}validateSessionVideoCaptionCommandPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoCaptionCommandPayload(e)}validateSessionVideoFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoFree(e)}validateSessionAudioCaption(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAudioCaption(e)}validateSessionAudioCaptionPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAudioCaptionPayload(e)}validateSessionAudioCaptionCommand(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAudioCaptionCommand(e)}validateSessionAudioCaptionCommandPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAudioCaptionCommandPayload(e)}validateSessionAudioFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAudioFree(e)}validateSessionDocumentCaption(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionDocumentCaption(e)}validateSessionDocumentCaptionPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionDocumentCaptionPayload(e)}validateSessionDocumentCaptionCommand(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionDocumentCaptionCommand(e)}validateSessionDocumentCaptionCommandPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionDocumentCaptionCommandPayload(e)}validateSessionDocumentFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionDocumentFree(e)}validateSessionAnimationCaption(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAnimationCaption(e)}validateSessionAnimationCaptionPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAnimationCaptionPayload(e)}validateSessionAnimationCaptionCommand(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAnimationCaptionCommand(e)}validateSessionAnimationCaptionCommandPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAnimationCaptionCommandPayload(e)}validateSessionAnimationFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionAnimationFree(e)}validateSessionVoiceCaption(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVoiceCaption(e)}validateSessionVoiceCaptionPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVoiceCaptionPayload(e)}validateSessionVoiceCaptionCommand(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVoiceCaptionCommand(e)}validateSessionVoiceCaptionCommandPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVoiceCaptionCommandPayload(e)}validateSessionVoiceFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVoiceFree(e)}validateSessionVideoNoteCaption(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoNoteCaption(e)}validateSessionVideoNoteCaptionPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoNoteCaptionPayload(e)}validateSessionVideoNoteCaptionCommand(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoNoteCaptionCommand(e)}validateSessionVideoNoteCaptionCommandPayload(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoNoteCaptionCommandPayload(e)}validateSessionVideoNoteFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionVideoNoteFree(e)}validateSessionStickerFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionStickerFree(e)}validateSessionLocationFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionLocationFree(e)}validateSessionContactFree(t,e){return t===void 0&&(t=this.session.method),t===void 0?!1:t===this.methodSessionContactFree(e)}sessionCallback(t,e){return this.sessionSet(this.methodSessionCallback(e),t)}sessionCallbackPayload(t,e){return this.sessionSet(this.methodSessionCallbackPayload(e),t)}sessionText(t,e){return this.sessionSet(this.methodSessionText(e),t)}sessionTextPayload(t,e){return this.sessionSet(this.methodSessionTextPayload(e),t)}sessionTextCommand(t,e){return this.sessionSet(this.methodSessionTextCommand(e),t)}sessionTextCommandPayload(t,e){return this.sessionSet(this.methodSessionTextCommandPayload(e),t)}sessionTextFree(t,e){return this.sessionSet(this.methodSessionTextFree(e),t)}sessionPhotoCaption(t,e){return this.sessionSet(this.methodSessionPhotoCaption(e),t)}sessionPhotoCaptionPayload(t,e){return this.sessionSet(this.methodSessionPhotoCaptionPayload(e),t)}sessionPhotoCaptionCommand(t,e){return this.sessionSet(this.methodSessionPhotoCaptionCommand(e),t)}sessionPhotoCaptionCommandPayload(t,e){return this.sessionSet(this.methodSessionPhotoCaptionCommandPayload(e),t)}sessionPhotoFree(t,e){return this.sessionSet(this.methodSessionPhotoFree(e),t)}sessionVideoCaption(t,e){return this.sessionSet(this.methodSessionVideoCaption(e),t)}sessionVideoCaptionPayload(t,e){return this.sessionSet(this.methodSessionVideoCaptionPayload(e),t)}sessionVideoCaptionCommand(t,e){return this.sessionSet(this.methodSessionVideoCaptionCommand(e),t)}sessionVideoCaptionCommandPayload(t,e){return this.sessionSet(this.methodSessionVideoCaptionCommandPayload(e),t)}sessionVideoFree(t,e){return this.sessionSet(this.methodSessionVideoFree(e),t)}sessionAudioCaption(t,e){return this.sessionSet(this.methodSessionAudioCaption(e),t)}sessionAudioCaptionPayload(t,e){return this.sessionSet(this.methodSessionAudioCaptionPayload(e),t)}sessionAudioCaptionCommand(t,e){return this.sessionSet(this.methodSessionAudioCaptionCommand(e),t)}sessionAudioCaptionCommandPayload(t,e){return this.sessionSet(this.methodSessionAudioCaptionCommandPayload(e),t)}sessionAudioFree(t,e){return this.sessionSet(this.methodSessionAudioFree(e),t)}sessionDocumentCaption(t,e){return this.sessionSet(this.methodSessionDocumentCaption(e),t)}sessionDocumentCaptionPayload(t,e){return this.sessionSet(this.methodSessionDocumentCaptionPayload(e),t)}sessionDocumentCaptionCommand(t,e){return this.sessionSet(this.methodSessionDocumentCaptionCommand(e),t)}sessionDocumentCaptionCommandPayload(t,e){return this.sessionSet(this.methodSessionDocumentCaptionCommandPayload(e),t)}sessionDocumentFree(t,e){return this.sessionSet(this.methodSessionDocumentFree(e),t)}sessionAnimationCaption(t,e){return this.sessionSet(this.methodSessionAnimationCaption(e),t)}sessionAnimationCaptionPayload(t,e){return this.sessionSet(this.methodSessionAnimationCaptionPayload(e),t)}sessionAnimationCaptionCommand(t,e){return this.sessionSet(this.methodSessionAnimationCaptionCommand(e),t)}sessionAnimationCaptionCommandPayload(t,e){return this.sessionSet(this.methodSessionAnimationCaptionCommandPayload(e),t)}sessionAnimationFree(t,e){return this.sessionSet(this.methodSessionAnimationFree(e),t)}sessionVoiceCaption(t,e){return this.sessionSet(this.methodSessionVoiceCaption(e),t)}sessionVoiceCaptionPayload(t,e){return this.sessionSet(this.methodSessionVoiceCaptionPayload(e),t)}sessionVoiceCaptionCommand(t,e){return this.sessionSet(this.methodSessionVoiceCaptionCommand(e),t)}sessionVoiceCaptionCommandPayload(t,e){return this.sessionSet(this.methodSessionVoiceCaptionCommandPayload(e),t)}sessionVoiceFree(t,e){return this.sessionSet(this.methodSessionVoiceFree(e),t)}sessionVideoNoteCaption(t,e){return this.sessionSet(this.methodSessionVideoNoteCaption(e),t)}sessionVideoNoteCaptionPayload(t,e){return this.sessionSet(this.methodSessionVideoNoteCaptionPayload(e),t)}sessionVideoNoteCaptionCommand(t,e){return this.sessionSet(this.methodSessionVideoNoteCaptionCommand(e),t)}sessionVideoNoteCaptionCommandPayload(t,e){return this.sessionSet(this.methodSessionVideoNoteCaptionCommandPayload(e),t)}sessionVideoNoteFree(t,e){return this.sessionSet(this.methodSessionVideoNoteFree(e),t)}sessionStickerFree(t,e){return this.sessionSet(this.methodSessionStickerFree(e),t)}sessionLocationFree(t,e){return this.sessionSet(this.methodSessionLocationFree(e),t)}sessionContactFree(t,e){return this.sessionSet(this.methodSessionContactFree(e),t)}sessionToCallback(t,e){return this.sessionCallback({...this.session.params,...t},e)}sessionToCallbackPayload(t,e){return this.sessionCallbackPayload({...this.session.params,...t},e)}sessionToText(t,e){return this.sessionText({...this.session.params,...t},e)}sessionToTextPayload(t,e){return this.sessionTextPayload({...this.session.params,...t},e)}sessionToTextCommand(t,e){return this.sessionTextCommand({...this.session.params,...t},e)}sessionToTextCommandPayload(t,e){return this.sessionTextCommandPayload({...this.session.params,...t},e)}sessionToTextFree(t,e){return this.sessionTextFree({...this.session.params,...t},e)}sessionToPhotoCaption(t,e){return this.sessionPhotoCaption({...this.session.params,...t},e)}sessionToPhotoCaptionPayload(t,e){return this.sessionPhotoCaptionPayload({...this.session.params,...t},e)}sessionToPhotoCaptionCommand(t,e){return this.sessionPhotoCaptionCommand({...this.session.params,...t},e)}sessionToPhotoCaptionCommandPayload(t,e){return this.sessionPhotoCaptionCommandPayload({...this.session.params,...t},e)}sessionToPhotoFree(t,e){return this.sessionPhotoFree({...this.session.params,...t},e)}sessionToVideoCaption(t,e){return this.sessionVideoCaption({...this.session.params,...t},e)}sessionToVideoCaptionPayload(t,e){return this.sessionVideoCaptionPayload({...this.session.params,...t},e)}sessionToVideoCaptionCommand(t,e){return this.sessionVideoCaptionCommand({...this.session.params,...t},e)}sessionToVideoCaptionCommandPayload(t,e){return this.sessionVideoCaptionCommandPayload({...this.session.params,...t},e)}sessionToVideoFree(t,e){return this.sessionVideoFree({...this.session.params,...t},e)}sessionToAudioCaption(t,e){return this.sessionAudioCaption({...this.session.params,...t},e)}sessionToAudioCaptionPayload(t,e){return this.sessionAudioCaptionPayload({...this.session.params,...t},e)}sessionToAudioCaptionCommand(t,e){return this.sessionAudioCaptionCommand({...this.session.params,...t},e)}sessionToAudioCaptionCommandPayload(t,e){return this.sessionAudioCaptionCommandPayload({...this.session.params,...t},e)}sessionToAudioFree(t,e){return this.sessionAudioFree({...this.session.params,...t},e)}sessionToDocumentCaption(t,e){return this.sessionDocumentCaption({...this.session.params,...t},e)}sessionToDocumentCaptionPayload(t,e){return this.sessionDocumentCaptionPayload({...this.session.params,...t},e)}sessionToDocumentCaptionCommand(t,e){return this.sessionDocumentCaptionCommand({...this.session.params,...t},e)}sessionToDocumentCaptionCommandPayload(t,e){return this.sessionDocumentCaptionCommandPayload({...this.session.params,...t},e)}sessionToDocumentFree(t,e){return this.sessionDocumentFree({...this.session.params,...t},e)}sessionToAnimationCaption(t,e){return this.sessionAnimationCaption({...this.session.params,...t},e)}sessionToAnimationCaptionPayload(t,e){return this.sessionAnimationCaptionPayload({...this.session.params,...t},e)}sessionToAnimationCaptionCommand(t,e){return this.sessionAnimationCaptionCommand({...this.session.params,...t},e)}sessionToAnimationCaptionCommandPayload(t,e){return this.sessionAnimationCaptionCommandPayload({...this.session.params,...t},e)}sessionToAnimationFree(t,e){return this.sessionAnimationFree({...this.session.params,...t},e)}sessionToVoiceCaption(t,e){return this.sessionVoiceCaption({...this.session.params,...t},e)}sessionToVoiceCaptionPayload(t,e){return this.sessionVoiceCaptionPayload({...this.session.params,...t},e)}sessionToVoiceCaptionCommand(t,e){return this.sessionVoiceCaptionCommand({...this.session.params,...t},e)}sessionToVoiceCaptionCommandPayload(t,e){return this.sessionVoiceCaptionCommandPayload({...this.session.params,...t},e)}sessionToVoiceFree(t,e){return this.sessionVoiceFree({...this.session.params,...t},e)}sessionToVideoNoteCaption(t,e){return this.sessionVideoNoteCaption({...this.session.params,...t},e)}sessionToVideoNoteCaptionPayload(t,e){return this.sessionVideoNoteCaptionPayload({...this.session.params,...t},e)}sessionToVideoNoteCaptionCommand(t,e){return this.sessionVideoNoteCaptionCommand({...this.session.params,...t},e)}sessionToVideoNoteCaptionCommandPayload(t,e){return this.sessionVideoNoteCaptionCommandPayload({...this.session.params,...t},e)}sessionToVideoNoteFree(t,e){return this.sessionVideoNoteFree({...this.session.params,...t},e)}sessionToStickerFree(t,e){return this.sessionStickerFree({...this.session.params,...t},e)}sessionToLocationFree(t,e){return this.sessionLocationFree({...this.session.params,...t},e)}sessionToContactFree(t,e){return this.sessionContactFree({...this.session.params,...t},e)}intent(t,e=!0){t||(t=e?this.data.callbackData():this.data.name);let i=e?":":" ";return t.indexOf(i)!=-1?t.split(i)[0]:t}methodSession(t,e){return`${t}-${e||this.data.name}`}methodSessionCallback(t){return this.methodSession("1",t)}methodSessionCallbackPayload(t){return this.methodSession("2",t)}methodSessionText(t){return this.methodSession("3",t)}methodSessionTextPayload(t){return this.methodSession("4",t)}methodSessionTextCommand(t){return this.methodSession("5",t)}methodSessionTextCommandPayload(t){return this.methodSession("6",t)}methodSessionTextFree(t){return this.methodSession("7",t)}methodSessionPhotoCaption(t){return this.methodSession("8",t)}methodSessionPhotoCaptionPayload(t){return this.methodSession("9",t)}methodSessionPhotoCaptionCommand(t){return this.methodSession("10",t)}methodSessionPhotoCaptionCommandPayload(t){return this.methodSession("11",t)}methodSessionPhotoFree(t){return this.methodSession("12",t)}methodSessionVideoCaption(t){return this.methodSession("13",t)}methodSessionVideoCaptionPayload(t){return this.methodSession("14",t)}methodSessionVideoCaptionCommand(t){return this.methodSession("15",t)}methodSessionVideoCaptionCommandPayload(t){return this.methodSession("16",t)}methodSessionVideoFree(t){return this.methodSession("17",t)}methodSessionAudioCaption(t){return this.methodSession("18",t)}methodSessionAudioCaptionPayload(t){return this.methodSession("19",t)}methodSessionAudioCaptionCommand(t){return this.methodSession("20",t)}methodSessionAudioCaptionCommandPayload(t){return this.methodSession("21",t)}methodSessionAudioFree(t){return this.methodSession("22",t)}methodSessionDocumentCaption(t){return this.methodSession("23",t)}methodSessionDocumentCaptionPayload(t){return this.methodSession("24",t)}methodSessionDocumentCaptionCommand(t){return this.methodSession("25",t)}methodSessionDocumentCaptionCommandPayload(t){return this.methodSession("26",t)}methodSessionDocumentFree(t){return this.methodSession("27",t)}methodSessionAnimationCaption(t){return this.methodSession("28",t)}methodSessionAnimationCaptionPayload(t){return this.methodSession("29",t)}methodSessionAnimationCaptionCommand(t){return this.methodSession("30",t)}methodSessionAnimationCaptionCommandPayload(t){return this.methodSession("31",t)}methodSessionAnimationFree(t){return this.methodSession("32",t)}methodSessionVoiceCaption(t){return this.methodSession("33",t)}methodSessionVoiceCaptionPayload(t){return this.methodSession("34",t)}methodSessionVoiceCaptionCommand(t){return this.methodSession("35",t)}methodSessionVoiceCaptionCommandPayload(t){return this.methodSession("36",t)}methodSessionVoiceFree(t){return this.methodSession("37",t)}methodSessionVideoNoteCaption(t){return this.methodSession("38",t)}methodSessionVideoNoteCaptionPayload(t){return this.methodSession("39",t)}methodSessionVideoNoteCaptionCommand(t){return this.methodSession("40",t)}methodSessionVideoNoteCaptionCommandPayload(t){return this.methodSession("41",t)}methodSessionVideoNoteFree(t){return this.methodSession("42",t)}methodSessionStickerFree(t){return this.methodSession("43",t)}methodSessionLocationFree(t){return this.methodSession("44",t)}methodSessionContactFree(t){return this.methodSession("45",t)}};import{serve as X}from"bun";import{join as Y,parse as Z}from"path";import{join as N}from"path";import{existsSync as J,readdirSync as G}from"fs";var f=class{static{h(this,"PageManager")}static _getFile(t,e,i=!0,s=0){let n=Object.values(t)[s];if(n===void 0)throw new Error(`File ${e} must have export (not default) class page`);return i===!0&&(n.template=void 0),n}static getFilePath(){return N(process.cwd(),"src","pages")}static getFileSync(t,e=!0,i=0){t||(t=this.getFilePath());let s=R(t);return this._getFile(s,t,e,i)}static async getFile(t,e=!0,i=0){t||(t=this.getFilePath());let s=await import(`${t}?t=${Date.now()}`);return this._getFile(s,t,e,i)}static getFileManySync(t,e=!0,i=0){if(t||(t=this.getFilePath()),!J(t))throw new Error(`Directory ${t} is not found, please make it manually`);return G(t).map(a=>{let n=N(t,a);return{path:n,page:this.getFileSync(n,e,i)}})}};import{LoggingPretty as tt}from"logging-pretty";import{basename as F,join as S,extname as B}from"path";import{readdirSync as H,readFileSync as V,existsSync as P,statSync as x,writeFileSync as z}from"fs";import{watch as q}from"chokidar";var b=class{static{h(this,"TemplateEngine")}static _escape(t){return t.replace(/\\/g,"\\\\").replace(/`/g,"\\`")}static _processVariables(t){return t.replace(/\{\{(\w+)\}\}/g,(e,i)=>`\${(data && data["${i}"]) ?? ""}`)}static _processBaseBlocks(t){let e=/<base\b[^>]*?(?:\sid=(["']?)([^"'\s>]+)\1)?[^>]*?>([\s\S]*?)<\/base>/g,i={};for(let s of t.matchAll(e)){let a=s[2]??"default",o=(s[3]??"").replace(/^[\t ]+/gm,"").replace(/^\n+/,"").replace(/\n+$/,"").trim();i[a]=o}return i}static _processScriptBlocks(t){return t.replace(/<script\b[^>]*>([\s\S]*?)<\/script>/gi,(e,i)=>{let s=i.trim();return s?`\${(() => { if (!data) return ''; try { with(data) { ${s} } } catch(e) { return '' } })()}`:""})}static _processSpaceBlocks(t){return t.replace(/^[ \t]*<space\s*\/>[ \t]*$/gim,"")}static _processDivBlocks(t){return t.replace(/<div>([\s\S]*?)<\/div>/gi,(e,i)=>i.replace(/^[\t ]+/gm,"").replace(/^\n+/,"").replace(/\n+$/,""))}static _processExec(t){return t.replace(/{~\s*([\s\S]*?)\s*~}/g,(e,i)=>`\${(() => { try { return ${i} } catch(e) { try { with(data || {}) { return ${i} } } catch(ee) { return '' } } })()}`)}static _processMapBlocks(t){let e=/<map(?:\s+([^>]*))?>([\s\S]*?)<\/map>/gi;return t.replace(e,(i,s,a)=>{let n={},o=/(\w+)="([^"]*)"/g;if(s)for(let p of s.matchAll(o))n[p[1]]=p[2];let r=n.src;if(!r)return"";let d=n.join??"\\n",c=a.replace(/^[\t ]+/gm,"").replace(/^\n+/,"").replace(/\n+$/,"").replace(/`/g,"\\`");c=c.replace(/\{\{([\w.]+)\}\}/g,(p,u)=>u==="e"||u.startsWith("e.")?`\${${u}}`:u==="i"?"${i}":p);let l=d.replace(/"/g,'\\"');return`\${(data && data["${r}"] || []).map((e, i) => \`${c}\`).join("${l}")}`})}static _processMediaBlocks(t){let e=/<media(?:\s+([^>]*))?>([\s\S]*?)<\/media>/gi,i={};for(let s of t.matchAll(e)){let a=s[1],n=s[2],o={},r=/(\w+)="([^"]*)"/g;if(a)for(let u of a.matchAll(r))o[u[1]]=u[2];let d=o.lang||"default",c=o.type,l=n.replace(/^[\t ]+/gm,"").replace(/^\n+/,"").replace(/\n+$/,"").trim(),p={type:c};o.src&&(p.src=o.src),l&&c!=="video_note"&&c!=="sticker"&&(p.caption=l),c==="location"&&(o.latitude&&(p.latitude=o.latitude),o.longitude&&(p.longitude=o.longitude),l&&(p.caption=l)),c==="contact"&&(o.phone&&(p.phone=o.phone),o.first_name&&(p.first_name=o.first_name),o.last_name&&(p.last_name=o.last_name),o.vcard&&(p.vcard=o.vcard),l&&(p.caption=l)),i[d]=p}return i}static _processMessageBlocks(t){let e=/<message(?:\s+([^>]*))?>([\s\S]*?)<\/message>/gi,i={};for(let[,s,a]of t.matchAll(e)){let n={},o=/(\w+)="([^"]*)"/g;if(s)for(let c of s.matchAll(o))n[c[1]]=c[2];let r=n.lang||"default",d=a.replace(/^[\t ]+/gm,"").replace(/^\n+/,"").replace(/\n+$/,"").trim();i[r]=d}return i}static _processDescriptionBlocks(t){let e=/<description(?:\s+([^>]*))?>([\s\S]*?)<\/description>/gi,i={};for(let[,s,a]of t.matchAll(e)){let n={},o=/(\w+)="([^"]*)"/g;if(s)for(let l of s.matchAll(o))n[l[1]]=l[2];let r=n.lang||"default",d=n.scope||"default",c=a.replace(/^[\t ]+/gm,"").replace(/^\n+/,"").replace(/\n+$/,"").trim();i[r]||(i[r]={}),i[r][d]=c}return i}static _processKeyboardBlocks(t){let e=/<keyboard(?:\s+([^>]*))?>([\s\S]*?)<\/keyboard>/gi,i={};for(let[,s,a]of t.matchAll(e)){let n={},o=/(\w+)="([^"]*)"/g;if(s)for(let l of s.matchAll(o))n[l[1]]=l[2];let r=n.lang||"default",d=n.type==="bottom"?"bottom":"inline",c=a.trim();i[r]||(i[r]={}),i[r][d]=c}return i}static _compileToFunction(t,e){let i;e?i=t.map(n=>`\`${n}\``).join(","):i=`\`${t}\``;let s=i;if(s=this._processScriptBlocks(this._processDivBlocks(this._processSpaceBlocks(s))),e){let a=`return [${s}]`;return new Function("data",a)}else{let a=`return ${s}`;return new Function("data",a)}}static compileMedia(t){let e=this._processBaseBlocks(t),i={};for(let[s,a]of Object.entries(e)){let n=this._processMediaBlocks(a);i[s]={};for(let[o,r]of Object.entries(n)){let d={};for(let[c,l]of Object.entries(r)){if(c==="type"||typeof l!="string")continue;let p=this._processVariables(this._processExec(this._processMapBlocks(this._processScriptBlocks(l))));d[c]=this._compileToFunction(p,!1)}i[s][o]=c=>{let l={...r};for(let[p,u]of Object.entries(d))l[p]=u(c);return l}}}return i}static compileMessages(t){let e=this._processBaseBlocks(t),i={};for(let[s,a]of Object.entries(e)){let n=this._processMessageBlocks(a);i[s]={};for(let[o,r]of Object.entries(n)){let d=this._processVariables(this._processExec(this._processMapBlocks(this._processScriptBlocks(r))));i[s][o]=this._compileToFunction(d,!1)}}return i}static compileDescription(t){let e=this._processBaseBlocks(t),i={};for(let[s,a]of Object.entries(e)){let n=this._processDescriptionBlocks(a);i[s]={};for(let[o,r]of Object.entries(n))for(let[d,c]of Object.entries(r)){i[s][d]||(i[s][d]={});let l=this._processVariables(this._processExec(this._processMapBlocks(this._processScriptBlocks(c))));i[s][d][o]=this._compileToFunction(l,!1)}}return i}static compileKeyboards(t){let e=this._processBaseBlocks(t),i={};for(let[s,a]of Object.entries(e)){let n=this._processKeyboardBlocks(a);i[s]={};for(let[o,r]of Object.entries(n)){i[s][o]={};for(let[d,c]of Object.entries(r)){let p=c.split(/(?<!\\),\s*/).map(u=>u.replace(/\\,/g,",").trim()).map(u=>this._processVariables(this._processExec(this._processMapBlocks(this._processScriptBlocks(u)))));i[s][o][d]=this._compileToFunction(p,!0)}}}return i}static _extractBlock(t,e,i="default",s="all"){let a=/<base\b[^>]*?(?:\sid=(["']?)([^"'\s>]+)\1)?[^>]*?>([\s\S]*?)<\/base>/g,n=t,o=!1;for(let r of t.matchAll(a))if((r[2]??"default")===i){n=r[3]??"",o=!0;break}if(!o){let r=/<base\b/.test(t);if(!(i==="default"&&!r))return""}if(e){if(e==="all")return n;let r=Array.isArray(e)?e:[e],d=[];for(let l of r){let p=new RegExp(`([ \\t]*)<${l}(?:\\s+([^>]*))?>([\\s\\S]*?)<\\/${l}>`,"gi");for(let u of n.matchAll(p))d.push({start:u.index,content:u[0],innerContent:u[3]??""})}d.sort((l,p)=>l.start-p.start);let c=h(l=>{let p=l.split(`
2
+ `);if(p.length<=1)return l.trim();let u=p.filter(m=>m.trim().length>0);if(u.length===0)return"";let g=u.reduce((m,T)=>{let v=T.match(/^([ \t]*)/),C=v?v[1].length:0;return C<m?C:m},1/0);return p.map(m=>m.length>=g?m.slice(g):m.trimStart()).join(`
3
+ `).trim()},"processResult");return s==="all"?d.map(p=>c(p.content)).join(`
4
+ `):d.map(p=>c(p.innerContent)).join(`
5
+ `)}return n}static _processImportBlocks(t){let e=/([ \t]*)<import(?:\s+([^>]*))?\/?>/gi,i=[];return{processed:t.replace(e,(a,n,o)=>{let r={},d=/(\w+)="([^"]*)"/g;if(o)for(let u of o.matchAll(d))r[u[1]]=u[2];let c=r.src;if(!c)return"";let l;r.block&&(r.block==="all"?l="all":r.block.includes(",")?l=r.block.split(",").map(u=>u.trim()).filter(u=>u):l=r.block);let p={src:c,blocks:l,base:r.base||"default",section:r.section||"all",indent:n||""};return i.push(p),`${n}__IMPORT_${i.length-1}__`}),imports:i}}static processImportBlocks(t,e){let{processed:i,imports:s}=this._processImportBlocks(t),a=i;for(let n=0;n<s.length;n++){let o=s[n],r=e(o.src),c=(r?this._extractBlock(r,o.blocks,o.base,o.section):"").split(`
6
+ `).map((l,p)=>p===0?l:o.indent+l).join(`
7
+ `);a=a.replace(`__IMPORT_${n}__`,c)}return a}};var M=class{static{h(this,"TemplateManager")}_map=new Map;_engine=b;_watcher=[];_supportedExtensions=new Set([".html",".ts"]);_path;_isWatchEnabled;constructor(t){let{path:e,enableWatch:i=!1}=t;this._path=e,this._isWatchEnabled=i,this._validatePath(),this._initializeWatcher(t.watchOptions),this._loadAllFiles()}_validatePath(){try{if(!this._path||!Array.isArray(this._path))throw new Error("Path must be set as string array");for(let t=0;t<this._path.length;t++){let e=this._path[t];if(!P(e))throw new Error(`Template base path is not exists: ${e}`);if(!x(e).isDirectory())throw new Error(`Template base path is not a directory: ${e}`)}return!0}catch(t){throw new Error(`Failed to validate Template base path: ${String(t)}`)}}_initializeWatcher(t){if(this._isWatchEnabled){if(this._watcher.length>0)throw new Error("Watcher already enabled");for(let e=0;e<this._path.length;e++){let i=this._path[e],s=q(i,{persistent:!0,ignoreInitial:!0,awaitWriteFinish:{stabilityThreshold:100,pollInterval:50},...t});this._watcher[e]=s,s.on("change",this._handleFileChange.bind(this)),s.on("add",this._handleFileAdd.bind(this)),s.on("unlink",this._handleFileDelete.bind(this)),s.on("error",this._handleWatchError.bind(this))}}}_handleFileChange(t){let e=F(t);this._isValidFile(e)&&this._loadFile(e)}_handleFileAdd(t){let e=F(t);this._isValidFile(e)&&this._loadFile(e)}_handleFileDelete(t){let e=F(t),i=this._getKey(e);this._map.has(i)&&this._map.delete(i)}_handleWatchError(t){}_loadAllFiles(){for(let t=0;t<this._path.length;t++){let e=this._path[t],s=H(e,"utf8").filter(a=>this._isValidFile(a));for(let a of s)this._loadFile(a)}}_loadFile(t){for(let e=0;e<this._path.length;e++){let i=this._path[e],s=S(i,t),a=this._getExtension(t),n=this._getKey(t);if(a==".html"){let o=V(s,"utf8");this._compile(n,o)}else if(a==".ts"){let o=f.getFileSync(s,!1);o?.template!==void 0&&this._compile(n,o.template),o.template=void 0}else throw new Error(`File ${t} is not support`)}}_getExtension(t){return B(t).toLowerCase()}_readTemplateFile(t){for(let e of this._path){let i=B(t).toLowerCase();if(i){let n=S(e,t);if(P(n)&&x(n).isFile()){if(i===".html")return V(n,"utf8");if(i===".ts")try{return f.getFileSync(n,!1)?.template??null}catch{return null}}continue}let s=S(e,`${t}.ts`);if(P(s)&&x(s).isFile())try{return f.getFileSync(s,!1)?.template??null}catch{}let a=S(e,`${t}.html`);if(P(a)&&x(a).isFile())return V(a,"utf8")}return null}_isValidFile(t){return this._supportedExtensions.has(this._getExtension(t))}_getKey(t){return t.replace(/\.[^/.]+$/,"")}_compile(t,e,i){let s=this._map.get(t),a=s||{},n=!1,o=b.processImportBlocks(e,r=>this._readTemplateFile(r));(i===void 0||i?.media===!1||s?.media===void 0)&&(a.media=b.compileMedia(o),n=!0),(i===void 0||i?.message===!1||s?.message===void 0)&&(a.message=b.compileMessages(o),n=!0),(i===void 0||i?.keyboard===!1||s?.keyboard===void 0)&&(a.keyboard=b.compileKeyboards(o),n=!0),(i===void 0||i?.description===!1||s?.description===void 0)&&(a.description=b.compileDescription(o),n=!0),n&&this._map.set(t,a)}getMedia(t,e="default",i="default",s){let n=this._map.get(t)?.media?.[e],o=n?.[i]||n?.default;try{if(!o)throw new Error("Media function not found, please check your template");return o(s)}catch{return{}}}getMessage(t,e="default",i="default",s){try{let n=this._map.get(t)?.message?.[e],o=n?.[i]||n?.default;if(!o)throw"";return o(s)}catch{return""}}getDescription(t,e="default",i="default",s="default",a){try{let o=this._map.get(t)?.description?.[e]?.[s],r=o?.[i]||o?.default;if(!r)throw"";return r(a)}catch{return""}}getDescriptionLanguage(t,e="default",i="default"){try{let a=this._map.get(t)?.description?.[e]?.[i];return a?Object.keys(a):[]}catch{return[]}}getDescriptionScopes(t,e="default"){try{let s=this._map.get(t)?.description?.[e];return s?Object.keys(s):[]}catch{return[]}}getKeyboard(t,e="default",i="default",s="inline",a){try{let o=this._map.get(t)?.keyboard?.[e],r=o?.[i]?.[s]||o?.default?.[s];if(!r)throw[];return r(a)}catch{return[]}}async write(t,e){if(this._getExtension(t)!=".html")return null;for(let i=0;i<this._path.length;i++){let s=S(this._path[i],t);if(P(s))return z(s,e,"utf8"),this._loadFile(t),!0}return null}has(t){return this._map.has(t)}async destroy(){if(this._watcher)for(let t of this._watcher)await t.close();this._map.clear()}};import{Bot as et,webhookCallback as K}from"grammy";var $=class{static{h(this,"Gramstax")}templateManager;cacheKeyboard;cacheSession;pages;log;bot;handlerBotUpdate;constructor(t){if(!t?.token||t?.token?.length==0)throw new Error("Token is not valid or empty");if(this.log=t.log===!0?new tt:t.log?t.log:void 0,this.cacheSession=t.cacheSession||new k("memory","session"),this.cacheKeyboard=t.cacheKeyboard||new Map,this.templateManager=t.templateManager||new M({path:[Y(process.cwd(),"src","pages")]}),this.bot=this.createBot(t.token),this.bot.catch(this.onCatch),this.registerOnCallbackQueryData(),this.registerOnMessageText(),this.registerOnMessagePhoto(),this.registerOnMessageVideo(),this.registerOnMessageAudio(),this.registerOnMessageDocument(),this.registerOnMessageAnimation(),this.registerOnMessageVoice(),this.registerOnMessageVideoNote(),this.registerOnMessageSticker(),this.registerOnMessageLocation(),this.registerOnMessageContact(),this.handlerBotUpdate=this.bot.handleUpdate,this.pages=this.pageLoads(),process.once("SIGINT",()=>this.bot.stop()),process.once("SIGTERM",()=>this.bot.stop()),t.deploy.startsWith("polling"))this.runPolling();else if(t.deploy.startsWith("webhook:"))this.runWebhook(t.deploy.split("webhook:")[1]);else if(t.deploy.startsWith("serverless:"))this.runServerless(t.deploy.split("serverless:")[1]);else if(!t.deploy.startsWith("test"))throw new Error("Params deploy is not valid, expected: polling, webhook:<public-url>, serverless:<your-adapter>")}onCatch(t){this.log?.error?.(`[Bot.catch]: ${JSON.stringify(t,null,2)}`)}async onStart(t,e,i){await this.executeSyncBlocksDescription(this.pages);let{username:s}=t;this.log?.success?.(`Telegram bot polling started: ${s} ${e=="webhook"||e=="serverless"?`(${i})`:""}`)}createBot(t){return new et(t)}createCtx(t){return new I({ct:t,cacheSession:this.cacheSession,cacheKeyboard:this.cacheKeyboard,templateManager:this.templateManager,temp:{session:{},data:{}}})}runPolling(){this.bot.start({onStart:h(t=>{this.onStart(t,"polling",null)},"onStart")})}async runWebhook(t){let e=K(this.bot,"bun");this.handlerBotUpdate=e;let i=new URL(t).pathname;X({fetch(n){let{pathname:o}=new URL(n.url);if(n.method=="POST"&&o==i)return e(n);if(n.method=="GET"&&o=="/"){let r=new Date().toISOString();return new Response(JSON.stringify({status:"OK",timestamp:r}),{status:200,headers:{"Content-Type":"application/json"}})}return new Response("Not found",{status:404})}}),(await this.bot.api.getWebhookInfo()).url!=t&&await this.bot.api.setWebhook(t);let a=await this.bot.api.getMe();await this.onStart(a,"webhook",t)}async runServerless(t){this.handlerBotUpdate=K(this.bot,t);let e=await this.bot.api.getMe();await this.onStart(e,"serverless",t)}pageBuildData(t,e){t?.data===void 0&&(t.data=t.buildData()),t.data.name=Z(e).name}pageSorts(t,e){let i=t.data.name,s=t.prototype,a="Payload",n="Command",o="Caption",r="Free",d=e.routeDynamic,c=e.routeDynamicSpesific,l=h(g=>g.charAt(0).toUpperCase()+g.slice(1),"upFirst"),p=h(g=>g.replace(a,"").replace(n,"").replace(o,"").replace(r,""),"getPureRouteName"),u=h((g,m)=>s[`methodSession${l(g)}`]?.(m),"methodSession");e.all[i]=t;for(let g=0;g<e.lenListRouteStaticSession;g++){let m=e.listRouteStaticSession[g],T=e.routeStatic[m],v=e.routeStaticIntent[m],C=e.routeStaticSession[m],_={name:i,routeName:m},D=T||v;if(D!==void 0){let E=m.includes(n)?`/${i}`:i;D[s.intent(E,m.startsWith("callback"))]=_}C!==void 0&&(C[u(m,i)]=_),m.toLowerCase().endsWith("free")&&c[p(m)].push(_)}s.free&&d.push(i)}async executeSyncBlocksDescription(t){let e=new Map,i=t.routeStatic.textCommand;for(let[,s]of Object.entries(i)){let a=s.name,n=this.templateManager.getDescriptionScopes(a);for(let o of n){e.has(o)||e.set(o,new Set);let r=this.templateManager.getDescriptionLanguage(a,"default",o);for(let d of r)e.get(o).add(d)}}for(let[s,a]of e)for(let n of a){let o=[];for(let[,c]of Object.entries(i)){let l=c.name,p=this.templateManager.getDescription(l,"default",n,s);p&&o.push({command:l,description:p})}if(o.length===0)continue;let r={type:s},d=n==="default"?void 0:n;try{let c=await this.bot.api.getMyCommands({scope:r,language_code:d}),l=JSON.stringify(c.map(u=>({command:u.command,description:u.description})).sort((u,g)=>u.command.localeCompare(g.command))),p=JSON.stringify(o.sort((u,g)=>u.command.localeCompare(g.command)));l!==p&&(await this.bot.api.setMyCommands(o,{scope:r,language_code:d}),this.log?.info?.(`Synced descriptions for scope: ${s},${d?` lang: ${d},`:""} total (${o.length})`))}catch(c){this.log?.error?.(`Failed to sync descriptions for scope: ${s},${d?` lang: ${d},`:""} error: ${String(c)}`)}}}pageLoads(){this.log?.info?.("Load pages..");let t=["callback","text","textCommand","photoCaption","photoCaptionCommand","videoCaption","videoCaptionCommand","audioCaption","audioCaptionCommand","documentCaption","documentCaptionCommand","animationCaption","animationCaptionCommand","voiceCaption","voiceCaptionCommand","videoNoteCaption","videoNoteCaptionCommand"],e=["callbackPayload","textPayload","textCommandPayload","photoCaptionPayload","photoCaptionCommandPayload","videoCaptionPayload","videoCaptionCommandPayload","audioCaptionPayload","audioCaptionCommandPayload","documentCaptionPayload","documentCaptionCommandPayload","animationCaptionPayload","animationCaptionCommandPayload","voiceCaptionPayload","voiceCaptionCommandPayload","videoNoteCaptionPayload","videoNoteCaptionCommandPayload"],i=[...t,...e,"textFree","photoFree","videoFree","audioFree","documentFree","animationFree","voiceFree","videoNoteFree","stickerFree","locationFree","contactFree"],s=h((o,r)=>Object.fromEntries(o.map(d=>[d,r()])),"makeObject"),a=h(()=>[],"initDynamicSpesific"),n={all:{},routeDynamic:[],routeDynamicSpesific:{text:a(),photo:a(),video:a(),audio:a(),document:a(),animation:a(),voice:a(),videoNote:a(),sticker:a(),location:a(),contact:a()},routeStatic:s(t,()=>({})),routeStaticIntent:s(e,()=>({})),routeStaticSession:s(i,()=>({})),listRouteStatic:t,listRouteStaticIntent:e,listRouteStaticSession:i,lenListRouteStatic:t.length,lenListRouteStaticIntent:e.length,lenListRouteStaticSession:i.length};return f.getFileManySync(void 0,!0).forEach(({path:o,page:r})=>{this.pageBuildData(r,o),this.pageSorts(r,n)}),this.log?.info?.(`Finish load pages with total (${Object.keys(n.all).length})`),n}async pageRoutes(t,e){let i,s;if(i===void 0){let n=t.callbackData,o=t.msgText,r=t.msgCaption,d=this.pages.routeStatic;for(let c=0;c<this.pages.lenListRouteStatic;c++){let l=this.pages.listRouteStatic[c];if(l.startsWith(e)&&(s=d[l][n||o||r]),s)break}s!==void 0&&(i=this.pages.all[s.name])}if(i===void 0){let n=await t.sessionGet();if(n){let o=n.method,r=this.pages.routeStaticSession;for(let d=0;d<this.pages.lenListRouteStaticSession;d++){let c=this.pages.listRouteStaticSession[d];if(c.startsWith(e)&&(s=r[c][o]),s)break}s!==void 0&&(i=this.pages.all[s.name])}}if(i===void 0){let n=t.callbackDataIntent,o=t.msgTextIntent,r=t.msgCaptionIntent,d=this.pages.routeStaticIntent;for(let c=0;c<this.pages.lenListRouteStaticIntent;c++){let l=this.pages.listRouteStaticIntent[c];if(l.startsWith(e)&&(s=d[l][n||o||r]),s)break}s!==void 0&&(i=this.pages.all[s.name])}if(i!==void 0&&s!==void 0)return i.prototype[s.routeName]?await new i(t)[s.routeName]?.():void 0;let a=this.pages.routeDynamicSpesific[e]||[];for(let n=0;n<a.length;n++){let o=a[n];if(o===void 0)continue;let r=this.pages.all[o.name];if(r===void 0||!r.prototype[o.routeName])continue;let c=await new r(t)[o.routeName]?.();if(c!==null)return c}for(let n=0;n<this.pages.routeDynamic.length;n++){let o=this.pages.routeDynamic[n];if(o===void 0)continue;let r=this.pages.all[o];if(r===void 0||!r.prototype.free)continue;let c=await new r(t).free?.();if(c!==null)return c}return null}async hookBeforeRoute(t,e){return!!(t||e)}async hookErrorPage(t,e,i,s){}async hookErrorInputNotFoundPage(t){}async onMessage(t,e){let i=this.createCtx(t);try{if(await this.hookBeforeRoute(i,e)===!1)return;await this.pageRoutes(i,e)===null&&await this.hookErrorInputNotFoundPage(i)}catch(s){await this.hookErrorPage(i,e,s,!1)}}async onCallbackQueryData(t){let e=this.createCtx(t);try{if(await e.callbackQueryAnswer(),await this.hookBeforeRoute(e,"onCallbackQueryData")===!1)return;await this.pageRoutes(e,"callback")}catch(i){await this.hookErrorPage(e,"onCallbackQueryData",i,!0)}}async onMessageText(t){await this.onMessage(t,"text")}async onMessagePhoto(t){await this.onMessage(t,"photo")}async onMessageVideo(t){await this.onMessage(t,"video")}async onMessageAudio(t){await this.onMessage(t,"audio")}async onMessageDocument(t){await this.onMessage(t,"document")}async onMessageAnimation(t){await this.onMessage(t,"animation")}async onMessageVoice(t){await this.onMessage(t,"voice")}async onMessageVideoNote(t){await this.onMessage(t,"videoNote")}async onMessageSticker(t){await this.onMessage(t,"sticker")}async onMessageLocation(t){await this.onMessage(t,"location")}async onMessageContact(t){await this.onMessage(t,"contact")}registerOnCallbackQueryData(){this.bot.on("callback_query:data",this.onCallbackQueryData.bind(this))}registerOnMessageText(){this.bot.on("message:text",this.onMessageText.bind(this))}registerOnMessagePhoto(){this.bot.on("message:photo",this.onMessagePhoto.bind(this))}registerOnMessageVideo(){this.bot.on("message:video",this.onMessageVideo.bind(this))}registerOnMessageAudio(){this.bot.on("message:audio",this.onMessageAudio.bind(this))}registerOnMessageDocument(){this.bot.on("message:document",this.onMessageDocument.bind(this))}registerOnMessageAnimation(){this.bot.on("message:animation",this.onMessageAnimation.bind(this))}registerOnMessageVoice(){this.bot.on("message:voice",this.onMessageVoice.bind(this))}registerOnMessageVideoNote(){this.bot.on("message:video_note",this.onMessageVideoNote.bind(this))}registerOnMessageSticker(){this.bot.on("message:sticker",this.onMessageSticker.bind(this))}registerOnMessageLocation(){this.bot.on("message:location",this.onMessageLocation.bind(this))}registerOnMessageContact(){this.bot.on("message:contact",this.onMessageContact.bind(this))}};export{k as CacheExternal,I as Ctx,$ as Gramstax,w as LoggingUtils,b as TemplateEngine,M as TemplateManager,ot as log};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gramstax",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public",