Adding ThrowPendingUpdates option to builder and Message client.

This commit is contained in:
Florian Zevedei 2023-12-09 14:56:02 +01:00
parent 064399ed77
commit c1018ac5a0
3 changed files with 30 additions and 10 deletions

View File

@ -28,6 +28,14 @@ public class MessageClient
private CancellationTokenSource _cancellationTokenSource; private CancellationTokenSource _cancellationTokenSource;
/// <summary>
/// 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.
/// </summary>
public bool ThrowPendingUpdates { get; set; }
public MessageClient(string apiKey) public MessageClient(string apiKey)
{ {
@ -114,6 +122,8 @@ public class MessageClient
var receiverOptions = new ReceiverOptions(); var receiverOptions = new ReceiverOptions();
receiverOptions.ThrowPendingUpdates = ThrowPendingUpdates;
TelegramClient.StartReceiving(HandleUpdateAsync, HandleErrorAsync, receiverOptions, TelegramClient.StartReceiving(HandleUpdateAsync, HandleErrorAsync, receiverOptions,
_cancellationTokenSource.Token); _cancellationTokenSource.Token);
} }

View File

@ -207,7 +207,7 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage,
#region "Step 4 (Network Settings)" #region "Step 4 (Network Settings)"
public IBotCommandsStage WithProxy(string proxyAddress) public IBotCommandsStage WithProxy(string proxyAddress, bool throwPendingUpdates = false)
{ {
var url = new Uri(proxyAddress); var url = new Uri(proxyAddress);
_client = new MessageClient(_apiKey, url) _client = new MessageClient(_apiKey, url)
@ -217,11 +217,12 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage,
Timeout = new TimeSpan(0, 1, 0) Timeout = new TimeSpan(0, 1, 0)
}, },
}; };
_client.ThrowPendingUpdates = throwPendingUpdates;
return this; return this;
} }
public IBotCommandsStage NoProxy() public IBotCommandsStage NoProxy(bool throwPendingUpdates = false)
{ {
_client = new MessageClient(_apiKey) _client = new MessageClient(_apiKey)
{ {
@ -230,11 +231,12 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage,
Timeout = new TimeSpan(0, 1, 0) Timeout = new TimeSpan(0, 1, 0)
} }
}; };
_client.ThrowPendingUpdates = throwPendingUpdates;
return this; return this;
} }
public IBotCommandsStage WithBotClient(TelegramBotClient tgclient) public IBotCommandsStage WithBotClient(TelegramBotClient tgclient, bool throwPendingUpdates = false)
{ {
_client = new MessageClient(_apiKey, tgclient) _client = new MessageClient(_apiKey, tgclient)
{ {
@ -243,11 +245,12 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage,
Timeout = new TimeSpan(0, 1, 0) Timeout = new TimeSpan(0, 1, 0)
} }
}; };
_client.ThrowPendingUpdates = throwPendingUpdates;
return this; 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) _client = new MessageClient(_apiKey, proxyHost, proxyPort)
{ {
@ -256,10 +259,11 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage,
Timeout = new TimeSpan(0, 1, 0) Timeout = new TimeSpan(0, 1, 0)
} }
}; };
_client.ThrowPendingUpdates = throwPendingUpdates;
return this; return this;
} }
public IBotCommandsStage WithHttpClient(HttpClient tgclient) public IBotCommandsStage WithHttpClient(HttpClient tgclient, bool throwPendingUpdates = false)
{ {
_client = new MessageClient(_apiKey, tgclient) _client = new MessageClient(_apiKey, tgclient)
{ {
@ -268,6 +272,7 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage,
Timeout = new TimeSpan(0, 1, 0) Timeout = new TimeSpan(0, 1, 0)
} }
}; };
_client.ThrowPendingUpdates = throwPendingUpdates;
return this; return this;
} }

View File

@ -9,22 +9,25 @@ public interface INetworkingSelectionStage
/// Chooses a proxy as network configuration. /// Chooses a proxy as network configuration.
/// </summary> /// </summary>
/// <param name="proxyAddress"></param> /// <param name="proxyAddress"></param>
/// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param>
/// <returns></returns> /// <returns></returns>
IBotCommandsStage WithProxy(string proxyAddress); IBotCommandsStage WithProxy(string proxyAddress, bool throwPendingUpdates = false);
/// <summary> /// <summary>
/// Do not choose a proxy as network configuration. /// Do not choose a proxy as network configuration.
/// </summary> /// </summary>
/// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param>
/// <returns></returns> /// <returns></returns>
IBotCommandsStage NoProxy(); IBotCommandsStage NoProxy(bool throwPendingUpdates = false);
/// <summary> /// <summary>
/// Chooses a custom instance of TelegramBotClient. /// Chooses a custom instance of TelegramBotClient.
/// </summary> /// </summary>
/// <param name="client"></param> /// <param name="client"></param>
/// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param>
/// <returns></returns> /// <returns></returns>
IBotCommandsStage WithBotClient(TelegramBotClient client); IBotCommandsStage WithBotClient(TelegramBotClient client, bool throwPendingUpdates = false);
/// <summary> /// <summary>
@ -32,13 +35,15 @@ public interface INetworkingSelectionStage
/// </summary> /// </summary>
/// <param name="proxyHost"></param> /// <param name="proxyHost"></param>
/// <param name="Port"></param> /// <param name="Port"></param>
/// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param>
/// <returns></returns> /// <returns></returns>
IBotCommandsStage WithHostAndPort(string proxyHost, int Port); IBotCommandsStage WithHostAndPort(string proxyHost, int Port, bool throwPendingUpdates = false);
/// <summary> /// <summary>
/// Uses a custom http client. /// Uses a custom http client.
/// </summary> /// </summary>
/// <param name="client"></param> /// <param name="client"></param>
/// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param>
/// <returns></returns> /// <returns></returns>
IBotCommandsStage WithHttpClient(HttpClient client); IBotCommandsStage WithHttpClient(HttpClient client, bool throwPendingUpdates = false);
} }