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

HandlePollingErrorAsync stop bot #1268

Closed
MansoorJafari9 opened this issue Jun 3, 2023 · 2 comments
Closed

HandlePollingErrorAsync stop bot #1268

MansoorJafari9 opened this issue Jun 3, 2023 · 2 comments
Labels
✔️ Resolution: Duplicate Resolved as a duplicate of another issue ✔️ Resolution: Won't Fix Resolved because we decided not to change the behavior reported in this issue.

Comments

@MansoorJafari9
Copy link

MansoorJafari9 commented Jun 3, 2023

Hi
When My bot got exception and it catch into HandlePollingErrorAsync my application stopped and I have to re open application
can you help me how can I change my code

`async Task HandlePollingErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken)
{
// Get the line number of the exception
var lineNumber = new StackTrace(exception, true).GetFrame(0).GetFileLineNumber();

if (exception is ApiRequestException apiException)
{
    await botClient.SendTextMessageAsync(3, apiException.Message + $" on line {lineNumber}");
    // handle Telegram API errors
    Console.WriteLine($"Telegram API error occurred: {apiException.Message} on line {lineNumber}");
}
else
{
    await botClient.SendTextMessageAsync(53608103, exception.Message + $" on line {lineNumber}");
    // handle all other errors
    Console.WriteLine($"Error occurred: {exception.Message} on line {lineNumber}");
}

// continue polling
await Task.Delay(1000);

}`

@tuscen
Copy link
Member

tuscen commented Jun 3, 2023

The poller doesn't catch exceptions from your own code. So if you have an exception inside your HandlePollingErrorAsync method it won't catch it, you need to ensure that your own code is exception safe. Try using try/catch clause.

@tuscen tuscen added the ✔️ Resolution: Won't Fix Resolved because we decided not to change the behavior reported in this issue. label Jun 3, 2023
@karb0f0s karb0f0s added the ✔️ Resolution: Duplicate Resolved as a duplicate of another issue label Aug 24, 2023
@karb0f0s
Copy link
Member

Duplicate for #1106

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✔️ Resolution: Duplicate Resolved as a duplicate of another issue ✔️ Resolution: Won't Fix Resolved because we decided not to change the behavior reported in this issue.
Projects
None yet
Development

No branches or pull requests

3 participants