Skip to content

Commit d56b26a

Browse files
authored
Merge pull request #55 from MajMcCloud/development
Integrating development branch into master
2 parents 121a788 + c1018ac commit d56b26a

File tree

6 files changed

+100
-24
lines changed

6 files changed

+100
-24
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ BitTorrent: `TYVZSykaVT1nKZnz9hjDgBRNB9VavU1bpW`
5959
* [TaggedButtonGrid](#tagged-button-grid)
6060
* [CheckedButtonList](#checked-button-list)
6161
* [MultiToggleButton](#multi-toggle-button)
62+
- [Localizations](#localizations)
6263
- [Groups](#groups)
6364
* [SplitterForm](#splitter-form)
6465
* [GroupForm](#group-form)
@@ -714,6 +715,19 @@ Check the example project [TelegramBotBase.Test/Tests/Controls/CheckedButtonList
714715

715716
Check the example project [TelegramBotBase.Test/Tests/Controls/MultiToggleButtonForm.cs](TelegramBotBase.Test/Tests/Controls/MultiToggleButtonForm.cs)
716717

718+
719+
## Localizations
720+
721+
The current available languages for controls are:
722+
723+
- English
724+
- German
725+
- Persian
726+
727+
You can add other languages easily by creating a subclass of the [TelegramBotBase/Localizations/Localization.cs](TelegramBotBase/Localizations/Localization.cs) class.
728+
729+
To set the default language set the *Language* property on the static [TelegramBotBase/Localizations/Default.cs](TelegramBotBase/Localizations/Default.cs) instance.
730+
717731
## Groups
718732

719733
For groups, there are multiple different tools which help to work with and allows bot also to manage

TelegramBotBase.Extensions.Images/TelegramBotBase.Extensions.Images.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@
1515
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1616
</PackageReference>
1717
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
18-
<PackageReference Include="TelegramBotBase" Version="6.0.0" />
1918
</ItemGroup>
2019

2120
<ItemGroup>
2221
<Folder Include="Properties\" />
2322
</ItemGroup>
2423

24+
<ItemGroup>
25+
<ProjectReference Include="..\TelegramBotBase\TelegramBotBase.csproj" />
26+
</ItemGroup>
27+
2528
</Project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
3+
namespace TelegramBotBase.Base
4+
{
5+
public class ErrorResult : EventArgs
6+
{
7+
public ErrorResult(Exception exception)
8+
{
9+
Exception = exception;
10+
}
11+
12+
public Exception Exception { get; }
13+
}
14+
}

TelegramBotBase/Base/MessageClient.cs

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace TelegramBotBase.Base;
1818
public class MessageClient
1919
{
2020
private static readonly object EvOnMessageLoop = new();
21+
private static readonly object EvOnReceiveError = new();
2122

2223
private static object __evOnMessage = new();
2324

@@ -27,6 +28,14 @@ public class MessageClient
2728

2829
private CancellationTokenSource _cancellationTokenSource;
2930

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+
3039

3140
public MessageClient(string apiKey)
3241
{
@@ -113,6 +122,8 @@ public void StartReceiving()
113122

114123
var receiverOptions = new ReceiverOptions();
115124

125+
receiverOptions.ThrowPendingUpdates = ThrowPendingUpdates;
126+
116127
TelegramClient.StartReceiving(HandleUpdateAsync, HandleErrorAsync, receiverOptions,
117128
_cancellationTokenSource.Token);
118129
}
@@ -128,22 +139,12 @@ public async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update,
128139
await OnMessageLoop(new UpdateResult(update, null));
129140
}
130141

131-
public Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception,
142+
public async Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception,
132143
CancellationToken cancellationToken)
133144
{
134-
if (exception is ApiRequestException exApi)
135-
{
136-
Console.WriteLine($"Telegram API Error:\n[{exApi.ErrorCode}]\n{exApi.Message}");
137-
}
138-
else
139-
{
140-
Console.WriteLine(exception.ToString());
141-
}
142-
143-
return Task.CompletedTask;
145+
await OnReceiveError(new ErrorResult(exception));
144146
}
145147

146-
147148
/// <summary>
148149
/// This will return the current list of bot commands.
149150
/// </summary>
@@ -186,7 +187,41 @@ public event Async.AsyncEventHandler<UpdateResult> MessageLoop
186187

187188
public async Task OnMessageLoop(UpdateResult update)
188189
{
189-
await (Events[EvOnMessageLoop] as Async.AsyncEventHandler<UpdateResult>)?.Invoke(this, update);
190+
var eventHandlers = (Events[EvOnMessageLoop] as Async.AsyncEventHandler<UpdateResult>)?.Invoke(this, update);
191+
192+
if (eventHandlers != null)
193+
{
194+
await eventHandlers;
195+
}
196+
}
197+
198+
199+
public event Async.AsyncEventHandler<ErrorResult> ReceiveError
200+
{
201+
add => Events.AddHandler(EvOnReceiveError, value);
202+
remove => Events.RemoveHandler(EvOnReceiveError, value);
203+
}
204+
205+
public async Task OnReceiveError(ErrorResult update)
206+
{
207+
var eventHandlers = (Events[EvOnReceiveError] as Async.AsyncEventHandler<ErrorResult>)?.Invoke(this, update);
208+
209+
if (eventHandlers != null)
210+
{
211+
await eventHandlers;
212+
return;
213+
}
214+
215+
//Fallback when no event handler is used.
216+
if (update.Exception is ApiRequestException exApi)
217+
{
218+
Console.WriteLine($"Telegram API Error:\n[{exApi.ErrorCode}]\n{exApi.Message}");
219+
}
220+
else
221+
{
222+
Console.WriteLine(update.Exception.ToString());
223+
}
224+
190225
}
191226

192227
#endregion

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)