@@ -17,6 +17,7 @@ export class CopBot {
1717 private webhookPath = `/bot/${ Config . token } ` ;
1818 private webhookURL = `${ Config . web_hook } ${ this . webhookPath } ` ;
1919 private mode = this . isProduction ? 'webhook' : 'long-polling' ;
20+ private static latestContext : Context | null = null ;
2021 private constructor ( ) {
2122 this . _bot = new Bot < Context > ( Config . token ) ;
2223 }
@@ -27,6 +28,19 @@ export class CopBot {
2728 }
2829 return CopBot . instance ;
2930 }
31+ public static setContext ( ctx : Context ) : void {
32+ logger . info ( `Setting new context: at ${ new Date ( ) . toISOString ( ) } ` ) ;
33+ this . latestContext = ctx ;
34+ }
35+
36+ public static getContext ( ) : Context | null {
37+ if ( this . latestContext ) {
38+ logger . info ( `Retrieved latest context: at ${ new Date ( ) . toISOString ( ) } ` ) ;
39+ } else {
40+ logger . warn ( 'Attempted to retrieve context, but no context is set.' ) ;
41+ }
42+ return this . latestContext ;
43+ }
3044 // Stop the bot
3145 async stop ( ) : Promise < void > {
3246 if ( this . healthCheckInterval ) {
@@ -82,6 +96,7 @@ export class CopBot {
8296 app . listen ( port , async ( ) => {
8397 logger . info ( `Webhook server running on port ${ port } ` ) ;
8498 await this . setupWebhook ( this . webhookURL ) ;
99+ logger . info ( `Bot started in ${ this . mode } mode!` ) ;
85100 } ) ;
86101 } catch ( err : any ) {
87102 console . error ( 'Error setting up webhook:' , err ) ;
@@ -100,11 +115,26 @@ export class CopBot {
100115 new GenerateCommand ( this . _bot ) . generate ( ) ;
101116 this . _bot . use (
102117 limit ( {
103- onLimitExceeded : ( ctx ) => ctx . reply ( 'Too many requests! Please slow down.' ) ,
118+ onLimitExceeded : async ( ctx : Context , next : ( ) => Promise < void > ) => {
119+ const waitTime = '1000' ;
120+ const message = `⚠️ Rate limit exceeded. Please wait for ${ waitTime } ms before trying again.` ;
121+
122+ try {
123+ await ctx . reply ( message ) ;
124+ } catch ( error : any ) {
125+ logger . error ( `Failed to send rate limit message: ${ error . message } ` ) ;
126+ }
127+ if ( next ) {
128+ await next ( ) ;
129+ }
130+ } ,
104131 } )
105132 ) ;
106133 this . _bot . on ( 'my_chat_member' , ( ctx ) => this . handleJoinNewChat ( ctx ) ) ;
107- this . _bot . on ( 'message' , ( ctx ) => this . handleMessage ( ctx ) ) ;
134+ this . _bot . on ( 'message' , ( ctx ) => {
135+ CopBot . setContext ( ctx ) ;
136+ this . handleMessage ( ctx ) ;
137+ } ) ;
108138 this . _bot . catch ( async ( error : BotError < Context > ) => {
109139 if ( error . message . includes ( 'timeout' ) ) {
110140 await error . ctx . reply ( 'The request took too long to process. Please try again later.' ) ;
0 commit comments