@@ -34,29 +34,24 @@ export class CopBot {
3434 const server = http . createServer ( async ( req , res ) => {
3535 if ( req . method === 'POST' && req . url === '/webhook' ) {
3636 let body = '' ;
37-
38- // Collect data chunks
3937 req . on ( 'data' , ( chunk ) => {
4038 body += chunk ;
4139 } ) ;
42-
43- // Handle incoming request once the data is complete
4440 req . on ( 'end' , async ( ) => {
4541 try {
46- // Parse the incoming JSON body
4742 const update = JSON . parse ( body ) ;
4843 if ( ! update ) {
4944 console . error ( 'Received empty body or malformed JSON.' ) ;
5045 return ( res . statusCode = 400 ) ;
5146 }
5247
5348 console . log ( 'Received webhook body:' , update ) ;
54- await this . _bot . handleUpdate ( update ) ; // Handle the update
55- res . statusCode = 200 ; // Success response to Telegram
49+ await this . _bot . handleUpdate ( update ) ;
50+ res . statusCode = 200 ;
5651 res . end ( ) ;
5752 } catch ( error : any ) {
5853 console . error ( 'Error parsing JSON in webhook request:' , error . message || error . stack ) ;
59- res . statusCode = 500 ; // Internal Server Error
54+ res . statusCode = 500 ;
6055 res . end ( 'Internal Server Error' ) ;
6156 }
6257 } ) ;
@@ -67,26 +62,15 @@ export class CopBot {
6762 res . end ( 'Bad Request' ) ;
6863 } ) ;
6964 } else {
70- res . statusCode = 404 ; // Not Found
65+ res . statusCode = 404 ;
7166 res . end ( ) ;
7267 }
7368 } ) ;
7469 server . listen ( port , '0.0.0.0' , ( ) => {
7570 console . log ( `Bot started on port ${ port } ` ) ;
7671 } ) ;
77- try {
78- await this . _bot . api . setWebhook ( `${ web_hook } ` ) ;
79- console . log ( `Webhook set successfully to: ${ web_hook } ` ) ;
80- const webhookStatus = await this . _bot . api . getWebhookInfo ( ) ;
81- if ( webhookStatus . url ) {
82- console . log ( `Webhook is already set to: ${ webhookStatus . url } ` ) ;
83- } else {
84- console . log ( 'No webhook set.' ) ;
85- }
86- } catch ( error : any ) {
87- console . error ( 'Error setting webhook:' , error . message || error . stack ) ;
88- process . exit ( 1 ) ; // Exit if critical error
89- }
72+ await this . _bot . api . setWebhook ( `${ web_hook } ` ) ;
73+ console . log ( `Webhook set successfully to: ${ web_hook } ` ) ;
9074 } else {
9175 try {
9276 await this . _bot . start ( {
@@ -96,7 +80,7 @@ export class CopBot {
9680 } ) ;
9781 } catch ( error ) {
9882 console . error ( 'Error starting bot in long-polling mode:' , error ) ;
99- process . exit ( 1 ) ; // Exit if the bot fails to start
83+ process . exit ( 1 ) ;
10084 }
10185 }
10286 }
@@ -179,9 +163,9 @@ export class CopBot {
179163 }
180164 @Catch ( )
181165 async initial ( ) : Promise < void > {
166+ await this . start ( ) ;
182167 new GenerateCommand ( this . _bot ) . generate ( ) ;
183168 await this . message ( ) ;
184- await this . start ( ) ;
185169 console . log ( 'Bot Is Start' ) ;
186170 }
187171}
0 commit comments