Skip to content

Commit c1018ac

Browse files
committed
Adding ThrowPendingUpdates option to builder and Message client.
1 parent 064399e commit c1018ac

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

TelegramBotBase/Base/MessageClient.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ public class MessageClient
2828

2929
private CancellationTokenSource _cancellationTokenSource;
3030

31+
/// <summary>
32+
/// Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before
33+
// start polling. If set to true Telegram.Bot.Polling.ReceiverOptions.AllowedUpdates
34+
// should be set to not null, otherwise Telegram.Bot.Polling.ReceiverOptions.AllowedUpdates
35+
// will effectively be set to receive all Telegram.Bot.Types.Updates.
36+
/// </summary>
37+
public bool ThrowPendingUpdates { get; set; }
38+
3139

3240
public MessageClient(string apiKey)
3341
{
@@ -114,6 +122,8 @@ public void StartReceiving()
114122

115123
var receiverOptions = new ReceiverOptions();
116124

125+
receiverOptions.ThrowPendingUpdates = ThrowPendingUpdates;
126+
117127
TelegramClient.StartReceiving(HandleUpdateAsync, HandleErrorAsync, receiverOptions,
118128
_cancellationTokenSource.Token);
119129
}

TelegramBotBase/Builder/BotBaseBuilder.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public INetworkingSelectionStage WithStartFormFactory(IStartFormFactory factory)
207207

208208
#region "Step 4 (Network Settings)"
209209

210-
public IBotCommandsStage WithProxy(string proxyAddress)
210+
public IBotCommandsStage WithProxy(string proxyAddress, bool throwPendingUpdates = false)
211211
{
212212
var url = new Uri(proxyAddress);
213213
_client = new MessageClient(_apiKey, url)
@@ -217,11 +217,12 @@ public IBotCommandsStage WithProxy(string proxyAddress)
217217
Timeout = new TimeSpan(0, 1, 0)
218218
},
219219
};
220+
_client.ThrowPendingUpdates = throwPendingUpdates;
220221
return this;
221222
}
222223

223224

224-
public IBotCommandsStage NoProxy()
225+
public IBotCommandsStage NoProxy(bool throwPendingUpdates = false)
225226
{
226227
_client = new MessageClient(_apiKey)
227228
{
@@ -230,11 +231,12 @@ public IBotCommandsStage NoProxy()
230231
Timeout = new TimeSpan(0, 1, 0)
231232
}
232233
};
234+
_client.ThrowPendingUpdates = throwPendingUpdates;
233235
return this;
234236
}
235237

236238

237-
public IBotCommandsStage WithBotClient(TelegramBotClient tgclient)
239+
public IBotCommandsStage WithBotClient(TelegramBotClient tgclient, bool throwPendingUpdates = false)
238240
{
239241
_client = new MessageClient(_apiKey, tgclient)
240242
{
@@ -243,11 +245,12 @@ public IBotCommandsStage WithBotClient(TelegramBotClient tgclient)
243245
Timeout = new TimeSpan(0, 1, 0)
244246
}
245247
};
248+
_client.ThrowPendingUpdates = throwPendingUpdates;
246249
return this;
247250
}
248251

249252

250-
public IBotCommandsStage WithHostAndPort(string proxyHost, int proxyPort)
253+
public IBotCommandsStage WithHostAndPort(string proxyHost, int proxyPort, bool throwPendingUpdates = false)
251254
{
252255
_client = new MessageClient(_apiKey, proxyHost, proxyPort)
253256
{
@@ -256,10 +259,11 @@ public IBotCommandsStage WithHostAndPort(string proxyHost, int proxyPort)
256259
Timeout = new TimeSpan(0, 1, 0)
257260
}
258261
};
262+
_client.ThrowPendingUpdates = throwPendingUpdates;
259263
return this;
260264
}
261265

262-
public IBotCommandsStage WithHttpClient(HttpClient tgclient)
266+
public IBotCommandsStage WithHttpClient(HttpClient tgclient, bool throwPendingUpdates = false)
263267
{
264268
_client = new MessageClient(_apiKey, tgclient)
265269
{
@@ -268,6 +272,7 @@ public IBotCommandsStage WithHttpClient(HttpClient tgclient)
268272
Timeout = new TimeSpan(0, 1, 0)
269273
}
270274
};
275+
_client.ThrowPendingUpdates = throwPendingUpdates;
271276
return this;
272277
}
273278

TelegramBotBase/Builder/Interfaces/INetworkingSelectionStage.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,41 @@ public interface INetworkingSelectionStage
99
/// Chooses a proxy as network configuration.
1010
/// </summary>
1111
/// <param name="proxyAddress"></param>
12+
/// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param>
1213
/// <returns></returns>
13-
IBotCommandsStage WithProxy(string proxyAddress);
14+
IBotCommandsStage WithProxy(string proxyAddress, bool throwPendingUpdates = false);
1415

1516
/// <summary>
1617
/// Do not choose a proxy as network configuration.
1718
/// </summary>
19+
/// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param>
1820
/// <returns></returns>
19-
IBotCommandsStage NoProxy();
21+
IBotCommandsStage NoProxy(bool throwPendingUpdates = false);
2022

2123

2224
/// <summary>
2325
/// Chooses a custom instance of TelegramBotClient.
2426
/// </summary>
2527
/// <param name="client"></param>
28+
/// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param>
2629
/// <returns></returns>
27-
IBotCommandsStage WithBotClient(TelegramBotClient client);
30+
IBotCommandsStage WithBotClient(TelegramBotClient client, bool throwPendingUpdates = false);
2831

2932

3033
/// <summary>
3134
/// Sets the custom proxy host and port.
3235
/// </summary>
3336
/// <param name="proxyHost"></param>
3437
/// <param name="Port"></param>
38+
/// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param>
3539
/// <returns></returns>
36-
IBotCommandsStage WithHostAndPort(string proxyHost, int Port);
40+
IBotCommandsStage WithHostAndPort(string proxyHost, int Port, bool throwPendingUpdates = false);
3741

3842
/// <summary>
3943
/// Uses a custom http client.
4044
/// </summary>
4145
/// <param name="client"></param>
46+
/// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param>
4247
/// <returns></returns>
43-
IBotCommandsStage WithHttpClient(HttpClient client);
48+
IBotCommandsStage WithHttpClient(HttpClient client, bool throwPendingUpdates = false);
4449
}

0 commit comments

Comments
 (0)