2024-05-29 00:28:17 +03:00
|
|
|
using BotFramework.Attributes;
|
|
|
|
|
using Kruzya.TelegramBot.Core.Data;
|
|
|
|
|
using Kruzya.TelegramBot.Core.Extensions;
|
|
|
|
|
using Kruzya.TelegramBot.Core.Handler;
|
|
|
|
|
using Kruzya.TelegramBot.Core.Service;
|
2024-05-29 00:59:55 +03:00
|
|
|
using Kruzya.TelegramBot.CustomChatAddOns.Data;
|
2024-05-29 00:28:17 +03:00
|
|
|
using Kruzya.TelegramBot.CustomChatAddOns.Options;
|
2024-05-29 00:59:55 +03:00
|
|
|
using Kruzya.TelegramBot.CustomChatAddOns.Service;
|
2024-05-29 00:28:17 +03:00
|
|
|
using Telegram.Bot.Types.Enums;
|
|
|
|
|
|
|
|
|
|
namespace Kruzya.TelegramBot.CustomChatAddOns.Handler
|
|
|
|
|
{
|
|
|
|
|
public class Config : CommonHandler
|
|
|
|
|
{
|
|
|
|
|
private readonly CustomChatHandler _option;
|
|
|
|
|
private readonly UserService _userService;
|
2024-05-29 00:59:55 +03:00
|
|
|
private readonly InteropService _interopService;
|
2024-05-29 00:28:17 +03:00
|
|
|
|
2024-05-29 00:59:55 +03:00
|
|
|
public Config(CustomChatHandler option, UserService userService, InteropService interopService, CoreContext db) : base(db)
|
2024-05-29 00:28:17 +03:00
|
|
|
{
|
|
|
|
|
_option = option;
|
|
|
|
|
_userService = userService;
|
2024-05-29 00:59:55 +03:00
|
|
|
_interopService = interopService;
|
2024-05-29 00:28:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-29 00:59:55 +03:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var ping = Ping.From(Message!);
|
|
|
|
|
var pong = await _interopService.PostMessage<Ping, Pong>(endpoint.ToString(), ping);
|
|
|
|
|
if (pong.Confirmation != ping.Confirmation)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Invalid response");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// catch (HttpRequestException e)
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
await Reply($"Sorry, this URL cannot be set as chat webhook: {e.Message}");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-29 00:28:17 +03:00
|
|
|
await _option.SetValueAsync(Chat.Id, endpoint.ToString());
|
|
|
|
|
await Reply("Done, configured");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|