diff --git a/TelegramBotBase/BotBase.cs b/TelegramBotBase/BotBase.cs index 74f2e52..9d9c354 100644 --- a/TelegramBotBase/BotBase.cs +++ b/TelegramBotBase/BotBase.cs @@ -72,6 +72,7 @@ namespace TelegramBotBase { this.SystemSettings = new Dictionary(); + SetSetting(eSettings.MaxNumberOfRetries, 5); SetSetting(eSettings.NavigationMaximum, 10); SetSetting(eSettings.LogAllMessages, false); SetSetting(eSettings.SkipAllMessages, false); @@ -177,6 +178,8 @@ namespace TelegramBotBase }); } + DeviceSession.MaxNumberOfRetries = this.GetSetting(eSettings.MaxNumberOfRetries, 5); + this.Client.TelegramClient.StartReceiving(); } diff --git a/TelegramBotBase/Enums/eSettings.cs b/TelegramBotBase/Enums/eSettings.cs index e425650..7e55710 100644 --- a/TelegramBotBase/Enums/eSettings.cs +++ b/TelegramBotBase/Enums/eSettings.cs @@ -27,9 +27,14 @@ namespace TelegramBotBase.Enums /// /// Does stick to the console event handler and saves all sessions on exit. /// - SaveSessionsOnConsoleExit = 4 + SaveSessionsOnConsoleExit = 4, + /// + /// Indicates the maximum number of times a request that received error + /// 429 will be sent again after a timeout until it receives code 200 or an error code not equal to 429. + /// + MaxNumberOfRetries = 5, } } diff --git a/TelegramBotBase/Sessions/DeviceSession.cs b/TelegramBotBase/Sessions/DeviceSession.cs index 7ce946f..7ce93c1 100644 --- a/TelegramBotBase/Sessions/DeviceSession.cs +++ b/TelegramBotBase/Sessions/DeviceSession.cs @@ -770,20 +770,24 @@ namespace TelegramBotBase.Sessions /// public async Task API(Func> call) { - try + var numberOfTries = 0; + while (numberOfTries < DeviceSession.MaxNumberOfRetries) { - return await call(this.Client.TelegramClient); - } - catch (ApiRequestException ex) - { - if (ex.Parameters != null) + try { - await Task.Delay(ex.Parameters.RetryAfter); + return await call(Client.TelegramClient); + } + catch (ApiRequestException ex) + { + if (ex.ErrorCode != 429) + throw; - return await call(this.Client.TelegramClient); + if (ex.Parameters != null) + await Task.Delay(ex.Parameters.RetryAfter * 1000); + + numberOfTries++; } } - return default(T); } @@ -794,17 +798,23 @@ namespace TelegramBotBase.Sessions /// public async Task API(Func call) { - try + var numberOfTries = 0; + while (numberOfTries < DeviceSession.MaxNumberOfRetries) { - await call(this.Client.TelegramClient); - } - catch (ApiRequestException ex) - { - if (ex.Parameters != null) + try { - await Task.Delay(ex.Parameters.RetryAfter); + await call(Client.TelegramClient); + return; + } + catch (ApiRequestException ex) + { + if (ex.ErrorCode != 429) + throw; - await call(this.Client.TelegramClient); + if (ex.Parameters != null) + await Task.Delay(ex.Parameters.RetryAfter * 1000); + + numberOfTries++; } } } @@ -876,5 +886,15 @@ namespace TelegramBotBase.Sessions } #endregion + + #region "Static" + + /// + /// Indicates the maximum number of times a request that received error + /// 429 will be sent again after a timeout until it receives code 200 or an error code not equal to 429. + /// + public static uint MaxNumberOfRetries { get; set; } + + #endregion "Static" } }