@@ -78,7 +78,6 @@ export class CopBot {
7878 app . listen ( port , async ( ) => {
7979 logger . info ( `Webhook server running on port ${ port } ` ) ;
8080 let webhookInfo = await this . _bot . api . getWebhookInfo ( ) ;
81- console . log ( `Webhook Info: ` , webhookInfo ) ;
8281 logger . info ( `Current Webhook: ${ webhookInfo . url } ` ) ;
8382 const MAX_RETRIES = 5 ;
8483 let retries = 0 ;
@@ -89,7 +88,6 @@ export class CopBot {
8988 logger . info ( 'Webhook is not set. Trying to set the webhook...' ) ;
9089 await this . _bot . api . setWebhook ( webhookURL ) ;
9190 webhookInfo = await this . _bot . api . getWebhookInfo ( ) ;
92- console . log ( `Updated Webhook Info: ` , webhookInfo ) ;
9391 logger . info ( `Webhook set: ${ webhookInfo . url } ` ) ;
9492 } else {
9593 logger . info ( 'Webhook is already set.' ) ;
@@ -139,37 +137,39 @@ export class CopBot {
139137 @SaveUserData ( )
140138 @MessageValidator ( )
141139 async handleMessage ( ctx : Context ) {
142- if ( ! ctx . message ?. text ) {
143- console . warn ( 'Message text is undefined' ) ;
144- return ;
145- }
146- console . log ( 'ctx.message.text:' , ctx . message ?. text ) ;
147- console . log ( 'ctx.chat' , ctx . chat ) ;
148- console . log ( 'ctx.message.from:' , ctx . message ?. from ) ;
149- const messageText = ctx . message ?. text ?. toLowerCase ( ) . trim ( ) ;
150- const reply = new BotReply ( ctx ) ;
151- const user = ctx . message ?. reply_to_message ?. from ;
152- if ( messageText === 'ask' && user ) {
153- const name = user . username ? `@${ user . username } ` : user . first_name ;
154- const responseMessage = `Dear ${ name } , ask your question correctly.\nIf you want to know how to do this, read the article below:\ndontasktoask.ir` ;
155- await reply . textReply ( responseMessage ) ;
156- return ;
157- }
158- if ( ctx . message ?. entities ) {
159- const commandEntity = ctx . message . entities . find ( ( entity ) => entity . type === 'bot_command' ) ;
160- console . log ( 'commandEntity' , commandEntity ) ;
161- if ( commandEntity ) {
162- let command = messageText ?. split ( ' ' ) [ 0 ] ?. replace ( '/' , '' ) ! ;
163- command = command . includes ( '@' ) ? command . split ( '@' ) [ 0 ] : command ;
164- console . log ( 'command' , command ) ;
165- const handler = ( GeneralCommands as any ) [ command ] || ( UserCommands as any ) [ command ] || ( AdminCommands as any ) [ command ] ;
166- if ( typeof handler === 'function' ) {
167- await handler ( ctx ) ;
168- return ;
140+ try {
141+ if ( ! ctx . message ?. text ) {
142+ logger . warn ( 'Message text is undefined' ) ;
143+ return ;
144+ }
145+ const messageText = ctx . message ?. text ?. toLowerCase ( ) . trim ( ) ;
146+ const reply = new BotReply ( ctx ) ;
147+ const user = ctx . message ?. reply_to_message ?. from ;
148+
149+ if ( messageText === 'ask' && user ) {
150+ const name = user . username ? `@${ user . username } ` : user . first_name ;
151+ const responseMessage = `Dear ${ name } , ask your question correctly.\nIf you want to know how to do this, read the article below:\ndontasktoask.ir` ;
152+ await reply . textReply ( responseMessage ) ;
153+ }
154+
155+ if ( ctx . message ?. entities ) {
156+ const commandEntity = ctx . message . entities . find ( ( entity ) => entity . type === 'bot_command' ) ;
157+ if ( commandEntity ) {
158+ let command = messageText ?. split ( ' ' ) [ 0 ] ?. replace ( '/' , '' ) ! ;
159+ logger . debug ( `Processing command: ${ command } ` ) ;
160+ command = command . includes ( '@' ) ? command . split ( '@' ) [ 0 ] : command ;
161+ const handler = ( GeneralCommands as any ) [ command ] || ( UserCommands as any ) [ command ] || ( AdminCommands as any ) [ command ] ;
162+ if ( typeof handler === 'function' ) {
163+ await handler ( ctx ) ;
164+ return ;
165+ }
169166 }
170167 }
168+ } catch ( err : any ) {
169+ logger . error ( 'Error in handleMessage:' , err . message ) ;
170+ const reply = new BotReply ( ctx ) ;
171+ await reply . textReply ( 'An unexpected error occurred.' ) ;
171172 }
172- return ;
173173 }
174174
175175 @SaveUserData ( )
@@ -184,7 +184,6 @@ export class CopBot {
184184 }
185185 const reply = new BotReply ( ctx ) ;
186186 const from = ctx . from ;
187- console . log ( `Bot added to group ${ chat . title } by ${ from ?. username } ` ) ;
188187 const message = `
189188 Hello ${ ctx . chat ?. title } !
190189First of all, thanks to @${ from ?. username ! } for inviting me to this awesome group!
0 commit comments