From 7598f776374548f47e07756f19364d3523f86f28 Mon Sep 17 00:00:00 2001 From: Andriy <30056636+West14@users.noreply.github.com> Date: Fri, 13 Jan 2023 00:58:12 +0200 Subject: [PATCH] [sticker-tools] init module --- Kruzya.TelegramBot.sln | 9 +++++- modules/StickerTools/Handler.cs | 36 ++++++++++++++++++++++++ modules/StickerTools/StickerTools.cs | 10 +++++++ modules/StickerTools/StickerTools.csproj | 17 +++++++++++ 4 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 modules/StickerTools/Handler.cs create mode 100644 modules/StickerTools/StickerTools.cs create mode 100644 modules/StickerTools/StickerTools.csproj diff --git a/Kruzya.TelegramBot.sln b/Kruzya.TelegramBot.sln index 3a2ccca..3dc09f6 100644 --- a/Kruzya.TelegramBot.sln +++ b/Kruzya.TelegramBot.sln @@ -31,7 +31,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ContentStore", "modules\Con EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DebugTools", "modules\DebugTools\DebugTools.csproj", "{B95C9180-4893-4A33-BF43-AB2EE998650C}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeWatcher", "modules\CodeWatcher\CodeWatcher.csproj", "{DEF70068-93E5-4198-8CED-4B3D8AE65162}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CodeWatcher", "modules\CodeWatcher\CodeWatcher.csproj", "{DEF70068-93E5-4198-8CED-4B3D8AE65162}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StickerTools", "modules\StickerTools\StickerTools.csproj", "{1D4FE1FA-F7FB-4F9E-95A3-EE761F29B159}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -95,6 +97,10 @@ Global {DEF70068-93E5-4198-8CED-4B3D8AE65162}.Debug|Any CPU.Build.0 = Debug|Any CPU {DEF70068-93E5-4198-8CED-4B3D8AE65162}.Release|Any CPU.ActiveCfg = Release|Any CPU {DEF70068-93E5-4198-8CED-4B3D8AE65162}.Release|Any CPU.Build.0 = Release|Any CPU + {1D4FE1FA-F7FB-4F9E-95A3-EE761F29B159}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1D4FE1FA-F7FB-4F9E-95A3-EE761F29B159}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1D4FE1FA-F7FB-4F9E-95A3-EE761F29B159}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1D4FE1FA-F7FB-4F9E-95A3-EE761F29B159}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -112,6 +118,7 @@ Global {4F1E8FE8-640F-4671-87FC-0FFCCB6EDF5E} = {C7821F15-DEDD-474F-A575-A296D4B58F10} {B95C9180-4893-4A33-BF43-AB2EE998650C} = {C7821F15-DEDD-474F-A575-A296D4B58F10} {DEF70068-93E5-4198-8CED-4B3D8AE65162} = {C7821F15-DEDD-474F-A575-A296D4B58F10} + {1D4FE1FA-F7FB-4F9E-95A3-EE761F29B159} = {C7821F15-DEDD-474F-A575-A296D4B58F10} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {5BA73C3B-D5FC-4942-9091-504325CDC308} diff --git a/modules/StickerTools/Handler.cs b/modules/StickerTools/Handler.cs new file mode 100644 index 0000000..03537f1 --- /dev/null +++ b/modules/StickerTools/Handler.cs @@ -0,0 +1,36 @@ +using BotFramework; +using BotFramework.Attributes; +using BotFramework.Enums; +using SixLabors.ImageSharp; +using Telegram.Bot.Types.Enums; +using Telegram.Bot; +using Telegram.Bot.Types; + +namespace StickerTools; + +public class Handler : BotEventHandler +{ + [Command(InChat.All, "png", CommandParseMode.Both)] + public async Task Sticker2Png() + { + var sticker = RawUpdate.Message?.ReplyToMessage?.Sticker; + if (sticker == null || sticker.IsAnimated || sticker.IsVideo) + { + return; + } + + await Bot.SendChatActionAsync(Chat.Id, ChatAction.UploadDocument); + using var file = new MemoryStream(); + await Bot.GetInfoAndDownloadFileAsync(sticker.FileId, file); + file.Position = 0; + + using var img = await Image.LoadAsync(file); + await file.DisposeAsync(); + + using var outfile = new MemoryStream(); + await img.SaveAsPngAsync(outfile); + outfile.Position = 0; + await Bot.SendDocumentAsync(Chat.Id, new InputFile(outfile, "sticker.png"), replyToMessageId: RawUpdate?.Message?.MessageId); + + } +} \ No newline at end of file diff --git a/modules/StickerTools/StickerTools.cs b/modules/StickerTools/StickerTools.cs new file mode 100644 index 0000000..7b0a427 --- /dev/null +++ b/modules/StickerTools/StickerTools.cs @@ -0,0 +1,10 @@ +using Kruzya.TelegramBot.Core; + +namespace StickerTools; + +public class StickerTools : Module +{ + public StickerTools(Core core) : base(core) + { + } +} diff --git a/modules/StickerTools/StickerTools.csproj b/modules/StickerTools/StickerTools.csproj new file mode 100644 index 0000000..63a32c1 --- /dev/null +++ b/modules/StickerTools/StickerTools.csproj @@ -0,0 +1,17 @@ + + + + net6.0 + enable + enable + + + + + + + + + + +