From 43fe6533d2b74eb5a8ae1c90e4032dd29c62f6fd Mon Sep 17 00:00:00 2001 From: FlorianDahn Date: Thu, 30 Aug 2018 01:44:33 +0200 Subject: [PATCH] added more exception handling --- TelegramBotBase/BotBase.cs | 4 ++ TelegramBotBase/Sessions/DeviceSession.cs | 56 +++++++++++++++++++---- TelegramBotBase/TelegramBotBase.csproj | 4 +- TelegramBotBase/packages.config | 2 +- 4 files changed, 55 insertions(+), 11 deletions(-) diff --git a/TelegramBotBase/BotBase.cs b/TelegramBotBase/BotBase.cs index 699620d..9a5731b 100644 --- a/TelegramBotBase/BotBase.cs +++ b/TelegramBotBase/BotBase.cs @@ -85,6 +85,10 @@ namespace TelegramBotBase try { Client_TryMessage(sender, e); + } + catch (Telegram.Bot.Exceptions.ApiRequestException ex) + { + } catch (Exception ex) { diff --git a/TelegramBotBase/Sessions/DeviceSession.cs b/TelegramBotBase/Sessions/DeviceSession.cs index 8b78abb..2376bfd 100644 --- a/TelegramBotBase/Sessions/DeviceSession.cs +++ b/TelegramBotBase/Sessions/DeviceSession.cs @@ -95,9 +95,23 @@ namespace TelegramBotBase.Sessions markup = buttons; } - var message = await (this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, text, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification)); + Message m = null; - OnMessageSent(new MessageSentEventArgs(message.MessageId, message)); + try + { + m = await (this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, text, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification)); + } + catch (Telegram.Bot.Exceptions.ApiRequestException ex) + { + return; + } + catch + { + return; + } + + + OnMessageSent(new MessageSentEventArgs(m.MessageId, m)); } /// @@ -107,14 +121,27 @@ namespace TelegramBotBase.Sessions /// /// /// - public async Task Send(String text, InlineKeyboardMarkup markup , int replyTo = 0, bool disableNotification = false) + public async Task Send(String text, InlineKeyboardMarkup markup, int replyTo = 0, bool disableNotification = false) { if (this.ActiveForm == null) return; - var message = await (this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, text, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification)); + Message m = null; - OnMessageSent(new MessageSentEventArgs(message.MessageId, message)); + try + { + m = await (this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, text, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification)); + } + catch (Telegram.Bot.Exceptions.ApiRequestException ex) + { + return; + } + catch + { + return; + } + + OnMessageSent(new MessageSentEventArgs(m.MessageId, m)); } /// @@ -135,9 +162,22 @@ namespace TelegramBotBase.Sessions markup = buttons; } - var message = await this.Client.TelegramClient.SendPhotoAsync(this.DeviceId, file, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification); + Message m = null; - OnMessageSent(new MessageSentEventArgs(message.MessageId, message)); + try + { + m = await this.Client.TelegramClient.SendPhotoAsync(this.DeviceId, file, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification); + } + catch (Telegram.Bot.Exceptions.ApiRequestException ex) + { + return; + } + catch + { + return; + } + + OnMessageSent(new MessageSentEventArgs(m.MessageId, m)); } /// @@ -212,7 +252,7 @@ namespace TelegramBotBase.Sessions markup = buttons; } - var message = await this.Client.TelegramClient.SendDocumentAsync(this.DeviceId, document, caption, replyMarkup: markup, disableNotification: disableNotification, replyToMessageId: replyTo); + var message = await this.Client.TelegramClient.SendDocumentAsync(this.DeviceId, document, caption, replyMarkup: markup, disableNotification: disableNotification, replyToMessageId: replyTo); OnMessageSent(new MessageSentEventArgs(message.MessageId, message)); } diff --git a/TelegramBotBase/TelegramBotBase.csproj b/TelegramBotBase/TelegramBotBase.csproj index 9ddb73d..148d417 100644 --- a/TelegramBotBase/TelegramBotBase.csproj +++ b/TelegramBotBase/TelegramBotBase.csproj @@ -50,8 +50,8 @@ - - ..\packages\Telegram.Bot.14.4.0\lib\net45\Telegram.Bot.dll + + ..\..\..\UGI\packages\Telegram.Bot.14.6.0\lib\net45\Telegram.Bot.dll True diff --git a/TelegramBotBase/packages.config b/TelegramBotBase/packages.config index 1055053..61cd9f7 100644 --- a/TelegramBotBase/packages.config +++ b/TelegramBotBase/packages.config @@ -34,5 +34,5 @@ - + \ No newline at end of file