From c1018ac5a0079d8f116966907b04b2deb8c1f6d1 Mon Sep 17 00:00:00 2001 From: Florian Zevedei Date: Sat, 9 Dec 2023 14:56:02 +0100 Subject: [PATCH] Adding ThrowPendingUpdates option to builder and Message client. --- TelegramBotBase/Base/MessageClient.cs | 10 ++++++++++ TelegramBotBase/Builder/BotBaseBuilder.cs | 15 ++++++++++----- .../Interfaces/INetworkingSelectionStage.cs | 15 ++++++++++----- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/TelegramBotBase/Base/MessageClient.cs b/TelegramBotBase/Base/MessageClient.cs index 17ec7f3..10a724e 100644 --- a/TelegramBotBase/Base/MessageClient.cs +++ b/TelegramBotBase/Base/MessageClient.cs @@ -28,6 +28,14 @@ public class MessageClient private CancellationTokenSource _cancellationTokenSource; + /// + /// Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before + // start polling. If set to true Telegram.Bot.Polling.ReceiverOptions.AllowedUpdates + // should be set to not null, otherwise Telegram.Bot.Polling.ReceiverOptions.AllowedUpdates + // will effectively be set to receive all Telegram.Bot.Types.Updates. + /// + public bool ThrowPendingUpdates { get; set; } + public MessageClient(string apiKey) { @@ -114,6 +122,8 @@ public class MessageClient var receiverOptions = new ReceiverOptions(); + receiverOptions.ThrowPendingUpdates = ThrowPendingUpdates; + TelegramClient.StartReceiving(HandleUpdateAsync, HandleErrorAsync, receiverOptions, _cancellationTokenSource.Token); } diff --git a/TelegramBotBase/Builder/BotBaseBuilder.cs b/TelegramBotBase/Builder/BotBaseBuilder.cs index 497bed0..c3d9394 100644 --- a/TelegramBotBase/Builder/BotBaseBuilder.cs +++ b/TelegramBotBase/Builder/BotBaseBuilder.cs @@ -207,7 +207,7 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage, #region "Step 4 (Network Settings)" - public IBotCommandsStage WithProxy(string proxyAddress) + public IBotCommandsStage WithProxy(string proxyAddress, bool throwPendingUpdates = false) { var url = new Uri(proxyAddress); _client = new MessageClient(_apiKey, url) @@ -217,11 +217,12 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage, Timeout = new TimeSpan(0, 1, 0) }, }; + _client.ThrowPendingUpdates = throwPendingUpdates; return this; } - public IBotCommandsStage NoProxy() + public IBotCommandsStage NoProxy(bool throwPendingUpdates = false) { _client = new MessageClient(_apiKey) { @@ -230,11 +231,12 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage, Timeout = new TimeSpan(0, 1, 0) } }; + _client.ThrowPendingUpdates = throwPendingUpdates; return this; } - public IBotCommandsStage WithBotClient(TelegramBotClient tgclient) + public IBotCommandsStage WithBotClient(TelegramBotClient tgclient, bool throwPendingUpdates = false) { _client = new MessageClient(_apiKey, tgclient) { @@ -243,11 +245,12 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage, Timeout = new TimeSpan(0, 1, 0) } }; + _client.ThrowPendingUpdates = throwPendingUpdates; return this; } - public IBotCommandsStage WithHostAndPort(string proxyHost, int proxyPort) + public IBotCommandsStage WithHostAndPort(string proxyHost, int proxyPort, bool throwPendingUpdates = false) { _client = new MessageClient(_apiKey, proxyHost, proxyPort) { @@ -256,10 +259,11 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage, Timeout = new TimeSpan(0, 1, 0) } }; + _client.ThrowPendingUpdates = throwPendingUpdates; return this; } - public IBotCommandsStage WithHttpClient(HttpClient tgclient) + public IBotCommandsStage WithHttpClient(HttpClient tgclient, bool throwPendingUpdates = false) { _client = new MessageClient(_apiKey, tgclient) { @@ -268,6 +272,7 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage, Timeout = new TimeSpan(0, 1, 0) } }; + _client.ThrowPendingUpdates = throwPendingUpdates; return this; } diff --git a/TelegramBotBase/Builder/Interfaces/INetworkingSelectionStage.cs b/TelegramBotBase/Builder/Interfaces/INetworkingSelectionStage.cs index 3e48b21..cd17e9d 100644 --- a/TelegramBotBase/Builder/Interfaces/INetworkingSelectionStage.cs +++ b/TelegramBotBase/Builder/Interfaces/INetworkingSelectionStage.cs @@ -9,22 +9,25 @@ public interface INetworkingSelectionStage /// Chooses a proxy as network configuration. /// /// + /// Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling. /// - IBotCommandsStage WithProxy(string proxyAddress); + IBotCommandsStage WithProxy(string proxyAddress, bool throwPendingUpdates = false); /// /// Do not choose a proxy as network configuration. /// + /// Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling. /// - IBotCommandsStage NoProxy(); + IBotCommandsStage NoProxy(bool throwPendingUpdates = false); /// /// Chooses a custom instance of TelegramBotClient. /// /// + /// Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling. /// - IBotCommandsStage WithBotClient(TelegramBotClient client); + IBotCommandsStage WithBotClient(TelegramBotClient client, bool throwPendingUpdates = false); /// @@ -32,13 +35,15 @@ public interface INetworkingSelectionStage /// /// /// + /// Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling. /// - IBotCommandsStage WithHostAndPort(string proxyHost, int Port); + IBotCommandsStage WithHostAndPort(string proxyHost, int Port, bool throwPendingUpdates = false); /// /// Uses a custom http client. /// /// + /// Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling. /// - IBotCommandsStage WithHttpClient(HttpClient client); + IBotCommandsStage WithHttpClient(HttpClient client, bool throwPendingUpdates = false); } \ No newline at end of file