add validation for configured URL

This commit is contained in:
2024-05-29 00:59:55 +03:00
parent 621ce7fbce
commit ec13a3be35
3 changed files with 65 additions and 4 deletions
+23 -3
View File
@@ -3,8 +3,12 @@ using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions;
using Kruzya.TelegramBot.Core.Handler;
using Kruzya.TelegramBot.Core.Service;
using Kruzya.TelegramBot.CustomChatAddOns.Data;
using Kruzya.TelegramBot.CustomChatAddOns.Options;
using Kruzya.TelegramBot.CustomChatAddOns.Service;
using System.Net.NetworkInformation;
using Telegram.Bot.Types.Enums;
using Ping = Kruzya.TelegramBot.CustomChatAddOns.Data.Ping;
namespace Kruzya.TelegramBot.CustomChatAddOns.Handler
{
@@ -12,12 +16,13 @@ namespace Kruzya.TelegramBot.CustomChatAddOns.Handler
{
private readonly CustomChatHandler _option;
private readonly UserService _userService;
private readonly InteropService _interopService;
public Config(CustomChatHandler option, UserService userService, CoreContext db) : base(db)
public Config(CustomChatHandler option, UserService userService, InteropService interopService, CoreContext db) : base(db)
{
_option = option;
_userService = userService;
_interopService = interopService;
}
private bool IsAllowedChatType(ChatType chatType)
@@ -49,7 +54,22 @@ namespace Kruzya.TelegramBot.CustomChatAddOns.Handler
return;
}
// TODO: perform a "ping" request to endpoint. "Ping" should contain chat id and user id who performs this action.
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;
}
await _option.SetValueAsync(Chat.Id, endpoint.ToString());
await Reply("Done, configured");
}