From 73ba36ea2b77804aabb228191f65804a572946b1 Mon Sep 17 00:00:00 2001 From: FlorianDahn Date: Tue, 22 Dec 2020 21:55:47 +0100 Subject: [PATCH] Optimization of API calls within DeviceSession - replacing all API calls with new method "API" to cover APIRequestExceptions - adding IDeviceSession interface for future use - adding "API" method without result --- TelegramBotBase/Interfaces/IDeviceSession.cs | 42 +++++++++ TelegramBotBase/Sessions/DeviceSession.cs | 92 +++++++++----------- 2 files changed, 84 insertions(+), 50 deletions(-) create mode 100644 TelegramBotBase/Interfaces/IDeviceSession.cs diff --git a/TelegramBotBase/Interfaces/IDeviceSession.cs b/TelegramBotBase/Interfaces/IDeviceSession.cs new file mode 100644 index 0000000..de487b6 --- /dev/null +++ b/TelegramBotBase/Interfaces/IDeviceSession.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Text; +using TelegramBotBase.Form; + +namespace TelegramBotBase.Interfaces +{ + interface IDeviceSession + { + /// + /// Device or chat id + /// + long DeviceId { get; set; } + + /// + /// Username of user or group + /// + String ChatTitle { get; set; } + + + /// + /// When did any last action happend (message received or button clicked) + /// + DateTime LastAction { get; set; } + + /// + /// Returns the form where the user/group is at the moment. + /// + FormBase ActiveForm { get; set; } + + /// + /// Returns the previous shown form + /// + FormBase PreviousForm { get; set; } + + /// + /// contains if the form has been switched (navigated) + /// + bool FormSwitched { get; set; } + + } +} diff --git a/TelegramBotBase/Sessions/DeviceSession.cs b/TelegramBotBase/Sessions/DeviceSession.cs index bd08ab3..bbfd618 100644 --- a/TelegramBotBase/Sessions/DeviceSession.cs +++ b/TelegramBotBase/Sessions/DeviceSession.cs @@ -16,6 +16,7 @@ using TelegramBotBase.Args; using TelegramBotBase.Base; using TelegramBotBase.Exceptions; using TelegramBotBase.Form; +using TelegramBotBase.Interfaces; using TelegramBotBase.Markdown; namespace TelegramBotBase.Sessions @@ -23,7 +24,7 @@ namespace TelegramBotBase.Sessions /// /// Base class for a device/chat session /// - public class DeviceSession + public class DeviceSession : IDeviceSession { /// /// Device or chat id @@ -154,7 +155,7 @@ namespace TelegramBotBase.Sessions try { - return await this.Client.TelegramClient.EditMessageTextAsync(this.DeviceId, messageId, text, parseMode, replyMarkup: markup); + return await API(a => a.EditMessageTextAsync(this.DeviceId, messageId, text, parseMode, replyMarkup: markup)); } catch { @@ -181,7 +182,7 @@ namespace TelegramBotBase.Sessions try { - return await this.Client.TelegramClient.EditMessageTextAsync(this.DeviceId, messageId, text, parseMode, replyMarkup: markup); + return await API(a => a.EditMessageTextAsync(this.DeviceId, messageId, text, parseMode, replyMarkup: markup)); } catch { @@ -210,7 +211,7 @@ namespace TelegramBotBase.Sessions try { - return await this.Client.TelegramClient.EditMessageTextAsync(this.DeviceId, message.MessageId, message.Text, parseMode, replyMarkup: markup); + return await API(a => a.EditMessageTextAsync(this.DeviceId, message.MessageId, message.Text, parseMode, replyMarkup: markup)); } catch { @@ -232,7 +233,7 @@ namespace TelegramBotBase.Sessions try { - return await this.Client.TelegramClient.EditMessageReplyMarkupAsync(this.DeviceId, messageId, bf); + return await API(a => a.EditMessageReplyMarkupAsync(this.DeviceId, messageId, bf)); } catch { @@ -271,15 +272,7 @@ namespace TelegramBotBase.Sessions try { - m = await (this.Client.TelegramClient.SendTextMessageAsync(deviceId, text, parseMode, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification)); - - OnMessageSent(new MessageSentEventArgs(m)); - } - catch (ApiRequestException ex) - { - await Task.Delay(ex.Parameters.RetryAfter); - - m = await (this.Client.TelegramClient.SendTextMessageAsync(deviceId, text, parseMode, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification)); + m = await API(a => a.SendTextMessageAsync(deviceId, text, parseMode, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification)); OnMessageSent(new MessageSentEventArgs(m)); } @@ -331,14 +324,10 @@ namespace TelegramBotBase.Sessions try { - m = await (this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, text, parseMode, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification)); + m = await API(a => a.SendTextMessageAsync(this.DeviceId, text, parseMode, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification)); OnMessageSent(new MessageSentEventArgs(m)); } - catch (ApiRequestException) - { - return null; - } catch { return null; @@ -374,14 +363,10 @@ namespace TelegramBotBase.Sessions try { - m = await (this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, text, parseMode, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification)); + m = await API(a => a.SendTextMessageAsync(this.DeviceId, text, parseMode, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification)); OnMessageSent(new MessageSentEventArgs(m)); } - catch (ApiRequestException) - { - return null; - } catch { return null; @@ -409,14 +394,10 @@ namespace TelegramBotBase.Sessions try { - m = await this.Client.TelegramClient.SendPhotoAsync(this.DeviceId, file, caption: caption, parseMode: parseMode, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification); + m = await API(a => a.SendPhotoAsync(this.DeviceId, file, caption: caption, parseMode: parseMode, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification)); OnMessageSent(new MessageSentEventArgs(m)); } - catch (ApiRequestException) - { - return null; - } catch { return null; @@ -482,14 +463,10 @@ namespace TelegramBotBase.Sessions try { - m = await this.Client.TelegramClient.SendVideoAsync(this.DeviceId, file, caption: caption, parseMode: parseMode, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification); + m = await API(a => a.SendVideoAsync(this.DeviceId, file, caption: caption, parseMode: parseMode, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification)); OnMessageSent(new MessageSentEventArgs(m)); } - catch (ApiRequestException) - { - return null; - } catch { return null; @@ -517,14 +494,10 @@ namespace TelegramBotBase.Sessions try { - m = await this.Client.TelegramClient.SendVideoAsync(this.DeviceId, new InputOnlineFile(url), parseMode: parseMode, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification); + m = await API(a => a.SendVideoAsync(this.DeviceId, new InputOnlineFile(url), parseMode: parseMode, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification)); OnMessageSent(new MessageSentEventArgs(m)); } - catch (ApiRequestException) - { - return null; - } catch { return null; @@ -595,7 +568,7 @@ namespace TelegramBotBase.Sessions markup = buttons; } - var m = await this.Client.TelegramClient.SendDocumentAsync(this.DeviceId, document, caption, replyMarkup: markup, disableNotification: disableNotification, replyToMessageId: replyTo); + var m = await API(a => a.SendDocumentAsync(this.DeviceId, document, caption, replyMarkup: markup, disableNotification: disableNotification, replyToMessageId: replyTo)); OnMessageSent(new MessageSentEventArgs(m)); @@ -609,7 +582,7 @@ namespace TelegramBotBase.Sessions /// public async Task SetAction(ChatAction action) { - await this.Client.TelegramClient.SendChatActionAsync(this.DeviceId, action); + await API(a => a.SendChatActionAsync(this.DeviceId, action)); } /// @@ -623,7 +596,7 @@ namespace TelegramBotBase.Sessions { var rck = new ReplyKeyboardMarkup(KeyboardButton.WithRequestContact(buttonText)); rck.OneTimeKeyboard = OneTimeOnly; - return await this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, requestMessage, replyMarkup: rck); + return await API(a => a.SendTextMessageAsync(this.DeviceId, requestMessage, replyMarkup: rck)); } /// @@ -637,7 +610,7 @@ namespace TelegramBotBase.Sessions { var rcl = new ReplyKeyboardMarkup(KeyboardButton.WithRequestLocation(buttonText)); rcl.OneTimeKeyboard = OneTimeOnly; - return await this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, requestMessage, replyMarkup: rcl); + return await API(a => a.SendTextMessageAsync(this.DeviceId, requestMessage, replyMarkup: rcl)); } public async Task HideReplyKeyboard(String closedMsg = "Closed", bool autoDeleteResponse = true) @@ -669,7 +642,7 @@ namespace TelegramBotBase.Sessions { try { - await this.Client.TelegramClient.DeleteMessageAsync(this.DeviceId, messageId); + await API(a => a.DeleteMessageAsync(this.DeviceId, messageId)); return true; } @@ -696,7 +669,7 @@ namespace TelegramBotBase.Sessions { try { - await this.Client.TelegramClient.SetChatPermissionsAsync(this.DeviceId, permissions); + await API(a => a.SetChatPermissionsAsync(this.DeviceId, permissions)); } catch { @@ -710,7 +683,7 @@ namespace TelegramBotBase.Sessions { try { - await this.Client.TelegramClient.RestrictChatMemberAsync(this.DeviceId, userId, permissions, until); + await API(a => a.RestrictChatMemberAsync(this.DeviceId, userId, permissions, until)); } catch { @@ -722,7 +695,7 @@ namespace TelegramBotBase.Sessions { try { - return await this.Client.TelegramClient.GetChatMemberAsync(this.DeviceId, userId); + return await API(a => a.GetChatMemberAsync(this.DeviceId, userId)); } catch { @@ -735,7 +708,7 @@ namespace TelegramBotBase.Sessions { try { - await this.Client.TelegramClient.KickChatMemberAsync(this.DeviceId, userId, until); + await API(a => a.KickChatMemberAsync(this.DeviceId, userId, until)); } catch { @@ -747,7 +720,7 @@ namespace TelegramBotBase.Sessions { try { - await this.Client.TelegramClient.UnbanChatMemberAsync(this.DeviceId, userId); + await API(a => a.UnbanChatMemberAsync(this.DeviceId, userId)); } catch { @@ -780,7 +753,7 @@ namespace TelegramBotBase.Sessions { return await call(this.Client.TelegramClient); } - catch(ApiRequestException ex) + catch (ApiRequestException ex) { await Task.Delay(ex.Parameters.RetryAfter); @@ -788,6 +761,25 @@ namespace TelegramBotBase.Sessions } } + /// + /// This will call a function on the TelegramClient and automatically Retry if an limit has been exceeded. + /// + /// + /// + /// + public async Task API(Func call) + { + try + { + await call(this.Client.TelegramClient); + } + catch (ApiRequestException ex) + { + await Task.Delay(ex.Parameters.RetryAfter); + + await call(this.Client.TelegramClient); + } + } #region "Events"