@@ -13,6 +13,10 @@ export class CopBot {
13
13
private static instance : CopBot ;
14
14
private _bot : Bot < Context > ;
15
15
private healthCheckInterval : NodeJS . Timeout | null = null ;
16
+ private isProduction = Config . environment === 'production' ;
17
+ private webhookPath = `/bot/${ Config . token } ` ;
18
+ private webhookURL = `${ Config . web_hook } ${ this . webhookPath } ` ;
19
+ private mode = this . isProduction ? 'webhook' : 'long-polling' ;
16
20
private constructor ( ) {
17
21
this . _bot = new Bot < Context > ( Config . token ) ;
18
22
}
@@ -42,21 +46,15 @@ export class CopBot {
42
46
} ,
43
47
} ) ;
44
48
} ;
45
-
46
- const isProduction = Config . environment === 'production' ;
47
- const webhookPath = `/bot/${ Config . token } ` ;
48
- const webhookURL = `${ Config . web_hook } ${ webhookPath } ` ;
49
- const mode = isProduction ? 'webhook' : 'long-polling' ;
50
-
51
49
logger . info ( `Environment: ${ Config . environment } ` ) ;
52
- logger . info ( `Webhook URL: ${ webhookURL } ` ) ;
53
- logger . info ( `Running in ${ mode } mode` ) ;
50
+ logger . info ( `Webhook URL: ${ this . webhookURL } ` ) ;
51
+ logger . info ( `Running in ${ this . mode } mode` ) ;
54
52
55
- if ( isProduction ) {
53
+ if ( this . isProduction ) {
56
54
try {
57
55
const app = express ( ) ;
58
56
app . use ( express . json ( ) ) ;
59
- app . post ( webhookPath , async ( req , res ) => {
57
+ app . post ( this . webhookPath , async ( req , res ) => {
60
58
res . sendStatus ( 200 ) ;
61
59
try {
62
60
await webhookCallback ( this . _bot , 'express' ) ( req , res ) ;
@@ -83,16 +81,16 @@ export class CopBot {
83
81
const port = process . env . PORT || 3000 ;
84
82
app . listen ( port , async ( ) => {
85
83
logger . info ( `Webhook server running on port ${ port } ` ) ;
86
- await this . setupWebhook ( webhookURL ) ;
87
- await startBot ( mode ) ;
84
+ await this . setupWebhook ( this . webhookURL ) ;
85
+ await startBot ( this . mode ) ;
88
86
} ) ;
89
87
} catch ( err : any ) {
90
88
console . error ( 'Error setting up webhook:' , err ) ;
91
89
process . exit ( 1 ) ;
92
90
}
93
91
} else {
94
92
try {
95
- await startBot ( mode ) ;
93
+ await startBot ( this . mode ) ;
96
94
} catch ( err : any ) {
97
95
console . error ( 'Error during long-polling mode:' , err ) ;
98
96
process . exit ( 1 ) ;
@@ -125,7 +123,12 @@ export class CopBot {
125
123
logger . warn ( 'Database health check failed. Attempting to reconnect...' ) ;
126
124
await ServiceProvider . initialize ( ) ;
127
125
}
128
- logger . info ( 'Bot is live' ) ;
126
+ const webhookInfo = await this . _bot . api . getWebhookInfo ( ) ;
127
+ if ( ! webhookInfo . url || webhookInfo . url !== this . webhookURL ) {
128
+ logger . warn ( 'Webhook mismatch detected. Resetting webhook...' ) ;
129
+ await this . setupWebhook ( this . webhookURL ) ;
130
+ }
131
+ logger . info ( 'Health check passed: Bot is live' ) ;
129
132
} catch ( error : any ) {
130
133
logger . error ( `Health check failed: ${ error . message } ` ) ;
131
134
}
0 commit comments