Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bot Error Handling in NestJS Application #1156

Closed
shahriarenayaty opened this issue Oct 13, 2023 · 2 comments
Closed

Bot Error Handling in NestJS Application #1156

shahriarenayaty opened this issue Oct 13, 2023 · 2 comments

Comments

@shahriarenayaty
Copy link

Hello every body,
I am encountering an issue with my Telegraf bot integrated into a NestJS application. When the bot encounters an error, it causes my entire application to crash, which is not the desired behavior. Instead, I want my application to log the error and continue functioning without any disruptions.
e.g. this error:
Error: 409: Conflict: terminated by other getUpdates request; make sure that only one bot instance is running
FetchError: request to https://api.telegram.org/bot**********:[REDACTED]/getMe failed, reason: connect ETIMEDOUT 10.10.34.36:443
Proposed Solution:
I believe the ideal solution for this issue is to make use of the bot.catch method in Telegraf, which is designed for error handling within the bot itself. This way, the bot can log errors and continue processing, ensuring that the NestJS application remains functional even when bot errors occur.

Code Attempted
I have tried the following code to handle errors within my Telegraf bot:

@Injectable()
export class TelegramBotService {
  constructor(
    @InjectBot(TelegramBotName)
    private bot: Telegraf<TelegramContext>,
    private readonly developerService: DeveloperService,
  ) {
    this.bot.catch((error, ctx: TelegramContext) => {
      console.error('Telegraf error:', error);
      console.log(ctx);
    });
  }

and this is how I add my telegrafModule to App Module

@Module({
  imports: [
      TelegrafModule.forRootAsync({
          imports: [ConfigModule],
          inject: [ConfigService], // Inject ConfigService
          botName: TelegramBotName,
          useFactory: (configService: ConfigService) => ({
            token: configService.get('TELEGRAM_TOKEN'),
            middlewares: [sessionMiddleware],
            include: [TelegramBotModule],
          }),
      })
  ]
  })
export class AppModule {}

Thank you for your assistance in finding a solution to this issue. If you have any recommendations or suggestions on how to implement proper error handling for the Telegraf bot within a NestJS application, please share them. Your input will be greatly appreciated.

@0x467
Copy link
Collaborator

0x467 commented Jun 26, 2024

Fixed in 9c0286c

@0x467 0x467 closed this as completed Jun 26, 2024
@llegoelkelo
Copy link

@0x467 this is still happening on the latest release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants