Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 7 additions & 17 deletions src/LinkTracker.Bot/Controllers/UpdatesController.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,25 @@
using Microsoft.AspNetCore.Mvc;
using Telegram.Bot;
using LinkTracker.Bot.Services.Notifications;
using LinkTracker.Shared.Models;

namespace LinkTracker.Bot.Controllers;

[ApiController]
[Route("updates")]
public class UpdatesController(
ITelegramBotClient botClient,
ILinkUpdateHandler linkUpdateHandler,
ILogger<UpdatesController> logger) : ControllerBase
{
[HttpPost]
public async Task<IActionResult> PostUpdate([FromBody] LinkUpdate update)
public async Task<IActionResult> 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)
Expand All @@ -38,4 +28,4 @@ await botClient.SendMessage(
return StatusCode(500, "Internal Server Error");
}
}
}
}
4 changes: 3 additions & 1 deletion src/LinkTracker.Bot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -29,6 +30,7 @@
builder.Services.AddSingleton<IUserRepository, InMemoryUserRepository>();
builder.Services.AddSingleton<UserStateService>();
builder.Services.AddSingleton<CommandDispatcher>();
builder.Services.AddSingleton<ILinkUpdateHandler, TelegramLinkUpdateHandler>();

builder.Services.AddTransient<IBotCommand, StartCommand>();
builder.Services.AddTransient<IBotCommand, HelpCommand>();
Expand All @@ -42,4 +44,4 @@

app.MapControllers();

app.Run();
app.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using LinkTracker.Shared.Models;

namespace LinkTracker.Bot.Services.Notifications;

public interface ILinkUpdateHandler
{
Task HandleAsync(LinkUpdate update, CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
@@ -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<TelegramLinkUpdateHandler> 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}";
}
}
Loading