You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There is a feature in telegraf library which lets you specify middleware for your message listeners.
I did a small hack and created a function for the same
// typestypeTBotMiddlewareFun=(msg: Message,meta: any,sendMessage: TSendMessage,next?: TBotNextFun)=>any;typeTBotNextFun=()=>any;typeTSendMessage=(message: string)=>Promise<Message>;// middleware creatorexportconstapplyBotMiddleware=(...fns: Array<TBotMiddlewareFun>)=><T>(msg: Message,meta: T)=>{constcallNext=(fn: TBotMiddlewareFun,fnIndex: number)=>{// exit when there is no next middlewareif(fnIndex>fns.length-1)return;fn(msg,meta,// A small helper to reply to current user (message: string)=>telegramApi.api.sendMessage(msg.chat.id,message),// this calls the next middlewareasync()=>{callNext(fns[++fnIndex],fnIndex)});};// calling first middlewarecallNext(fns[0],0);};// You can now use this function in on handlers like thisapi.onText(/\/start (.+)/,applyBotMiddleware(isBlocked,skipAuthorizedUsers,startController));// function signature of middlewarefunctionmiddleware(ctx: Message,match: RegExpMatchArray,reply: TSendMessage,next: TBotNextFun);
Feel free to improve the solution
The text was updated successfully, but these errors were encountered:
There is a feature in telegraf library which lets you specify middleware for your message listeners.
I did a small hack and created a function for the same
Feel free to improve the solution
The text was updated successfully, but these errors were encountered: