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

Server crashes when Telegram connection fails or returns a Bad Request error #347

Open
hoangbao0201 opened this issue Nov 13, 2024 · 2 comments

Comments

@hoangbao0201
Copy link

hoangbao0201 commented Nov 13, 2024

I'm currently using the nestjs-telegram module in my NestJS project to integrate with Telegram Bot API. However, I'm encountering an issue where the server crashes if there is a connection problem with Telegram, or if a Bad Request error occurs during communication with the Telegram API.

Whenever a connection to Telegram fails or an invalid request is made, it causes the server to crash, which is disruptive to the application's operation. I would like to know if the nestjs-telegram module is designed to handle such errors gracefully and prevent server crashes.

Specifically, when an error like 401 Unauthorized or a timeout occurs, the bot should log the error and continue running without impacting the rest of the application.

Steps to reproduce:

Configure nestjs-telegram with a valid Telegram bot token.
Simulate a failed connection to Telegram (e.g., invalid token or network issue).
Observe that the server crashes or the application stops running.
Expected behavior:

The server should not crash when an error occurs while connecting to the Telegram API.
The bot should handle connection errors (e.g., bad requests, timeouts) gracefully and continue running.
Errors should be logged properly for further debugging, but should not cause a complete server failure.
Actual behavior:

The server crashes or stops responding when there is a connection issue with Telegram or a Bad Request error.
Additional context:

I'm using the latest version of nestjs-telegram.
I'm using NestJS with a custom error handling mechanism to prevent server crashes in other parts of the application.
The Telegram bot token is correctly set in the configuration, but errors still occur.

1
a
b

@truongbo17
Copy link

try this, this may not crash app.

 this.telegramService
          .sendMessage({
            chat_id: <string | number>(
              this.configService.get('telegram.groupNotify.error404')
            ),
            text: <string>job.data.message,
            parse_mode: 'markdown',
          })
          .pipe(
            delay(1000),
            catchError(() => {
              return of({} as TelegramMessage);
            })
          )
          .subscribe();

@truongbo17
Copy link

Screen Shot 2024-12-24 at 11 18 17

Update 24/12/2024, Really doesn't completely solve the problem of app crashing when sending message with response status code 400 or 502. I think we should use axios to call http request to send message.

try this, this may not crash app.

 this.telegramService
          .sendMessage({
            chat_id: <string | number>(
              this.configService.get('telegram.groupNotify.error404')
            ),
            text: <string>job.data.message,
            parse_mode: 'markdown',
          })
          .pipe(
            delay(1000),
            catchError(() => {
              return of({} as TelegramMessage);
            })
          )
          .subscribe();

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

2 participants