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

crash in telegram task #124

Open
Deenayd opened this issue Oct 13, 2022 · 0 comments
Open

crash in telegram task #124

Deenayd opened this issue Oct 13, 2022 · 0 comments

Comments

@Deenayd
Copy link

Deenayd commented Oct 13, 2022

I have been getting rendom crashes in TBot and was trying to chase it.

Finally found it in telegram. Looks like a random connectivity problem causes it to throw an exception. Like this:

[Error] [Telegram] [13.10.2022 00:16:34] - Unexpected exception: Bad Gateway
[Warning] [Telegram] [13.10.2022 00:16:34] - Stacktrace:    at Telegram.Bot.TelegramBotClient.MakeRequestAsync[TResponse](IRequest`1 request, CancellationToken cancellationToken)
   at Telegram.Bot.TelegramBotClientExtensions.SendTextMessageAsync(ITelegramBotClient botClient, ChatId chatId, String text, Nullable`1 parseMode, IEnumerable`1 entities, Nullable`1 disableWebPagePreview, Nullable`1 disableNotification, Nullable`1 protectContent, Nullable`1 replyToMessageId, Nullable`1 allowSendingWithoutReply, IReplyMarkup replyMarkup, CancellationToken cancellationToken)
   at Tbot.Includes.TelegramMessenger.HandleErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken) in C:\Users\Sławomir\source\repos\TBot2\TBot\Includes\TelegramMessenger.cs:line 907
   at Telegram.Bot.Polling.DefaultUpdateHandler.HandlePollingErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken)
   at Telegram.Bot.Polling.DefaultUpdateReceiver.ReceiveAsync(IUpdateHandler updateHandler, CancellationToken cancellationToken)
   at Telegram.Bot.TelegramBotClientExtensions.ReceiveAsync(ITelegramBotClient botClient, IUpdateHandler updateHandler, ReceiverOptions receiverOptions, CancellationToken cancellationToken)
   at Telegram.Bot.TelegramBotClientExtensions.ReceiveAsync(ITelegramBotClient botClient, Func`4 updateHandler, Func`4 pollingErrorHandler, ReceiverOptions receiverOptions, CancellationToken cancellationToken)
   at Tbot.Includes.TelegramMessenger.TelegramBot() in ******\TBot\Includes\TelegramMessenger.cs:line 922

In original TBot code it used to be an unhandled exception. Guess it would be nice to catch it, log and restart telegram. I am not restarting telegram and I'm not sure how to do that correctly. So my code just catchs exceptions and logs them:

		public async void TelegramBot() {

			var cts = new CancellationTokenSource();
			var cancellationToken = cts.Token;

			var receiverOptions = new ReceiverOptions {
				AllowedUpdates = Array.Empty<UpdateType>(),
				ThrowPendingUpdates = true
			};

			try {
				await Client.ReceiveAsync(HandleUpdateAsync, HandleErrorAsync, receiverOptions, cts.Token);
			} catch (Exception e) {
				Helpers.WriteLog(LogType.Error, LogSender.Telegram, $"Unexpected exception: {e.Message}");
				Helpers.WriteLog(LogType.Warning, LogSender.Telegram, $"Stacktrace: {e.StackTrace}");
			}
		}
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

1 participant