From 621ce7fbce9e2dc469403974acabe1094c3ed8cd Mon Sep 17 00:00:00 2001 From: Sergey Gut Date: Wed, 29 May 2024 00:28:17 +0300 Subject: [PATCH] start working on custom chat handlers --- CustomChatAddOns/CustomChatAddOns.cs | 19 +++++++ CustomChatAddOns/CustomChatAddOns.csproj | 15 +++++ CustomChatAddOns/Data/Ping.cs | 24 ++++++++ CustomChatAddOns/Data/Pong.cs | 12 ++++ CustomChatAddOns/Handler/Config.cs | 57 +++++++++++++++++++ CustomChatAddOns/Options/CustomChatHandler.cs | 19 +++++++ Kruzya.TelegramBot.sln | 9 ++- 7 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 CustomChatAddOns/CustomChatAddOns.cs create mode 100644 CustomChatAddOns/CustomChatAddOns.csproj create mode 100644 CustomChatAddOns/Data/Ping.cs create mode 100644 CustomChatAddOns/Data/Pong.cs create mode 100644 CustomChatAddOns/Handler/Config.cs create mode 100644 CustomChatAddOns/Options/CustomChatHandler.cs diff --git a/CustomChatAddOns/CustomChatAddOns.cs b/CustomChatAddOns/CustomChatAddOns.cs new file mode 100644 index 0000000..352d18e --- /dev/null +++ b/CustomChatAddOns/CustomChatAddOns.cs @@ -0,0 +1,19 @@ +using Kruzya.TelegramBot.Core; +using Kruzya.TelegramBot.Core.Extensions; +using Kruzya.TelegramBot.CustomChatAddOns.Options; +using Microsoft.Extensions.DependencyInjection; + +namespace Kruzya.TelegramBot.CustomChatAddOns +{ + public class CustomChatAddOns : Module + { + public CustomChatAddOns(Core.Core core) : base(core) + { + } + + public override void ConfigureServices(IServiceCollection services) + { + services.AddOption(); + } + } +} diff --git a/CustomChatAddOns/CustomChatAddOns.csproj b/CustomChatAddOns/CustomChatAddOns.csproj new file mode 100644 index 0000000..6f0d215 --- /dev/null +++ b/CustomChatAddOns/CustomChatAddOns.csproj @@ -0,0 +1,15 @@ + + + + net6.0 + enable + enable + TelegramBot.CustomChatAddOns + Kruzya.TelegramBot.CustomChatAddOns + + + + + + + diff --git a/CustomChatAddOns/Data/Ping.cs b/CustomChatAddOns/Data/Ping.cs new file mode 100644 index 0000000..d6985a7 --- /dev/null +++ b/CustomChatAddOns/Data/Ping.cs @@ -0,0 +1,24 @@ +using Kruzya.TelegramBot.Core.Attributes; +using Newtonsoft.Json; +using Telegram.Bot.Types; + +namespace Kruzya.TelegramBot.CustomChatAddOns.Data +{ + [AllowedSerializableType("ping")] + public class Ping + { + [JsonProperty("chat_id")] + public long ChatId { get; set; } + + [JsonProperty("user_id")] + public long UserId { get; set; } + + [JsonProperty("confirmation")] + public string Confirmation { get; set; } = Guid.NewGuid().ToString(); + + public Ping From(Message message) => new() { + ChatId = message.Chat.Id, + UserId = message.From!.Id + }; + } +} diff --git a/CustomChatAddOns/Data/Pong.cs b/CustomChatAddOns/Data/Pong.cs new file mode 100644 index 0000000..bafd76c --- /dev/null +++ b/CustomChatAddOns/Data/Pong.cs @@ -0,0 +1,12 @@ +using Kruzya.TelegramBot.Core.Attributes; +using Newtonsoft.Json; + +namespace Kruzya.TelegramBot.CustomChatAddOns.Data +{ + [AllowedSerializableType("pong")] + public class Pong + { + [JsonProperty("confirmation")] + public string Confirmation { get; set; } = Guid.NewGuid().ToString(); + } +} diff --git a/CustomChatAddOns/Handler/Config.cs b/CustomChatAddOns/Handler/Config.cs new file mode 100644 index 0000000..f494771 --- /dev/null +++ b/CustomChatAddOns/Handler/Config.cs @@ -0,0 +1,57 @@ +using BotFramework.Attributes; +using Kruzya.TelegramBot.Core.Data; +using Kruzya.TelegramBot.Core.Extensions; +using Kruzya.TelegramBot.Core.Handler; +using Kruzya.TelegramBot.Core.Service; +using Kruzya.TelegramBot.CustomChatAddOns.Options; +using Telegram.Bot.Types.Enums; + +namespace Kruzya.TelegramBot.CustomChatAddOns.Handler +{ + public class Config : CommonHandler + { + private readonly CustomChatHandler _option; + private readonly UserService _userService; + + public Config(CustomChatHandler option, UserService userService, CoreContext db) : base(db) + { + _option = option; + _userService = userService; + + } + + private bool IsAllowedChatType(ChatType chatType) + { + // TODO: move to class constant. + var allowedChatTypes = new[] { ChatType.Supergroup, ChatType.Group, ChatType.Channel }; + return allowedChatTypes.Contains(chatType); + } + + [ParametrizedCommand("set_custom_handler")] + public async Task Handle(Uri endpoint) + { + if (!IsAllowedChatType(Chat.Type)) + { + return; + } + + if (!await Bot.IsUserAdminAsync(Chat, From)) + { + await Reply("Sorry, you cannot do this"); + return; + } + + // URLs to somewhere in local bot network should be allowed for using only super-admins. + // It may be bypassed via using, for example, "kitties" instead of "kitties.bot.svc.cluster.local". Maybe check resulting IP? + if (endpoint.Host.EndsWith(".local") && !_userService.IsUserSuper(From.Id)) + { + await Reply("Sorry, you cannot do this"); + return; + } + + // TODO: perform a "ping" request to endpoint. "Ping" should contain chat id and user id who performs this action. + await _option.SetValueAsync(Chat.Id, endpoint.ToString()); + await Reply("Done, configured"); + } + } +} diff --git a/CustomChatAddOns/Options/CustomChatHandler.cs b/CustomChatAddOns/Options/CustomChatHandler.cs new file mode 100644 index 0000000..0e47120 --- /dev/null +++ b/CustomChatAddOns/Options/CustomChatHandler.cs @@ -0,0 +1,19 @@ +using Kruzya.TelegramBot.Core.Options; + +namespace Kruzya.TelegramBot.CustomChatAddOns.Options +{ + public class CustomChatHandler : AbstractOption + { + public CustomChatHandler(IOptionProvider optionProvider) : base(optionProvider) + { + } + + public override string OptionId => "customChatHandlerUrl"; + + public override string OptionName => "Custom Chat Handler"; + + public override OptionType OptionType => OptionType.Hidden; + + public override string DefaultValue => ""; + } +} diff --git a/Kruzya.TelegramBot.sln b/Kruzya.TelegramBot.sln index 3dc09f6..f7c396b 100644 --- a/Kruzya.TelegramBot.sln +++ b/Kruzya.TelegramBot.sln @@ -33,7 +33,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DebugTools", "modules\Debug EndProject 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}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StickerTools", "modules\StickerTools\StickerTools.csproj", "{1D4FE1FA-F7FB-4F9E-95A3-EE761F29B159}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CustomChatAddOns", "CustomChatAddOns\CustomChatAddOns.csproj", "{5632DD5E-620D-4324-99FA-7B189932FCAC}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -101,6 +103,10 @@ Global {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 + {5632DD5E-620D-4324-99FA-7B189932FCAC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5632DD5E-620D-4324-99FA-7B189932FCAC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5632DD5E-620D-4324-99FA-7B189932FCAC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5632DD5E-620D-4324-99FA-7B189932FCAC}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -119,6 +125,7 @@ Global {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} + {5632DD5E-620D-4324-99FA-7B189932FCAC} = {C7821F15-DEDD-474F-A575-A296D4B58F10} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {5BA73C3B-D5FC-4942-9091-504325CDC308}