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 => "";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user