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 1/2] [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 + + + + + + + + + + + From f1c437561390b446d38434c22f65e71b058330d6 Mon Sep 17 00:00:00 2001 From: Andriy <30056636+West14@users.noreply.github.com> Date: Fri, 13 Jan 2023 01:28:34 +0200 Subject: [PATCH 2/2] [sticker-tools] add to dockerfile, adjust project configuration Co-authored-by: voed --- Dockerfile | 1 + modules/StickerTools/Handler.cs | 14 +++++------ modules/StickerTools/StickerTools.cs | 2 +- modules/StickerTools/StickerTools.csproj | 30 ++++++++++++++---------- 4 files changed, 27 insertions(+), 20 deletions(-) diff --git a/Dockerfile b/Dockerfile index 66f18ff..9e9a18e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -18,6 +18,7 @@ COPY ./modules/Reputation/*.csproj ./modules/Reputation/ COPY ./modules/ContentStore/*.csproj ./modules/ContentStore/ COPY ./modules/DebugTools/*.csproj ./modules/DebugTools/ COPY ./modules/CodeWatcher/*.csproj ./modules/CodeWatcher/ +COPY ./modules/StickerTools/*.csproj ./modules/StickerTools/ RUN dotnet restore Kruzya.TelegramBot.sln # Copy all project files diff --git a/modules/StickerTools/Handler.cs b/modules/StickerTools/Handler.cs index 03537f1..8f7710a 100644 --- a/modules/StickerTools/Handler.cs +++ b/modules/StickerTools/Handler.cs @@ -2,11 +2,11 @@ using BotFramework.Attributes; using BotFramework.Enums; using SixLabors.ImageSharp; -using Telegram.Bot.Types.Enums; using Telegram.Bot; using Telegram.Bot.Types; +using Telegram.Bot.Types.Enums; -namespace StickerTools; +namespace West.TelegramBot.StickerTools; public class Handler : BotEventHandler { @@ -18,19 +18,19 @@ public class Handler : BotEventHandler { 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); - + 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 index 7b0a427..f49f171 100644 --- a/modules/StickerTools/StickerTools.cs +++ b/modules/StickerTools/StickerTools.cs @@ -1,6 +1,6 @@ using Kruzya.TelegramBot.Core; -namespace StickerTools; +namespace West.TelegramBot.StickerTools; public class StickerTools : Module { diff --git a/modules/StickerTools/StickerTools.csproj b/modules/StickerTools/StickerTools.csproj index 63a32c1..85f4c55 100644 --- a/modules/StickerTools/StickerTools.csproj +++ b/modules/StickerTools/StickerTools.csproj @@ -1,17 +1,23 @@ - + - - net6.0 - enable - enable - + + net6.0 + enable + enable + TelegramBot.StickerTools + West.TelegramBot.StickerTools + - - - + + + - - - + + + + + + +