@@ -2,14 +2,11 @@ import { Bot, webhookCallback } from 'grammy';
22import type { Context } from 'grammy' ;
33import Config from '../config' ;
44import { GenerateCommand } from './handlers/GenerateCommands' ;
5- import { GeneralCommands } from './commands/genearl/GeneralCommands' ;
6- import { UserCommands } from './commands/user/UserCommands' ;
7- import { AdminCommands } from './commands/admin/AdminCommands' ;
85import { SaveUserData } from '../decorators/Database' ;
96import { MessageValidator } from '../decorators/Context' ;
7+ import * as express from 'express' ;
108import { BotReply } from '../utils/chat/BotReply' ;
119import logger from '../utils/logger' ;
12- import * as express from 'express' ;
1310export class CopBot {
1411 private static instance : CopBot ;
1512 private _bot : Bot < Context > ;
@@ -53,25 +50,13 @@ export class CopBot {
5350 app . use ( express . json ( ) ) ;
5451 app . post ( webhookPath , async ( req , res ) => {
5552 res . sendStatus ( 200 ) ;
56- const MAX_RETRIES = 3 ;
57- let retryCount = 0 ;
58-
59- const processWebhook = async ( ) => {
53+ setImmediate ( async ( ) => {
6054 try {
61- // Process the webhook
6255 await webhookCallback ( this . _bot , 'express' ) ( req , res ) ;
63- } catch ( err ) {
64- if ( retryCount < MAX_RETRIES ) {
65- retryCount ++ ;
66- const delay = Math . min ( 1000 * 2 ** retryCount , 30000 ) ;
67- await new Promise ( ( resolve ) => setTimeout ( resolve , delay ) ) ;
68- await processWebhook ( ) ;
69- } else {
70- console . error ( 'Max retries reached. Webhook processing failed.' ) ;
71- }
56+ } catch ( error : any ) {
57+ logger . error ( `Error processing update:${ error } ` ) ;
7258 }
73- } ;
74- await processWebhook ( ) ;
59+ } ) ;
7560 } ) ;
7661
7762 const port = process . env . PORT || 3000 ;
@@ -85,7 +70,7 @@ export class CopBot {
8570 const trySetWebhook = async ( ) => {
8671 try {
8772 if ( ! webhookInfo . url ) {
88- logger . info ( 'Webhook is not set. Trying to set the webhook...' ) ;
73+ logger . warn ( 'Webhook is not set. Trying to set the webhook...' ) ;
8974 await this . _bot . api . setWebhook ( webhookURL ) ;
9075 webhookInfo = await this . _bot . api . getWebhookInfo ( ) ;
9176 logger . info ( `Webhook set: ${ webhookInfo . url } ` ) ;
@@ -97,7 +82,7 @@ export class CopBot {
9782 logger . error ( `Error setting webhook (Attempt ${ retries } ): ${ error . message } ` ) ;
9883 if ( retries < MAX_RETRIES ) {
9984 const delay = Math . min ( 1000 * 2 ** retries , 30000 ) ; // Exponential backoff
100- logger . info ( `Retrying in ${ delay / 1000 } seconds...` ) ;
85+ logger . warn ( `Retrying in ${ delay / 1000 } seconds...` ) ;
10186 await new Promise ( ( resolve ) => setTimeout ( resolve , delay ) ) ;
10287 await trySetWebhook ( ) ;
10388 } else {
@@ -130,48 +115,11 @@ export class CopBot {
130115 this . _bot . catch ( ( err ) => {
131116 console . error ( 'Middleware error:' , err ) ;
132117 } ) ;
133-
134118 await this . start ( ) ;
135119 logger . info ( 'Bot is running' ) ;
136120 }
137- @SaveUserData ( )
138121 @MessageValidator ( )
139- async handleMessage ( ctx : Context ) {
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- }
166- }
167- }
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.' ) ;
172- }
173- }
174-
122+ async handleMessage ( ctx : Context ) { }
175123 @SaveUserData ( )
176124 async handleJoinNewChat ( ctx : Context ) {
177125 if ( ! ctx . message ?. text ) {
0 commit comments