From 7fcaa407ad075c74db638519b5a5267c03e1477d Mon Sep 17 00:00:00 2001 From: Florian Zevedei Date: Tue, 26 Dec 2023 17:40:13 +0100 Subject: [PATCH 01/17] Adding parallel processing via ThreadPool --- TelegramBotBase.Test/Program.cs | 1 + TelegramBotBase/Base/MessageClient.cs | 60 ++++++++++++++++--- TelegramBotBase/Builder/BotBaseBuilder.cs | 42 +++++++++++-- .../Interfaces/ILanguageSelectionStage.cs | 10 ++-- .../Builder/Interfaces/IThreadingStage.cs | 24 ++++++++ 5 files changed, 119 insertions(+), 18 deletions(-) create mode 100644 TelegramBotBase/Builder/Interfaces/IThreadingStage.cs diff --git a/TelegramBotBase.Test/Program.cs b/TelegramBotBase.Test/Program.cs index 9276cbd..a9236a1 100644 --- a/TelegramBotBase.Test/Program.cs +++ b/TelegramBotBase.Test/Program.cs @@ -32,6 +32,7 @@ internal class Program }) .NoSerialization() .UseEnglish() + .UseThreadPool() .Build(); diff --git a/TelegramBotBase/Base/MessageClient.cs b/TelegramBotBase/Base/MessageClient.cs index 10a724e..5271dc4 100644 --- a/TelegramBotBase/Base/MessageClient.cs +++ b/TelegramBotBase/Base/MessageClient.cs @@ -28,6 +28,12 @@ public class MessageClient private CancellationTokenSource _cancellationTokenSource; + public string ApiKey { get; } + + public ITelegramBotClient TelegramClient { get; set; } + + private EventHandlerList Events { get; } = new(); + /// /// Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before // start polling. If set to true Telegram.Bot.Polling.ReceiverOptions.AllowedUpdates @@ -36,6 +42,12 @@ public class MessageClient /// public bool ThrowPendingUpdates { get; set; } + public bool UseThreadPool { get; set; } = false; + + public int ThreadPool_WorkerThreads { get; set; } = 1; + + public int ThreadPool_IOThreads { get; set; } = 1; + public MessageClient(string apiKey) { @@ -103,11 +115,6 @@ public class MessageClient } - public string ApiKey { get; } - - public ITelegramBotClient TelegramClient { get; set; } - - private EventHandlerList Events { get; } = new(); public void Prepare() @@ -124,8 +131,19 @@ public class MessageClient receiverOptions.ThrowPendingUpdates = ThrowPendingUpdates; - TelegramClient.StartReceiving(HandleUpdateAsync, HandleErrorAsync, receiverOptions, - _cancellationTokenSource.Token); + if (UseThreadPool) + { + ThreadPool.SetMaxThreads(ThreadPool_WorkerThreads, ThreadPool_IOThreads); + + TelegramClient.StartReceiving(HandleUpdateAsyncThreadPool, HandleErrorAsyncThreadPool, receiverOptions, + _cancellationTokenSource.Token); + } + else + { + TelegramClient.StartReceiving(HandleUpdateAsync, HandleErrorAsync, receiverOptions, + _cancellationTokenSource.Token); + } + } public void StopReceiving() @@ -133,6 +151,7 @@ public class MessageClient _cancellationTokenSource.Cancel(); } + #region "Single Thread" public async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken) { @@ -145,6 +164,33 @@ public class MessageClient await OnReceiveError(new ErrorResult(exception)); } + #endregion + + #region "Thread Pool" + + public Task HandleUpdateAsyncThreadPool(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken) + { + ThreadPool.QueueUserWorkItem(async a => + { + await OnMessageLoop(new UpdateResult(update, null)); + }); + + return Task.CompletedTask; + } + + public Task HandleErrorAsyncThreadPool(ITelegramBotClient botClient, Exception exception, + CancellationToken cancellationToken) + { + ThreadPool.QueueUserWorkItem(async a => + { + await OnReceiveError(new ErrorResult(exception)); + }); + + return Task.CompletedTask; + } + + #endregion + /// /// This will return the current list of bot commands. /// diff --git a/TelegramBotBase/Builder/BotBaseBuilder.cs b/TelegramBotBase/Builder/BotBaseBuilder.cs index c3d9394..85b5621 100644 --- a/TelegramBotBase/Builder/BotBaseBuilder.cs +++ b/TelegramBotBase/Builder/BotBaseBuilder.cs @@ -18,7 +18,7 @@ namespace TelegramBotBase.Builder; public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage, IStartFormSelectionStage, IBuildingStage, INetworkingSelectionStage, IBotCommandsStage, ISessionSerializationStage, - ILanguageSelectionStage + ILanguageSelectionStage, IThreadingStage { private string _apiKey; @@ -87,6 +87,8 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage, DefaultLanguage(); + UseSingleThread(); + return this; } @@ -107,6 +109,8 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage, DefaultLanguage(); + UseSingleThread(); + return this; } @@ -125,6 +129,8 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage, DefaultLanguage(); + UseSingleThread(); + return this; } @@ -391,34 +397,58 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage, #region "Step 7 (Language)" - public IBuildingStage DefaultLanguage() + public IThreadingStage DefaultLanguage() { return this; } - public IBuildingStage UseEnglish() + public IThreadingStage UseEnglish() { Default.Language = new English(); return this; } - public IBuildingStage UseGerman() + public IThreadingStage UseGerman() { Default.Language = new German(); return this; } - public IBuildingStage UsePersian() + public IThreadingStage UsePersian() { Default.Language = new Persian(); return this; } - public IBuildingStage Custom(Localization language) + public IThreadingStage Custom(Localization language) { Default.Language = language; return this; } + #endregion + + #region "Step 8 (Threading)" + + public IBuildingStage UseSingleThread() + { + _client.UseThreadPool = false; + + return this; + } + + public IBuildingStage UseThreadPool(int workerThreads = 2, int ioThreads = 1) + { + _client.UseThreadPool = true; + _client.ThreadPool_WorkerThreads = workerThreads; + _client.ThreadPool_IOThreads = ioThreads; + + return this; + } + + + + #endregion + } diff --git a/TelegramBotBase/Builder/Interfaces/ILanguageSelectionStage.cs b/TelegramBotBase/Builder/Interfaces/ILanguageSelectionStage.cs index 8021ee2..1a43a52 100644 --- a/TelegramBotBase/Builder/Interfaces/ILanguageSelectionStage.cs +++ b/TelegramBotBase/Builder/Interfaces/ILanguageSelectionStage.cs @@ -8,29 +8,29 @@ public interface ILanguageSelectionStage /// Selects the default language for control usage. (English) /// /// - IBuildingStage DefaultLanguage(); + IThreadingStage DefaultLanguage(); /// /// Selects english as the default language for control labels. /// /// - IBuildingStage UseEnglish(); + IThreadingStage UseEnglish(); /// /// Selects german as the default language for control labels. /// /// - IBuildingStage UseGerman(); + IThreadingStage UseGerman(); /// /// Selects persian as the default language for control labels. /// /// - IBuildingStage UsePersian(); + IThreadingStage UsePersian(); /// /// Selects a custom language as the default language for control labels. /// /// - IBuildingStage Custom(Localization language); + IThreadingStage Custom(Localization language); } \ No newline at end of file diff --git a/TelegramBotBase/Builder/Interfaces/IThreadingStage.cs b/TelegramBotBase/Builder/Interfaces/IThreadingStage.cs new file mode 100644 index 0000000..f53c087 --- /dev/null +++ b/TelegramBotBase/Builder/Interfaces/IThreadingStage.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace TelegramBotBase.Builder.Interfaces +{ + public interface IThreadingStage + { + /// + /// Uses one single thread for message loop. (Default) + /// + /// + public IBuildingStage UseSingleThread(); + + /// + /// Using the threadpool for managing requests. + /// + /// Number of concurrent working threads. + /// Number of concurrent I/O threads. + /// + public IBuildingStage UseThreadPool(int workerThreads = 2, int ioThreads = 1); + + } +} From d5ffa914f098e7e3b62f8d80c951c3c45e48997b Mon Sep 17 00:00:00 2001 From: Florian Zevedei Date: Thu, 28 Dec 2023 15:17:43 +0100 Subject: [PATCH 02/17] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ec0f265..7acd760 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ **Support group: [@tgbotbase](https://t.me/tgbotbase)** -**Discord Server: [https://discord.gg/p3PPhYbTf](https://discord.gg/p3PPhYbTf)** +**Discord Server: [https://discord.gg/V3PxreDYfE](https://discord.gg/V3PxreDYfE)** **Releases: [GitHub](https://github.com/MajMcCloud/TelegramBotFramework/releases)** @@ -1101,4 +1101,4 @@ Want to use Inline- and ReplyMarkup at the same time ? Here is an example how yo Alpha: Full Dependency Injection example within this framework. -- [Examples/DependencyInjection](Examples/DependencyInjection) \ No newline at end of file +- [Examples/DependencyInjection](Examples/DependencyInjection) From 26152ee348bafaf898583217217d60de0cb75c60 Mon Sep 17 00:00:00 2001 From: Florian Zevedei Date: Fri, 29 Dec 2023 14:37:19 +0100 Subject: [PATCH 03/17] Removing System.Drawing.Common dependency --- TelegramBotBase/TelegramBotBase.nuspec | 2 -- 1 file changed, 2 deletions(-) diff --git a/TelegramBotBase/TelegramBotBase.nuspec b/TelegramBotBase/TelegramBotBase.nuspec index 5dfff90..ec58bce 100644 --- a/TelegramBotBase/TelegramBotBase.nuspec +++ b/TelegramBotBase/TelegramBotBase.nuspec @@ -14,12 +14,10 @@ - - From ad40675b87d2180824dd3bd1307a93a2dcd0b95c Mon Sep 17 00:00:00 2001 From: Florian Zevedei Date: Thu, 4 Jan 2024 13:44:18 +0100 Subject: [PATCH 04/17] First release of IronSoftware drawing extension --- .../ImageExtensions.cs | 73 +++++++++++++++++++ .../README.md | 7 ++ ...Base.Extensions.Images.IronSoftware.csproj | 25 +++++++ TelegramBotFramework.sln | 9 ++- 4 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 TelegramBotBase.Extensions.Images.IronSoftware/ImageExtensions.cs create mode 100644 TelegramBotBase.Extensions.Images.IronSoftware/README.md create mode 100644 TelegramBotBase.Extensions.Images.IronSoftware/TelegramBotBase.Extensions.Images.IronSoftware.csproj diff --git a/TelegramBotBase.Extensions.Images.IronSoftware/ImageExtensions.cs b/TelegramBotBase.Extensions.Images.IronSoftware/ImageExtensions.cs new file mode 100644 index 0000000..bc067ee --- /dev/null +++ b/TelegramBotBase.Extensions.Images.IronSoftware/ImageExtensions.cs @@ -0,0 +1,73 @@ +using IronSoftware.Drawing; +using SixLabors.ImageSharp; +using System.IO; +using System.Threading.Tasks; +using Telegram.Bot.Types; +using TelegramBotBase.Form; +using TelegramBotBase.Sessions; +using static IronSoftware.Drawing.AnyBitmap; +using SKImage = SixLabors.ImageSharp.Image; + +namespace TelegramBotBase.Extensions.Images.IronSoftware +{ + public static class ImageExtensions + { + public static Stream ToStream(this AnyBitmap image, ImageFormat format) + { + var stream = new MemoryStream(); + image.ExportStream(stream, format); + stream.Position = 0; + return stream; + } + + public static async Task ToStream(this SKImage image) + { + var stream = new MemoryStream(); + await image.SaveAsPngAsync(stream); + stream.Position = 0; + return stream; + } + + /// + /// Sends an image + /// + /// + /// + /// + /// + /// + /// + public static async Task SendPhoto(this DeviceSession session, AnyBitmap image, string name, + string caption, ButtonForm buttons = null, int replyTo = 0, + bool disableNotification = false) + { + using (var fileStream = ToStream(image, ImageFormat.Png)) + { + var fts = InputFile.FromStream(fileStream, name); + + return await session.SendPhoto(fts, caption, buttons, replyTo, disableNotification); + } + } + + /// + /// Sends an image + /// + /// + /// + /// + /// + /// + /// + public static async Task SendPhoto(this DeviceSession session, SKImage image, string name, + string caption, ButtonForm buttons = null, int replyTo = 0, + bool disableNotification = false) + { + using (var fileStream = await ToStream(image)) + { + var fts = InputFile.FromStream(fileStream, name); + + return await session.SendPhoto(fts, caption, buttons, replyTo, disableNotification); + } + } + } +} \ No newline at end of file diff --git a/TelegramBotBase.Extensions.Images.IronSoftware/README.md b/TelegramBotBase.Extensions.Images.IronSoftware/README.md new file mode 100644 index 0000000..879e552 --- /dev/null +++ b/TelegramBotBase.Extensions.Images.IronSoftware/README.md @@ -0,0 +1,7 @@ +# TelegramBotBase.Extensions.Images.IronSoftware + +[![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/) +[![Telegram chat](https://img.shields.io/badge/Support_Chat-Telegram-blue.svg?style=flat-square)](https://www.t.me/tgbotbase) + +[![License](https://img.shields.io/github/license/MajMcCloud/telegrambotframework.svg?style=flat-square&maxAge=2592000&label=License)](https://raw.githubusercontent.com/MajMcCloud/TelegramBotFramework/master/LICENCE.md) +[![Package 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) diff --git a/TelegramBotBase.Extensions.Images.IronSoftware/TelegramBotBase.Extensions.Images.IronSoftware.csproj b/TelegramBotBase.Extensions.Images.IronSoftware/TelegramBotBase.Extensions.Images.IronSoftware.csproj new file mode 100644 index 0000000..0e60a55 --- /dev/null +++ b/TelegramBotBase.Extensions.Images.IronSoftware/TelegramBotBase.Extensions.Images.IronSoftware.csproj @@ -0,0 +1,25 @@ + + + + netstandard2.0;netcoreapp3.1;net6 + https://github.com/MajMcCloud/TelegramBotFramework + https://github.com/MajMcCloud/TelegramBotFramework + MIT + true + snupkg + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/TelegramBotFramework.sln b/TelegramBotFramework.sln index 8fd0e98..84dbf82 100644 --- a/TelegramBotFramework.sln +++ b/TelegramBotFramework.sln @@ -32,7 +32,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotAndWebApplication", "Exa EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InlineAndReplyCombination", "Examples\InlineAndReplyCombination\InlineAndReplyCombination.csproj", "{067E8EBE-F90A-4AFF-A0FF-20578216486E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DependencyInjection", "Examples\DependencyInjection\DependencyInjection.csproj", "{689B16BC-200E-4C68-BB2E-8B209070849B}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DependencyInjection", "Examples\DependencyInjection\DependencyInjection.csproj", "{689B16BC-200E-4C68-BB2E-8B209070849B}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TelegramBotBase.Extensions.Images.IronSoftware", "TelegramBotBase.Extensions.Images.IronSoftware\TelegramBotBase.Extensions.Images.IronSoftware.csproj", "{DC521A4C-7446-46F7-845B-AAF10EDCF8C6}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -84,6 +86,10 @@ Global {689B16BC-200E-4C68-BB2E-8B209070849B}.Debug|Any CPU.Build.0 = Debug|Any CPU {689B16BC-200E-4C68-BB2E-8B209070849B}.Release|Any CPU.ActiveCfg = Release|Any CPU {689B16BC-200E-4C68-BB2E-8B209070849B}.Release|Any CPU.Build.0 = Release|Any CPU + {DC521A4C-7446-46F7-845B-AAF10EDCF8C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DC521A4C-7446-46F7-845B-AAF10EDCF8C6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DC521A4C-7446-46F7-845B-AAF10EDCF8C6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DC521A4C-7446-46F7-845B-AAF10EDCF8C6}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -98,6 +104,7 @@ Global {52EA3201-02E8-46F5-87C4-B4752C8A815C} = {BFA71E3F-31C0-4FC1-A320-4DCF704768C5} {067E8EBE-F90A-4AFF-A0FF-20578216486E} = {BFA71E3F-31C0-4FC1-A320-4DCF704768C5} {689B16BC-200E-4C68-BB2E-8B209070849B} = {BFA71E3F-31C0-4FC1-A320-4DCF704768C5} + {DC521A4C-7446-46F7-845B-AAF10EDCF8C6} = {E3193182-6FDA-4FA3-AD26-A487291E7681} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {59CB40E1-9FA7-4867-A56F-4F418286F057} From 0cafca9aee7af640cf823ced41286e5125cbab89 Mon Sep 17 00:00:00 2001 From: Florian Zevedei Date: Thu, 4 Jan 2024 13:46:58 +0100 Subject: [PATCH 05/17] Update README.md --- TelegramBotBase.Extensions.Images.IronSoftware/README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/TelegramBotBase.Extensions.Images.IronSoftware/README.md b/TelegramBotBase.Extensions.Images.IronSoftware/README.md index 879e552..357438b 100644 --- a/TelegramBotBase.Extensions.Images.IronSoftware/README.md +++ b/TelegramBotBase.Extensions.Images.IronSoftware/README.md @@ -5,3 +5,8 @@ [![License](https://img.shields.io/github/license/MajMcCloud/telegrambotframework.svg?style=flat-square&maxAge=2592000&label=License)](https://raw.githubusercontent.com/MajMcCloud/TelegramBotFramework/master/LICENCE.md) [![Package 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) + + +This extension uses the [IronSoftware](https://ironsoftware.com/open-source/csharp/drawing/docs/) drawing library. + +Check [https://ironsoftware.com/open-source/csharp/drawing/docs/](https://ironsoftware.com/open-source/csharp/drawing/docs/) for more details on this library. \ No newline at end of file From 75064fd64c845250e483a247f9e231b988930f8a Mon Sep 17 00:00:00 2001 From: Florian Zevedei Date: Thu, 4 Jan 2024 14:11:21 +0100 Subject: [PATCH 06/17] Adding nuget references and nuget specs --- ...mBotBase.Extensions.Images.IronSoftware.csproj | 8 ++++++-- .../TelegramBotBase.Extensions.Images.csproj | 15 +++++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/TelegramBotBase.Extensions.Images.IronSoftware/TelegramBotBase.Extensions.Images.IronSoftware.csproj b/TelegramBotBase.Extensions.Images.IronSoftware/TelegramBotBase.Extensions.Images.IronSoftware.csproj index 0e60a55..df94a01 100644 --- a/TelegramBotBase.Extensions.Images.IronSoftware/TelegramBotBase.Extensions.Images.IronSoftware.csproj +++ b/TelegramBotBase.Extensions.Images.IronSoftware/TelegramBotBase.Extensions.Images.IronSoftware.csproj @@ -2,11 +2,15 @@ netstandard2.0;netcoreapp3.1;net6 - https://github.com/MajMcCloud/TelegramBotFramework - https://github.com/MajMcCloud/TelegramBotFramework + https://github.com/MajMcCloud/TelegramBotFramework/tree/development/TelegramBotBase.Extensions.Images.IronSoftware + https://github.com/MajMcCloud/TelegramBotFramework/tree/development/TelegramBotBase.Extensions.Images.IronSoftware MIT true snupkg + 1.0.1 + 1.0.1 + This is an extension for sending Bitmap/Images platform independent by using IronSoftware's drawing library via TelegramBotBase. + 1.0.1 diff --git a/TelegramBotBase.Extensions.Images/TelegramBotBase.Extensions.Images.csproj b/TelegramBotBase.Extensions.Images/TelegramBotBase.Extensions.Images.csproj index 347e740..6eb1036 100644 --- a/TelegramBotBase.Extensions.Images/TelegramBotBase.Extensions.Images.csproj +++ b/TelegramBotBase.Extensions.Images/TelegramBotBase.Extensions.Images.csproj @@ -2,11 +2,17 @@ netstandard2.0;netcoreapp3.1;net6 - https://github.com/MajMcCloud/TelegramBotFramework - https://github.com/MajMcCloud/TelegramBotFramework + https://github.com/MajMcCloud/TelegramBotFramework/tree/development/TelegramBotBase.Extensions.Images + https://github.com/MajMcCloud/TelegramBotFramework/tree/development/TelegramBotBase.Extensions.Images MIT true snupkg + False + $(AssemblyName) + This is an extension for sending Bitmap/Images via TelegramBotBase. + 1.1.1 + 1.1.1 + 1.1.1 @@ -15,14 +21,11 @@ runtime; build; native; contentfiles; analyzers; buildtransitive + - - - - From 3aff0aa94b5a00ff3da21fff9361f8b295c2788b Mon Sep 17 00:00:00 2001 From: Florian Zevedei Date: Thu, 4 Jan 2024 14:16:52 +0100 Subject: [PATCH 07/17] Updating example projects for use of SingleThread/ThreadPool --- Examples/DependencyInjection/Program.cs | 1 + Examples/EFCoreBot/Program.cs | 1 + Examples/InlineAndReplyCombination/Program.cs | 1 + 3 files changed, 3 insertions(+) diff --git a/Examples/DependencyInjection/Program.cs b/Examples/DependencyInjection/Program.cs index f33bf53..1e32385 100644 --- a/Examples/DependencyInjection/Program.cs +++ b/Examples/DependencyInjection/Program.cs @@ -25,6 +25,7 @@ namespace DependencyInjection .NoCommands() .NoSerialization() .DefaultLanguage() + .UseSingleThread() .Build(); await bot.Start(); diff --git a/Examples/EFCoreBot/Program.cs b/Examples/EFCoreBot/Program.cs index 37f5587..e49a1c1 100644 --- a/Examples/EFCoreBot/Program.cs +++ b/Examples/EFCoreBot/Program.cs @@ -18,6 +18,7 @@ var bot = BotBaseBuilder.Create() .NoCommands() .NoSerialization() .DefaultLanguage() + .UseSingleThread() .Build(); await bot.Start(); diff --git a/Examples/InlineAndReplyCombination/Program.cs b/Examples/InlineAndReplyCombination/Program.cs index fab456d..7354ec7 100644 --- a/Examples/InlineAndReplyCombination/Program.cs +++ b/Examples/InlineAndReplyCombination/Program.cs @@ -21,6 +21,7 @@ namespace InlineAndReplyCombination .DefaultCommands() .UseJSON(Path.Combine(Directory.GetCurrentDirectory(), "states.json")) .UseEnglish() + .UseSingleThread() .Build(); From deef8dd086f2f3745d9d961893d29dcd2cca286a Mon Sep 17 00:00:00 2001 From: Florian Zevedei Date: Thu, 4 Jan 2024 14:17:09 +0100 Subject: [PATCH 08/17] Replacing project references with nuget reference --- TelegramBotBase.Test/TelegramBotBase.Example.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TelegramBotBase.Test/TelegramBotBase.Example.csproj b/TelegramBotBase.Test/TelegramBotBase.Example.csproj index 3e534ad..6fdb766 100644 --- a/TelegramBotBase.Test/TelegramBotBase.Example.csproj +++ b/TelegramBotBase.Test/TelegramBotBase.Example.csproj @@ -8,8 +8,8 @@ - - + + From 8e4bc7a2222ac0ee454f2215ddc9390149ec4bfe Mon Sep 17 00:00:00 2001 From: Florian Zevedei Date: Wed, 17 Jan 2024 22:42:03 +0100 Subject: [PATCH 09/17] Bumps Microsoft.Data.SqlClient from 5.0.0 to 5.1.3. --- .../TelegramBotBase.Extensions.Serializer.Database.MSSQL.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TelegramBotBase.Extensions.Serializer.Database.MSSQL/TelegramBotBase.Extensions.Serializer.Database.MSSQL.csproj b/TelegramBotBase.Extensions.Serializer.Database.MSSQL/TelegramBotBase.Extensions.Serializer.Database.MSSQL.csproj index 4d2c765..7acb0e4 100644 --- a/TelegramBotBase.Extensions.Serializer.Database.MSSQL/TelegramBotBase.Extensions.Serializer.Database.MSSQL.csproj +++ b/TelegramBotBase.Extensions.Serializer.Database.MSSQL/TelegramBotBase.Extensions.Serializer.Database.MSSQL.csproj @@ -16,7 +16,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive From 2b06fba8ffb810f357eccc04b25743b6a0e00695 Mon Sep 17 00:00:00 2001 From: Florian Zevedei Date: Wed, 17 Jan 2024 22:51:40 +0100 Subject: [PATCH 10/17] Adding readme note for IronSoftware extension --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index 7acd760..0583d39 100644 --- a/README.md +++ b/README.md @@ -1050,6 +1050,17 @@ Extends the base package with some additional image methods like SendPhoto (usin [https://www.nuget.org/packages/TelegramBotBase.Extensions.Images/](https://www.nuget.org/packages/TelegramBotBase.Extensions.Images/) +### TelegramBotBase.Extensions.Images.IronSoftware + +Extends the base package with some additional image methods like SendPhoto (using Bitmap) + +Important: This extension uses the IronSoftware drawing library. + +[![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/) +[![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) + +[https://www.nuget.org/packages/TelegramBotBase.Extensions.Images.IronSoftware/](https://www.nuget.org/packages/TelegramBotBase.Extensions.Images.IronSoftware/) + ### TelegramBotBase.Extensions.Serializer.Database.MSSQL A session serializer for Microsoft SQL Server. From fdd814c88ed5a6daebdec8ce424d42a41763897e Mon Sep 17 00:00:00 2001 From: Florian Zevedei Date: Wed, 17 Jan 2024 22:54:30 +0100 Subject: [PATCH 11/17] Adding project reference links --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 0583d39..95dea70 100644 --- a/README.md +++ b/README.md @@ -1050,6 +1050,8 @@ Extends the base package with some additional image methods like SendPhoto (usin [https://www.nuget.org/packages/TelegramBotBase.Extensions.Images/](https://www.nuget.org/packages/TelegramBotBase.Extensions.Images/) +Project: [open source](Extensions/TelegramBotBase.Extensions.Images/) + ### TelegramBotBase.Extensions.Images.IronSoftware Extends the base package with some additional image methods like SendPhoto (using Bitmap) @@ -1061,6 +1063,8 @@ Important: This extension uses the IronSoftware drawing library. [https://www.nuget.org/packages/TelegramBotBase.Extensions.Images.IronSoftware/](https://www.nuget.org/packages/TelegramBotBase.Extensions.Images.IronSoftware/) +Project: [open source](Extensions/TelegramBotBase.Extensions.Images.IronSoftware/) + ### TelegramBotBase.Extensions.Serializer.Database.MSSQL A session serializer for Microsoft SQL Server. @@ -1070,6 +1074,8 @@ A session serializer for Microsoft SQL Server. [https://www.nuget.org/packages/TelegramBotBase.Extensions.Serializer.Database.MSSQL/](https://www.nuget.org/packages/TelegramBotBase.Extensions.Serializer.Database.MSSQL/) +Project: [open source](Extensions/TelegramBotBase.Extensions.Serializer.Database.MSSQL/) + ## Test Project There is a "TelegramBotBase.Test" project inside the repository which includes minimal examples for all controls available. From 2d3393aa059083573c386b9adc2b7e52dd2468c7 Mon Sep 17 00:00:00 2001 From: Florian Zevedei Date: Wed, 17 Jan 2024 22:55:11 +0100 Subject: [PATCH 12/17] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 95dea70..a4ec1b1 100644 --- a/README.md +++ b/README.md @@ -1050,7 +1050,7 @@ Extends the base package with some additional image methods like SendPhoto (usin [https://www.nuget.org/packages/TelegramBotBase.Extensions.Images/](https://www.nuget.org/packages/TelegramBotBase.Extensions.Images/) -Project: [open source](Extensions/TelegramBotBase.Extensions.Images/) +Project: [open source](TelegramBotBase.Extensions.Images/) ### TelegramBotBase.Extensions.Images.IronSoftware @@ -1063,7 +1063,7 @@ Important: This extension uses the IronSoftware drawing library. [https://www.nuget.org/packages/TelegramBotBase.Extensions.Images.IronSoftware/](https://www.nuget.org/packages/TelegramBotBase.Extensions.Images.IronSoftware/) -Project: [open source](Extensions/TelegramBotBase.Extensions.Images.IronSoftware/) +Project: [open source](TelegramBotBase.Extensions.Images.IronSoftware/) ### TelegramBotBase.Extensions.Serializer.Database.MSSQL @@ -1074,7 +1074,7 @@ A session serializer for Microsoft SQL Server. [https://www.nuget.org/packages/TelegramBotBase.Extensions.Serializer.Database.MSSQL/](https://www.nuget.org/packages/TelegramBotBase.Extensions.Serializer.Database.MSSQL/) -Project: [open source](Extensions/TelegramBotBase.Extensions.Serializer.Database.MSSQL/) +Project: [open source](TelegramBotBase.Extensions.Serializer.Database.MSSQL/) ## Test Project From dbdc40582af086ba5f57ce2b22f5d8d9423bb577 Mon Sep 17 00:00:00 2001 From: Florian Zevedei Date: Wed, 24 Jan 2024 03:04:05 +0100 Subject: [PATCH 13/17] Splitting MessageClient into 2 instances --- .../TelegramBotBase.Example.csproj | 5 +- TelegramBotBase/Base/MessageClient.cs | 88 +++----------- .../Base/ThreadPoolMessageClient.cs | 112 ++++++++++++++++++ TelegramBotBase/Builder/BotBaseBuilder.cs | 15 +-- 4 files changed, 140 insertions(+), 80 deletions(-) create mode 100644 TelegramBotBase/Base/ThreadPoolMessageClient.cs diff --git a/TelegramBotBase.Test/TelegramBotBase.Example.csproj b/TelegramBotBase.Test/TelegramBotBase.Example.csproj index 6fdb766..3affa0a 100644 --- a/TelegramBotBase.Test/TelegramBotBase.Example.csproj +++ b/TelegramBotBase.Test/TelegramBotBase.Example.csproj @@ -8,8 +8,11 @@ - + + + + diff --git a/TelegramBotBase/Base/MessageClient.cs b/TelegramBotBase/Base/MessageClient.cs index 5271dc4..62c083d 100644 --- a/TelegramBotBase/Base/MessageClient.cs +++ b/TelegramBotBase/Base/MessageClient.cs @@ -9,6 +9,7 @@ using Telegram.Bot; using Telegram.Bot.Exceptions; using Telegram.Bot.Polling; using Telegram.Bot.Types; +using TelegramBotBase.Interfaces; namespace TelegramBotBase.Base; @@ -17,22 +18,18 @@ namespace TelegramBotBase.Base; /// public class MessageClient { + private EventHandlerList Events { get; } = new(); + private static readonly object EvOnMessageLoop = new(); private static readonly object EvOnReceiveError = new(); - private static object __evOnMessage = new(); - - private static object __evOnMessageEdit = new(); - - private static object __evCallbackQuery = new(); - private CancellationTokenSource _cancellationTokenSource; public string ApiKey { get; } public ITelegramBotClient TelegramClient { get; set; } - private EventHandlerList Events { get; } = new(); + /// /// Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before @@ -42,27 +39,17 @@ public class MessageClient /// public bool ThrowPendingUpdates { get; set; } - public bool UseThreadPool { get; set; } = false; - - public int ThreadPool_WorkerThreads { get; set; } = 1; - - public int ThreadPool_IOThreads { get; set; } = 1; - public MessageClient(string apiKey) { ApiKey = apiKey; TelegramClient = new TelegramBotClient(apiKey); - - Prepare(); } public MessageClient(string apiKey, HttpClient proxy) { ApiKey = apiKey; TelegramClient = new TelegramBotClient(apiKey, proxy); - - Prepare(); } @@ -80,8 +67,6 @@ public class MessageClient ); TelegramClient = new TelegramBotClient(apiKey, httpClient); - - Prepare(); } /// @@ -101,8 +86,6 @@ public class MessageClient ); TelegramClient = new TelegramBotClient(apiKey, httpClient); - - Prepare(); } @@ -110,20 +93,10 @@ public class MessageClient { ApiKey = apiKey; TelegramClient = client; - - Prepare(); } - - - public void Prepare() - { - TelegramClient.Timeout = new TimeSpan(0, 0, 30); - } - - - public void StartReceiving() + public virtual void StartReceiving() { _cancellationTokenSource = new CancellationTokenSource(); @@ -131,66 +104,32 @@ public class MessageClient receiverOptions.ThrowPendingUpdates = ThrowPendingUpdates; - if (UseThreadPool) - { - ThreadPool.SetMaxThreads(ThreadPool_WorkerThreads, ThreadPool_IOThreads); - - TelegramClient.StartReceiving(HandleUpdateAsyncThreadPool, HandleErrorAsyncThreadPool, receiverOptions, - _cancellationTokenSource.Token); - } - else - { - TelegramClient.StartReceiving(HandleUpdateAsync, HandleErrorAsync, receiverOptions, - _cancellationTokenSource.Token); - } + TelegramClient.Timeout = new TimeSpan(0, 1, 0); + TelegramClient.StartReceiving(HandleUpdateAsync, HandleErrorAsync, receiverOptions, _cancellationTokenSource.Token); } - public void StopReceiving() + + public virtual void StopReceiving() { _cancellationTokenSource.Cancel(); } - #region "Single Thread" public async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken) { await OnMessageLoop(new UpdateResult(update, null)); } + public async Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken) { await OnReceiveError(new ErrorResult(exception)); } - #endregion - - #region "Thread Pool" - - public Task HandleUpdateAsyncThreadPool(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken) - { - ThreadPool.QueueUserWorkItem(async a => - { - await OnMessageLoop(new UpdateResult(update, null)); - }); - - return Task.CompletedTask; - } - - public Task HandleErrorAsyncThreadPool(ITelegramBotClient botClient, Exception exception, - CancellationToken cancellationToken) - { - ThreadPool.QueueUserWorkItem(async a => - { - await OnReceiveError(new ErrorResult(exception)); - }); - - return Task.CompletedTask; - } - - #endregion + #region "BotCommands" /// /// This will return the current list of bot commands. /// @@ -222,6 +161,8 @@ public class MessageClient await TelegramClient.DeleteMyCommandsAsync(scope, languageCode); } + #endregion + #region "Events" @@ -231,6 +172,7 @@ public class MessageClient remove => Events.RemoveHandler(EvOnMessageLoop, value); } + public async Task OnMessageLoop(UpdateResult update) { var eventHandlers = (Events[EvOnMessageLoop] as Async.AsyncEventHandler)?.Invoke(this, update); @@ -248,6 +190,7 @@ public class MessageClient remove => Events.RemoveHandler(EvOnReceiveError, value); } + public async Task OnReceiveError(ErrorResult update) { var eventHandlers = (Events[EvOnReceiveError] as Async.AsyncEventHandler)?.Invoke(this, update); @@ -271,4 +214,5 @@ public class MessageClient } #endregion + } diff --git a/TelegramBotBase/Base/ThreadPoolMessageClient.cs b/TelegramBotBase/Base/ThreadPoolMessageClient.cs new file mode 100644 index 0000000..7808850 --- /dev/null +++ b/TelegramBotBase/Base/ThreadPoolMessageClient.cs @@ -0,0 +1,112 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Net; +using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; +using Telegram.Bot; +using Telegram.Bot.Exceptions; +using Telegram.Bot.Polling; +using Telegram.Bot.Types; +using TelegramBotBase.Interfaces; + +namespace TelegramBotBase.Base; + +/// +/// Base class for message handling +/// +public class ThreadPoolMessageClient : 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 int ThreadPool_WorkerThreads { get; set; } = 1; + + public int ThreadPool_IOThreads { get; set; } = 1; + + + public ThreadPoolMessageClient(string apiKey) : base(apiKey) + { + + } + + public ThreadPoolMessageClient(string apiKey, HttpClient proxy) : base(apiKey, proxy) + { + + } + + + public ThreadPoolMessageClient(string apiKey, Uri proxyUrl, NetworkCredential credential = null) : base(apiKey, proxyUrl, credential) + { + + } + + /// + /// Initializes the client with a proxy + /// + /// + /// i.e. 127.0.0.1 + /// i.e. 10000 + public ThreadPoolMessageClient(string apiKey, string proxyHost, int proxyPort) : base(apiKey, proxyHost, proxyPort) + { + + } + + + public ThreadPoolMessageClient(string apiKey, TelegramBotClient client) : base(apiKey, client) + { + + } + + + + public override void StartReceiving() + { + _cancellationTokenSource = new CancellationTokenSource(); + + var receiverOptions = new ReceiverOptions(); + + receiverOptions.ThrowPendingUpdates = ThrowPendingUpdates; + + ThreadPool.SetMaxThreads(ThreadPool_WorkerThreads, ThreadPool_IOThreads); + + TelegramClient.Timeout = new TimeSpan(0, 1, 0); + + TelegramClient.StartReceiving(HandleUpdateAsyncThreadPool, HandleErrorAsyncThreadPool, receiverOptions, _cancellationTokenSource.Token); + } + + public override void StopReceiving() + { + _cancellationTokenSource.Cancel(); + } + + + public Task HandleUpdateAsyncThreadPool(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken) + { + ThreadPool.QueueUserWorkItem(async a => + { + await OnMessageLoop(new UpdateResult(update, null)); + }); + + return Task.CompletedTask; + } + + public Task HandleErrorAsyncThreadPool(ITelegramBotClient botClient, Exception exception, + CancellationToken cancellationToken) + { + ThreadPool.QueueUserWorkItem(async a => + { + await OnReceiveError(new ErrorResult(exception)); + }); + + return Task.CompletedTask; + } + +} diff --git a/TelegramBotBase/Builder/BotBaseBuilder.cs b/TelegramBotBase/Builder/BotBaseBuilder.cs index 85b5621..4291063 100644 --- a/TelegramBotBase/Builder/BotBaseBuilder.cs +++ b/TelegramBotBase/Builder/BotBaseBuilder.cs @@ -32,6 +32,7 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage, private BotBaseBuilder() { + } /// @@ -429,26 +430,26 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage, #endregion + #region "Step 8 (Threading)" public IBuildingStage UseSingleThread() { - _client.UseThreadPool = false; - return this; } public IBuildingStage UseThreadPool(int workerThreads = 2, int ioThreads = 1) { - _client.UseThreadPool = true; - _client.ThreadPool_WorkerThreads = workerThreads; - _client.ThreadPool_IOThreads = ioThreads; + var c = new ThreadPoolMessageClient(_apiKey, (TelegramBotClient)_client.TelegramClient); + + c.ThreadPool_WorkerThreads = workerThreads; + c.ThreadPool_IOThreads = ioThreads; + + _client = c; return this; } - - #endregion } From 94c680e0360f03cacba62ec50bd84c1cb1185a51 Mon Sep 17 00:00:00 2001 From: Florian Zevedei Date: Wed, 24 Jan 2024 17:39:18 +0100 Subject: [PATCH 14/17] Timeout fixes - Fixing wrong timeout setter - removing timeout setter in "Prepare" method in client which overrides setting --- TelegramBotBase/Base/MessageClient.cs | 9 ++++----- .../Base/ThreadPoolMessageClient.cs | 4 +--- TelegramBotBase/Builder/BotBaseBuilder.cs | 20 +++++++++---------- .../Interfaces/INetworkingSelectionStage.cs | 10 +++++----- 4 files changed, 20 insertions(+), 23 deletions(-) diff --git a/TelegramBotBase/Base/MessageClient.cs b/TelegramBotBase/Base/MessageClient.cs index 62c083d..6e4ae5c 100644 --- a/TelegramBotBase/Base/MessageClient.cs +++ b/TelegramBotBase/Base/MessageClient.cs @@ -3,13 +3,14 @@ using System.Collections.Generic; using System.ComponentModel; using System.Net; using System.Net.Http; +using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; using Telegram.Bot; using Telegram.Bot.Exceptions; using Telegram.Bot.Polling; using Telegram.Bot.Types; -using TelegramBotBase.Interfaces; + namespace TelegramBotBase.Base; @@ -104,8 +105,6 @@ public class MessageClient receiverOptions.ThrowPendingUpdates = ThrowPendingUpdates; - TelegramClient.Timeout = new TimeSpan(0, 1, 0); - TelegramClient.StartReceiving(HandleUpdateAsync, HandleErrorAsync, receiverOptions, _cancellationTokenSource.Token); } @@ -116,13 +115,13 @@ public class MessageClient } - public async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken) + private async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken) { await OnMessageLoop(new UpdateResult(update, null)); } - public async Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, + private async Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken) { await OnReceiveError(new ErrorResult(exception)); diff --git a/TelegramBotBase/Base/ThreadPoolMessageClient.cs b/TelegramBotBase/Base/ThreadPoolMessageClient.cs index 7808850..2b7cd3e 100644 --- a/TelegramBotBase/Base/ThreadPoolMessageClient.cs +++ b/TelegramBotBase/Base/ThreadPoolMessageClient.cs @@ -76,9 +76,7 @@ public class ThreadPoolMessageClient : MessageClient receiverOptions.ThrowPendingUpdates = ThrowPendingUpdates; ThreadPool.SetMaxThreads(ThreadPool_WorkerThreads, ThreadPool_IOThreads); - - TelegramClient.Timeout = new TimeSpan(0, 1, 0); - + TelegramClient.StartReceiving(HandleUpdateAsyncThreadPool, HandleErrorAsyncThreadPool, receiverOptions, _cancellationTokenSource.Token); } diff --git a/TelegramBotBase/Builder/BotBaseBuilder.cs b/TelegramBotBase/Builder/BotBaseBuilder.cs index 4291063..8a971c3 100644 --- a/TelegramBotBase/Builder/BotBaseBuilder.cs +++ b/TelegramBotBase/Builder/BotBaseBuilder.cs @@ -214,14 +214,14 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage, #region "Step 4 (Network Settings)" - public IBotCommandsStage WithProxy(string proxyAddress, bool throwPendingUpdates = false) + public IBotCommandsStage WithProxy(string proxyAddress, bool throwPendingUpdates = false, int timeoutInSeconds = 60) { var url = new Uri(proxyAddress); _client = new MessageClient(_apiKey, url) { TelegramClient = { - Timeout = new TimeSpan(0, 1, 0) + Timeout = TimeSpan.FromSeconds(timeoutInSeconds) }, }; _client.ThrowPendingUpdates = throwPendingUpdates; @@ -229,13 +229,13 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage, } - public IBotCommandsStage NoProxy(bool throwPendingUpdates = false) + public IBotCommandsStage NoProxy(bool throwPendingUpdates = false, int timeoutInSeconds = 60) { _client = new MessageClient(_apiKey) { TelegramClient = { - Timeout = new TimeSpan(0, 1, 0) + Timeout = TimeSpan.FromSeconds(timeoutInSeconds)// new TimeSpan(0, 1, 0) } }; _client.ThrowPendingUpdates = throwPendingUpdates; @@ -243,13 +243,13 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage, } - public IBotCommandsStage WithBotClient(TelegramBotClient tgclient, bool throwPendingUpdates = false) + public IBotCommandsStage WithBotClient(TelegramBotClient tgclient, bool throwPendingUpdates = false, int timeoutInSeconds = 60) { _client = new MessageClient(_apiKey, tgclient) { TelegramClient = { - Timeout = new TimeSpan(0, 1, 0) + Timeout = TimeSpan.FromSeconds(timeoutInSeconds)// new TimeSpan(0, 1, 0) } }; _client.ThrowPendingUpdates = throwPendingUpdates; @@ -257,26 +257,26 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage, } - public IBotCommandsStage WithHostAndPort(string proxyHost, int proxyPort, bool throwPendingUpdates = false) + public IBotCommandsStage WithHostAndPort(string proxyHost, int proxyPort, bool throwPendingUpdates = false, int timeoutInSeconds = 60) { _client = new MessageClient(_apiKey, proxyHost, proxyPort) { TelegramClient = { - Timeout = new TimeSpan(0, 1, 0) + Timeout = TimeSpan.FromSeconds(timeoutInSeconds)// new TimeSpan(0, 1, 0) } }; _client.ThrowPendingUpdates = throwPendingUpdates; return this; } - public IBotCommandsStage WithHttpClient(HttpClient tgclient, bool throwPendingUpdates = false) + public IBotCommandsStage WithHttpClient(HttpClient tgclient, bool throwPendingUpdates = false, int timeoutInSeconds = 60) { _client = new MessageClient(_apiKey, tgclient) { TelegramClient = { - Timeout = new TimeSpan(0, 1, 0) + Timeout = TimeSpan.FromSeconds(timeoutInSeconds)// new TimeSpan(0, 1, 0) } }; _client.ThrowPendingUpdates = throwPendingUpdates; diff --git a/TelegramBotBase/Builder/Interfaces/INetworkingSelectionStage.cs b/TelegramBotBase/Builder/Interfaces/INetworkingSelectionStage.cs index cd17e9d..8ab58d4 100644 --- a/TelegramBotBase/Builder/Interfaces/INetworkingSelectionStage.cs +++ b/TelegramBotBase/Builder/Interfaces/INetworkingSelectionStage.cs @@ -11,14 +11,14 @@ public interface INetworkingSelectionStage /// /// Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling. /// - IBotCommandsStage WithProxy(string proxyAddress, bool throwPendingUpdates = false); + IBotCommandsStage WithProxy(string proxyAddress, bool throwPendingUpdates = false, int timeoutInSeconds = 60); /// /// 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(bool throwPendingUpdates = false); + IBotCommandsStage NoProxy(bool throwPendingUpdates = false, int timeoutInSeconds = 60); /// @@ -27,7 +27,7 @@ public interface INetworkingSelectionStage /// /// Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling. /// - IBotCommandsStage WithBotClient(TelegramBotClient client, bool throwPendingUpdates = false); + IBotCommandsStage WithBotClient(TelegramBotClient client, bool throwPendingUpdates = false, int timeoutInSeconds = 60); /// @@ -37,7 +37,7 @@ public interface INetworkingSelectionStage /// /// Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling. /// - IBotCommandsStage WithHostAndPort(string proxyHost, int Port, bool throwPendingUpdates = false); + IBotCommandsStage WithHostAndPort(string proxyHost, int Port, bool throwPendingUpdates = false, int timeoutInSeconds = 60); /// /// Uses a custom http client. @@ -45,5 +45,5 @@ public interface INetworkingSelectionStage /// /// Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling. /// - IBotCommandsStage WithHttpClient(HttpClient client, bool throwPendingUpdates = false); + IBotCommandsStage WithHttpClient(HttpClient client, bool throwPendingUpdates = false, int timeoutInSeconds = 60); } \ No newline at end of file From 078bdfb163a122e217ae440a4332adb295cf3873 Mon Sep 17 00:00:00 2001 From: Florian Zevedei Date: Wed, 24 Jan 2024 17:43:44 +0100 Subject: [PATCH 15/17] Making cancellationTokenSource protected --- TelegramBotBase/Base/MessageClient.cs | 2 +- TelegramBotBase/Base/ThreadPoolMessageClient.cs | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/TelegramBotBase/Base/MessageClient.cs b/TelegramBotBase/Base/MessageClient.cs index 6e4ae5c..5d82f8d 100644 --- a/TelegramBotBase/Base/MessageClient.cs +++ b/TelegramBotBase/Base/MessageClient.cs @@ -24,7 +24,7 @@ public class MessageClient private static readonly object EvOnMessageLoop = new(); private static readonly object EvOnReceiveError = new(); - private CancellationTokenSource _cancellationTokenSource; + protected CancellationTokenSource _cancellationTokenSource; public string ApiKey { get; } diff --git a/TelegramBotBase/Base/ThreadPoolMessageClient.cs b/TelegramBotBase/Base/ThreadPoolMessageClient.cs index 2b7cd3e..41fb131 100644 --- a/TelegramBotBase/Base/ThreadPoolMessageClient.cs +++ b/TelegramBotBase/Base/ThreadPoolMessageClient.cs @@ -18,7 +18,6 @@ namespace TelegramBotBase.Base; /// public class ThreadPoolMessageClient : MessageClient { - private CancellationTokenSource _cancellationTokenSource; /// /// Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before From c1d1d5f543c45d962ea6202546bd6a7bc76af6d4 Mon Sep 17 00:00:00 2001 From: Florian Zevedei Date: Wed, 24 Jan 2024 17:48:25 +0100 Subject: [PATCH 16/17] Fix, add missing ThrowPendingUpdates --- TelegramBotBase/Builder/BotBaseBuilder.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/TelegramBotBase/Builder/BotBaseBuilder.cs b/TelegramBotBase/Builder/BotBaseBuilder.cs index 8a971c3..6dd7462 100644 --- a/TelegramBotBase/Builder/BotBaseBuilder.cs +++ b/TelegramBotBase/Builder/BotBaseBuilder.cs @@ -444,6 +444,7 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage, c.ThreadPool_WorkerThreads = workerThreads; c.ThreadPool_IOThreads = ioThreads; + c.ThrowPendingUpdates = _client.ThrowPendingUpdates; _client = c; From b20b3b282638d7b45197758618336aef8e14895f Mon Sep 17 00:00:00 2001 From: Florian Zevedei Date: Wed, 24 Jan 2024 22:12:26 +0100 Subject: [PATCH 17/17] Bugfix, adding missing DeviceSession to UpdateResult --- TelegramBotBase/BotBase.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/TelegramBotBase/BotBase.cs b/TelegramBotBase/BotBase.cs index 05f8053..a481d6d 100644 --- a/TelegramBotBase/BotBase.cs +++ b/TelegramBotBase/BotBase.cs @@ -115,12 +115,13 @@ public sealed class BotBase if (ds == null) { ds = await Sessions.StartSession(e.DeviceId); - e.Device = ds; ds.LastMessage = e.RawData.Message; OnSessionBegins(new SessionBeginEventArgs(e.DeviceId, ds)); } + e.Device = ds; + var mr = new MessageResult(e.RawData); var i = 0;