import { Context , session , Telegraf } from 'telegraf' ; interface SessionData { messageCount : number ; } interface MyContext extends Context { session ? : SessionData ; } if ( process . env . BOT_TOKEN === undefined ) { throw new TypeError ( 'BOT_TOKEN must be provided!' ) ; } const bot = new Telegraf < MyContext > ( process . env . BOT_TOKEN ) ; bot . use ( session ( ) ) ; bot . on ( 'message' , async ( ctx ) => { ctx . session ??= { messageCount : 0 } ; ctx . session . messageCount ++ ; await ctx . reply ( `Seen ${ ctx . session . messageCount } messages.` ) ; } ) ; bot . launch ( ) ; process . once ( 'SIGINT' , ( ) => bot . stop ( 'SIGINT' ) ) ; process . once ( 'SIGTERM' , ( ) => bot . stop ( 'SIGTERM' ) ) ;