@@ -8,6 +8,7 @@ import * as express from 'express';
88import { BotReply } from '../utils/chat/BotReply' ;
99import logger from '../utils/logger' ;
1010import { limit } from '@grammyjs/ratelimiter' ;
11+ import { ServiceProvider } from '../service/database/ServiceProvider' ;
1112export class CopBot {
1213 private static instance : CopBot ;
1314 private _bot : Bot < Context > ;
@@ -52,16 +53,18 @@ export class CopBot {
5253 const app = express ( ) ;
5354 app . use ( express . json ( ) ) ;
5455 app . post ( webhookPath , async ( req , res ) => {
55- setImmediate ( async ( ) => {
56- try {
57- await webhookCallback ( this . _bot , 'express' ) ( req , res ) ;
58- } catch ( error : any ) {
59- logger . error ( `Error processing update:${ error } ` ) ;
60- return ;
61- }
62- } ) ;
56+ res . sendStatus ( 200 ) ;
57+ try {
58+ await webhookCallback ( this . _bot , 'express' ) ( req , res ) ;
59+ } catch ( error : any ) {
60+ logger . error ( `Error processing update : ${ error . message } ` ) ;
61+ return ;
62+ }
63+ } ) ;
64+ app . get ( '/health' , async ( _ , res ) => {
65+ const isHealthy = await ServiceProvider . getInstance ( ) . healthCheck ( ) ;
66+ res . status ( isHealthy ? 200 : 500 ) . send ( isHealthy ? 'OK' : 'Database not reachable' ) ;
6367 } ) ;
64-
6568 const port = process . env . PORT || 3000 ;
6669 app . listen ( port , async ( ) => {
6770 logger . info ( `Webhook server running on port ${ port } ` ) ;
@@ -74,7 +77,7 @@ export class CopBot {
7477 try {
7578 if ( ! webhookInfo . url ) {
7679 logger . warn ( 'Webhook is not set. Trying to set the webhook...' ) ;
77- await this . _bot . api . setWebhook ( webhookURL , { max_connections : 100 } ) ;
80+ await this . _bot . api . setWebhook ( webhookURL , { drop_pending_updates : true } ) ;
7881 webhookInfo = await this . _bot . api . getWebhookInfo ( ) ;
7982 logger . info ( `Webhook set: ${ webhookInfo . url } ` ) ;
8083 } else {
@@ -96,6 +99,7 @@ export class CopBot {
9699 } ;
97100
98101 await trySetWebhook ( ) ;
102+ await startBot ( mode ) ;
99103 } ) ;
100104 } catch ( err : any ) {
101105 console . error ( 'Error setting up webhook:' , err ) ;
@@ -112,6 +116,11 @@ export class CopBot {
112116 }
113117 async initial ( ) : Promise < void > {
114118 new GenerateCommand ( this . _bot ) . generate ( ) ;
119+ this . _bot . use (
120+ limit ( {
121+ onLimitExceeded : ( ctx ) => ctx . reply ( 'Too many requests! Please slow down.' ) ,
122+ } )
123+ ) ;
115124 this . _bot . on ( 'my_chat_member' , ( ctx ) => this . handleJoinNewChat ( ctx ) ) ;
116125 this . _bot . on ( 'message' , ( ctx ) => this . handleMessage ( ctx ) ) ;
117126 this . _bot . catch ( async ( error : BotError < Context > ) => {
@@ -120,11 +129,6 @@ export class CopBot {
120129 }
121130 logger . error ( `Bot error occurred: ${ error . error } ` ) ;
122131 } ) ;
123- this . _bot . use (
124- limit ( {
125- onLimitExceeded : ( ctx ) => ctx . reply ( 'Too many requests! Please slow down.' ) ,
126- } )
127- ) ;
128132 await this . start ( ) ;
129133 logger . info ( 'Bot is running' ) ;
130134 }
0 commit comments