diff --git a/CustomChatAddOns/Data/Ping.cs b/CustomChatAddOns/Data/Ping.cs index d6985a7..0346c70 100644 --- a/CustomChatAddOns/Data/Ping.cs +++ b/CustomChatAddOns/Data/Ping.cs @@ -16,7 +16,7 @@ namespace Kruzya.TelegramBot.CustomChatAddOns.Data [JsonProperty("confirmation")] 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, UserId = message.From!.Id }; diff --git a/CustomChatAddOns/Handler/Config.cs b/CustomChatAddOns/Handler/Config.cs index f494771..6aea598 100644 --- a/CustomChatAddOns/Handler/Config.cs +++ b/CustomChatAddOns/Handler/Config.cs @@ -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(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"); } diff --git a/CustomChatAddOns/Service/InteropService.cs b/CustomChatAddOns/Service/InteropService.cs new file mode 100644 index 0000000..c985814 --- /dev/null +++ b/CustomChatAddOns/Service/InteropService.cs @@ -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 PostMessage(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(response, _serializerSettings)!; + } + } +}