Skip to content

Commit 2db8e2c

Browse files
authored
Merge pull request #63 from MajMcCloud/development
Integrating latest developments into master branch
2 parents 31d0d98 + 6b7f2ca commit 2db8e2c

File tree

11 files changed

+228
-50
lines changed

11 files changed

+228
-50
lines changed

Examples/DependencyInjection/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ static async Task Main(string[] args)
2525
.NoCommands()
2626
.NoSerialization()
2727
.DefaultLanguage()
28+
.UseSingleThread()
2829
.Build();
2930

3031
await bot.Start();

Examples/EFCoreBot/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
.NoCommands()
1919
.NoSerialization()
2020
.DefaultLanguage()
21+
.UseSingleThread()
2122
.Build();
2223

2324
await bot.Start();

Examples/InlineAndReplyCombination/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ static async Task Main(string[] args)
2121
.DefaultCommands()
2222
.UseJSON(Path.Combine(Directory.GetCurrentDirectory(), "states.json"))
2323
.UseEnglish()
24+
.UseSingleThread()
2425
.Build();
2526

2627

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,21 @@ Nuget package: [https://www.nuget.org/packages/TelegramBotBase.Extensions.IronSo
10741074

10751075
---
10761076

1077+
Project: [open source](TelegramBotBase.Extensions.Images/)
1078+
1079+
### TelegramBotBase.Extensions.Images.IronSoftware
1080+
1081+
Extends the base package with some additional image methods like SendPhoto (using Bitmap)
1082+
1083+
Important: This extension uses the IronSoftware drawing library.
1084+
1085+
[![NuGet version (TelegramBotBase)](https://img.shields.io/nuget/v/TelegramBotBase.Extensions.Images.IronSoftware.svg?style=flat-square)](https://www.nuget.org/packages/TelegramBotBase.Extensions.Images.IronSoftware/)
1086+
[![Downloads](https://img.shields.io/nuget/dt/TelegramBotBase.Extensions.Images.IronSoftware.svg?style=flat-square&label=Package%20Downloads)](https://www.nuget.org/packages/TelegramBotBase.Extensions.Images.IronSoftware)
1087+
1088+
[https://www.nuget.org/packages/TelegramBotBase.Extensions.Images.IronSoftware/](https://www.nuget.org/packages/TelegramBotBase.Extensions.Images.IronSoftware/)
1089+
1090+
Project: [open source](TelegramBotBase.Extensions.Images.IronSoftware/)
1091+
10771092
### TelegramBotBase.Extensions.Serializer.Database.MSSQL
10781093

10791094
A session serializer for Microsoft SQL Server.
@@ -1102,6 +1117,8 @@ Credits: [@Kataane](https://github.com/Kataane)
11021117

11031118
---
11041119

1120+
Project: [open source](TelegramBotBase.Extensions.Serializer.Database.MSSQL/)
1121+
11051122
## Test Project
11061123

11071124
There is a "TelegramBotBase.Test" project inside the repository which includes minimal examples for all controls available.

TelegramBotBase.Test/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ private static async Task Main(string[] args)
3232
})
3333
.NoSerialization()
3434
.UseEnglish()
35+
.UseThreadPool()
3536
.Build();
3637

3738

TelegramBotBase.Test/TelegramBotBase.Example.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<ProjectReference Include="..\TelegramBotBase.Extensions.Images\TelegramBotBase.Extensions.Images.csproj" />
11+
<PackageReference Include="TelegramBotBase.Extensions.Images" Version="1.1.2" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
1215
<ProjectReference Include="..\TelegramBotBase\TelegramBotBase.csproj" />
1316
</ItemGroup>
1417

TelegramBotBase/Base/MessageClient.cs

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,34 @@
33
using System.ComponentModel;
44
using System.Net;
55
using System.Net.Http;
6+
using System.Runtime.InteropServices;
67
using System.Threading;
78
using System.Threading.Tasks;
89
using Telegram.Bot;
910
using Telegram.Bot.Exceptions;
1011
using Telegram.Bot.Polling;
1112
using Telegram.Bot.Types;
1213

14+
1315
namespace TelegramBotBase.Base;
1416

1517
/// <summary>
1618
/// Base class for message handling
1719
/// </summary>
1820
public class MessageClient
1921
{
22+
private EventHandlerList Events { get; } = new();
23+
2024
private static readonly object EvOnMessageLoop = new();
2125
private static readonly object EvOnReceiveError = new();
2226

23-
private static object __evOnMessage = new();
27+
protected CancellationTokenSource _cancellationTokenSource;
28+
29+
public string ApiKey { get; }
2430

25-
private static object __evOnMessageEdit = new();
31+
public ITelegramBotClient TelegramClient { get; set; }
2632

27-
private static object __evCallbackQuery = new();
2833

29-
private CancellationTokenSource _cancellationTokenSource;
3034

3135
/// <summary>
3236
/// Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before
@@ -41,16 +45,12 @@ public MessageClient(string apiKey)
4145
{
4246
ApiKey = apiKey;
4347
TelegramClient = new TelegramBotClient(apiKey);
44-
45-
Prepare();
4648
}
4749

4850
public MessageClient(string apiKey, HttpClient proxy)
4951
{
5052
ApiKey = apiKey;
5153
TelegramClient = new TelegramBotClient(apiKey, proxy);
52-
53-
Prepare();
5454
}
5555

5656

@@ -68,8 +68,6 @@ public MessageClient(string apiKey, Uri proxyUrl, NetworkCredential credential =
6868
);
6969

7070
TelegramClient = new TelegramBotClient(apiKey, httpClient);
71-
72-
Prepare();
7371
}
7472

7573
/// <summary>
@@ -89,62 +87,48 @@ public MessageClient(string apiKey, string proxyHost, int proxyPort)
8987
);
9088

9189
TelegramClient = new TelegramBotClient(apiKey, httpClient);
92-
93-
Prepare();
9490
}
9591

9692

9793
public MessageClient(string apiKey, TelegramBotClient client)
9894
{
9995
ApiKey = apiKey;
10096
TelegramClient = client;
101-
102-
Prepare();
103-
}
104-
105-
106-
public string ApiKey { get; }
107-
108-
public ITelegramBotClient TelegramClient { get; set; }
109-
110-
private EventHandlerList Events { get; } = new();
111-
112-
113-
public void Prepare()
114-
{
115-
TelegramClient.Timeout = new TimeSpan(0, 0, 30);
11697
}
11798

11899

119-
public void StartReceiving()
100+
public virtual void StartReceiving()
120101
{
121102
_cancellationTokenSource = new CancellationTokenSource();
122103

123104
var receiverOptions = new ReceiverOptions();
124105

125106
receiverOptions.ThrowPendingUpdates = ThrowPendingUpdates;
126107

127-
TelegramClient.StartReceiving(HandleUpdateAsync, HandleErrorAsync, receiverOptions,
128-
_cancellationTokenSource.Token);
108+
TelegramClient.StartReceiving(HandleUpdateAsync, HandleErrorAsync, receiverOptions, _cancellationTokenSource.Token);
129109
}
130110

131-
public void StopReceiving()
111+
112+
public virtual void StopReceiving()
132113
{
133114
_cancellationTokenSource.Cancel();
134115
}
135116

136117

137-
public async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
118+
private async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
138119
{
139120
await OnMessageLoop(new UpdateResult(update, null));
140121
}
141122

142-
public async Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception,
123+
124+
private async Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception,
143125
CancellationToken cancellationToken)
144126
{
145127
await OnReceiveError(new ErrorResult(exception));
146128
}
147129

130+
131+
#region "BotCommands"
148132
/// <summary>
149133
/// This will return the current list of bot commands.
150134
/// </summary>
@@ -176,6 +160,8 @@ public async Task DeleteBotCommands(BotCommandScope scope = null, string languag
176160
await TelegramClient.DeleteMyCommandsAsync(scope, languageCode);
177161
}
178162

163+
#endregion
164+
179165

180166
#region "Events"
181167

@@ -185,6 +171,7 @@ public event Async.AsyncEventHandler<UpdateResult> MessageLoop
185171
remove => Events.RemoveHandler(EvOnMessageLoop, value);
186172
}
187173

174+
188175
public async Task OnMessageLoop(UpdateResult update)
189176
{
190177
var eventHandlers = (Events[EvOnMessageLoop] as Async.AsyncEventHandler<UpdateResult>)?.Invoke(this, update);
@@ -202,6 +189,7 @@ public event Async.AsyncEventHandler<ErrorResult> ReceiveError
202189
remove => Events.RemoveHandler(EvOnReceiveError, value);
203190
}
204191

192+
205193
public async Task OnReceiveError(ErrorResult update)
206194
{
207195
var eventHandlers = (Events[EvOnReceiveError] as Async.AsyncEventHandler<ErrorResult>)?.Invoke(this, update);
@@ -225,4 +213,5 @@ public async Task OnReceiveError(ErrorResult update)
225213
}
226214

227215
#endregion
216+
228217
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
using System.Net;
5+
using System.Net.Http;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Telegram.Bot;
9+
using Telegram.Bot.Exceptions;
10+
using Telegram.Bot.Polling;
11+
using Telegram.Bot.Types;
12+
using TelegramBotBase.Interfaces;
13+
14+
namespace TelegramBotBase.Base;
15+
16+
/// <summary>
17+
/// Base class for message handling
18+
/// </summary>
19+
public class ThreadPoolMessageClient : MessageClient
20+
{
21+
22+
/// <summary>
23+
/// Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before
24+
// start polling. If set to true Telegram.Bot.Polling.ReceiverOptions.AllowedUpdates
25+
// should be set to not null, otherwise Telegram.Bot.Polling.ReceiverOptions.AllowedUpdates
26+
// will effectively be set to receive all Telegram.Bot.Types.Updates.
27+
/// </summary>
28+
29+
public int ThreadPool_WorkerThreads { get; set; } = 1;
30+
31+
public int ThreadPool_IOThreads { get; set; } = 1;
32+
33+
34+
public ThreadPoolMessageClient(string apiKey) : base(apiKey)
35+
{
36+
37+
}
38+
39+
public ThreadPoolMessageClient(string apiKey, HttpClient proxy) : base(apiKey, proxy)
40+
{
41+
42+
}
43+
44+
45+
public ThreadPoolMessageClient(string apiKey, Uri proxyUrl, NetworkCredential credential = null) : base(apiKey, proxyUrl, credential)
46+
{
47+
48+
}
49+
50+
/// <summary>
51+
/// Initializes the client with a proxy
52+
/// </summary>
53+
/// <param name="apiKey"></param>
54+
/// <param name="proxyHost">i.e. 127.0.0.1</param>
55+
/// <param name="proxyPort">i.e. 10000</param>
56+
public ThreadPoolMessageClient(string apiKey, string proxyHost, int proxyPort) : base(apiKey, proxyHost, proxyPort)
57+
{
58+
59+
}
60+
61+
62+
public ThreadPoolMessageClient(string apiKey, TelegramBotClient client) : base(apiKey, client)
63+
{
64+
65+
}
66+
67+
68+
69+
public override void StartReceiving()
70+
{
71+
_cancellationTokenSource = new CancellationTokenSource();
72+
73+
var receiverOptions = new ReceiverOptions();
74+
75+
receiverOptions.ThrowPendingUpdates = ThrowPendingUpdates;
76+
77+
ThreadPool.SetMaxThreads(ThreadPool_WorkerThreads, ThreadPool_IOThreads);
78+
79+
TelegramClient.StartReceiving(HandleUpdateAsyncThreadPool, HandleErrorAsyncThreadPool, receiverOptions, _cancellationTokenSource.Token);
80+
}
81+
82+
public override void StopReceiving()
83+
{
84+
_cancellationTokenSource.Cancel();
85+
}
86+
87+
88+
public Task HandleUpdateAsyncThreadPool(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
89+
{
90+
ThreadPool.QueueUserWorkItem(async a =>
91+
{
92+
await OnMessageLoop(new UpdateResult(update, null));
93+
});
94+
95+
return Task.CompletedTask;
96+
}
97+
98+
public Task HandleErrorAsyncThreadPool(ITelegramBotClient botClient, Exception exception,
99+
CancellationToken cancellationToken)
100+
{
101+
ThreadPool.QueueUserWorkItem(async a =>
102+
{
103+
await OnReceiveError(new ErrorResult(exception));
104+
});
105+
106+
return Task.CompletedTask;
107+
}
108+
109+
}

0 commit comments

Comments
 (0)