mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
start working on custom chat handlers
This commit is contained in:
@@ -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<CustomChatHandler>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<AssemblyName>TelegramBot.CustomChatAddOns</AssemblyName>
|
||||
<RootNamespace>Kruzya.TelegramBot.CustomChatAddOns</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Core\Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -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
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using Kruzya.TelegramBot.Core.Options;
|
||||
|
||||
namespace Kruzya.TelegramBot.CustomChatAddOns.Options
|
||||
{
|
||||
public class CustomChatHandler : AbstractOption<CustomChatHandler, string>
|
||||
{
|
||||
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 => "";
|
||||
}
|
||||
}
|
||||
@@ -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}
|
||||
|
||||
Reference in New Issue
Block a user