TBot icon indicating copy to clipboard operation
TBot copied to clipboard

crash in telegram task

Open Deenayd opened this issue 2 years ago • 0 comments

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}");
			}
		}

Deenayd avatar Oct 13 '22 06:10 Deenayd