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:
@@ -16,7 +16,7 @@ namespace Kruzya.TelegramBot.CustomChatAddOns.Data
|
|||||||
[JsonProperty("confirmation")]
|
[JsonProperty("confirmation")]
|
||||||
public string Confirmation { get; set; } = Guid.NewGuid().ToString();
|
public string Confirmation { get; set; } = Guid.NewGuid().ToString();
|
||||||
|
|
||||||
public Ping From(Message message) => new() {
|
public static Ping From(Message message) => new() {
|
||||||
ChatId = message.Chat.Id,
|
ChatId = message.Chat.Id,
|
||||||
UserId = message.From!.Id
|
UserId = message.From!.Id
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -3,8 +3,12 @@ using Kruzya.TelegramBot.Core.Data;
|
|||||||
using Kruzya.TelegramBot.Core.Extensions;
|
using Kruzya.TelegramBot.Core.Extensions;
|
||||||
using Kruzya.TelegramBot.Core.Handler;
|
using Kruzya.TelegramBot.Core.Handler;
|
||||||
using Kruzya.TelegramBot.Core.Service;
|
using Kruzya.TelegramBot.Core.Service;
|
||||||
|
using Kruzya.TelegramBot.CustomChatAddOns.Data;
|
||||||
using Kruzya.TelegramBot.CustomChatAddOns.Options;
|
using Kruzya.TelegramBot.CustomChatAddOns.Options;
|
||||||
|
using Kruzya.TelegramBot.CustomChatAddOns.Service;
|
||||||
|
using System.Net.NetworkInformation;
|
||||||
using Telegram.Bot.Types.Enums;
|
using Telegram.Bot.Types.Enums;
|
||||||
|
using Ping = Kruzya.TelegramBot.CustomChatAddOns.Data.Ping;
|
||||||
|
|
||||||
namespace Kruzya.TelegramBot.CustomChatAddOns.Handler
|
namespace Kruzya.TelegramBot.CustomChatAddOns.Handler
|
||||||
{
|
{
|
||||||
@@ -12,12 +16,13 @@ namespace Kruzya.TelegramBot.CustomChatAddOns.Handler
|
|||||||
{
|
{
|
||||||
private readonly CustomChatHandler _option;
|
private readonly CustomChatHandler _option;
|
||||||
private readonly UserService _userService;
|
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;
|
_option = option;
|
||||||
_userService = userService;
|
_userService = userService;
|
||||||
|
_interopService = interopService;
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool IsAllowedChatType(ChatType chatType)
|
private bool IsAllowedChatType(ChatType chatType)
|
||||||
@@ -49,7 +54,22 @@ namespace Kruzya.TelegramBot.CustomChatAddOns.Handler
|
|||||||
return;
|
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 _option.SetValueAsync(Chat.Id, endpoint.ToString());
|
||||||
await Reply("Done, configured");
|
await Reply("Done, configured");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Serialization;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net.Http.Json;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Telegram.Bot.Types;
|
||||||
|
|
||||||
|
namespace Kruzya.TelegramBot.CustomChatAddOns.Service
|
||||||
|
{
|
||||||
|
public class InteropService
|
||||||
|
{
|
||||||
|
private readonly JsonSerializerSettings _serializerSettings;
|
||||||
|
private readonly HttpClient _client;
|
||||||
|
|
||||||
|
public InteropService(ISerializationBinder serializationBinder, HttpClient client)
|
||||||
|
{
|
||||||
|
_serializerSettings = new()
|
||||||
|
{
|
||||||
|
TypeNameHandling = TypeNameHandling.Objects,
|
||||||
|
SerializationBinder = serializationBinder
|
||||||
|
};
|
||||||
|
|
||||||
|
_client = client;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<TOut> PostMessage<TIn, TOut>(string endpoint, TIn message)
|
||||||
|
{
|
||||||
|
var serializedMessage = JsonConvert.SerializeObject(message, _serializerSettings);
|
||||||
|
var httpContent = new StringContent(serializedMessage, Encoding.UTF8, "application/json");
|
||||||
|
|
||||||
|
var response = await (await _client.PostAsync(endpoint, httpContent))
|
||||||
|
.EnsureSuccessStatusCode()
|
||||||
|
.Content.ReadAsStringAsync();
|
||||||
|
|
||||||
|
return JsonConvert.DeserializeObject<TOut>(response, _serializerSettings)!;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user