diff --git a/src/LinkTracker.Bot/Controllers/UpdatesController.cs b/src/LinkTracker.Bot/Controllers/UpdatesController.cs index d5a197e..75f5bdc 100644 --- a/src/LinkTracker.Bot/Controllers/UpdatesController.cs +++ b/src/LinkTracker.Bot/Controllers/UpdatesController.cs @@ -1,5 +1,5 @@ using Microsoft.AspNetCore.Mvc; -using Telegram.Bot; +using LinkTracker.Bot.Services.Notifications; using LinkTracker.Shared.Models; namespace LinkTracker.Bot.Controllers; @@ -7,29 +7,19 @@ namespace LinkTracker.Bot.Controllers; [ApiController] [Route("updates")] public class UpdatesController( - ITelegramBotClient botClient, + ILinkUpdateHandler linkUpdateHandler, ILogger logger) : ControllerBase { [HttpPost] - public async Task PostUpdate([FromBody] LinkUpdate update) + public async Task PostUpdate( + [FromBody] LinkUpdate update, + CancellationToken cancellationToken) { logger.LogInformation("Received update for URL: {Url}", update.Url); try { - foreach (var chatId in update.TgChatIds) - { - var message = $"🔔 *Update found!*\n\n" + - $"Source: {update.Url}\n" + - $"Description: {update.Description}"; - - await botClient.SendMessage( - chatId: chatId, - text: message, - parseMode: Telegram.Bot.Types.Enums.ParseMode.Markdown - ); - } - + await linkUpdateHandler.HandleAsync(update, cancellationToken); return Ok(); } catch (Exception ex) @@ -38,4 +28,4 @@ await botClient.SendMessage( return StatusCode(500, "Internal Server Error"); } } -} \ No newline at end of file +} diff --git a/src/LinkTracker.Bot/Program.cs b/src/LinkTracker.Bot/Program.cs index 5f079b4..1042f0b 100644 --- a/src/LinkTracker.Bot/Program.cs +++ b/src/LinkTracker.Bot/Program.cs @@ -2,6 +2,7 @@ using LinkTracker.Bot.Configuration; using LinkTracker.Bot.Repositories; using LinkTracker.Bot.Services; +using LinkTracker.Bot.Services.Notifications; using LinkTracker.Bot.Clients; using Telegram.Bot; using Refit; @@ -29,6 +30,7 @@ builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); +builder.Services.AddSingleton(); builder.Services.AddTransient(); builder.Services.AddTransient(); @@ -42,4 +44,4 @@ app.MapControllers(); -app.Run(); \ No newline at end of file +app.Run(); diff --git a/src/LinkTracker.Bot/Services/Notifications/ILinkUpdateHandler.cs b/src/LinkTracker.Bot/Services/Notifications/ILinkUpdateHandler.cs new file mode 100644 index 0000000..8855dd4 --- /dev/null +++ b/src/LinkTracker.Bot/Services/Notifications/ILinkUpdateHandler.cs @@ -0,0 +1,8 @@ +using LinkTracker.Shared.Models; + +namespace LinkTracker.Bot.Services.Notifications; + +public interface ILinkUpdateHandler +{ + Task HandleAsync(LinkUpdate update, CancellationToken cancellationToken); +} diff --git a/src/LinkTracker.Bot/Services/Notifications/TelegramLinkUpdateHandler.cs b/src/LinkTracker.Bot/Services/Notifications/TelegramLinkUpdateHandler.cs new file mode 100644 index 0000000..64c53ed --- /dev/null +++ b/src/LinkTracker.Bot/Services/Notifications/TelegramLinkUpdateHandler.cs @@ -0,0 +1,34 @@ +using LinkTracker.Shared.Models; +using Telegram.Bot; +using Telegram.Bot.Types.Enums; + +namespace LinkTracker.Bot.Services.Notifications; + +public class TelegramLinkUpdateHandler( + ITelegramBotClient botClient, + ILogger logger) : ILinkUpdateHandler +{ + public async Task HandleAsync(LinkUpdate update, CancellationToken cancellationToken) + { + logger.LogInformation( + "Sending link update notification for URL {Url} to {ChatCount} chats", + update.Url, + update.TgChatIds.Length); + + foreach (var chatId in update.TgChatIds) + { + await botClient.SendMessage( + chatId: chatId, + text: BuildMessage(update), + parseMode: ParseMode.Markdown, + cancellationToken: cancellationToken); + } + } + + private static string BuildMessage(LinkUpdate update) + { + return $"🔔 *Update found!*\n\n" + + $"Source: {update.Url}\n" + + $"Description: {update.Description}"; + } +}