mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
add validation for configured URL
This commit is contained in:
@@ -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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user