From 08f56e14c2543e960367ed289729e8da7d98708e Mon Sep 17 00:00:00 2001 From: Leonid Pershin Date: Fri, 31 Jul 2026 08:28:42 +0300 Subject: [PATCH] Refactor TelegramBotService to streamline message sending process Updated the TelegramBotService to replace the SendMessageAsync method with a new NoticeAsync method, improving clarity and functionality. Adjusted related tests to ensure proper handling of message IDs and maintain consistency in message management. This change enhances the overall user experience by simplifying the message notification process. --- .../Notifications/TelegramBotService.cs | 4 ++-- .../Notifications/TelegramBotTests.cs | 21 ++++++++++++------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/backend/src/TeleWave.Application/Notifications/TelegramBotService.cs b/backend/src/TeleWave.Application/Notifications/TelegramBotService.cs index bdb8dfd..5a89960 100644 --- a/backend/src/TeleWave.Application/Notifications/TelegramBotService.cs +++ b/backend/src/TeleWave.Application/Notifications/TelegramBotService.cs @@ -406,11 +406,11 @@ public sealed class TelegramBotService(IAppDbContext dbContext, ITelegramApi api lines.Add($"{mark} {local:HH:mm} — {title}"); } - await api.SendMessageAsync( + await NoticeAsync( settings, + subscriber, chatId, string.Join('\n', lines), - null, cancellationToken ); } diff --git a/backend/tests/TeleWave.Application.Tests/Notifications/TelegramBotTests.cs b/backend/tests/TeleWave.Application.Tests/Notifications/TelegramBotTests.cs index 6715952..e7d090c 100644 --- a/backend/tests/TeleWave.Application.Tests/Notifications/TelegramBotTests.cs +++ b/backend/tests/TeleWave.Application.Tests/Notifications/TelegramBotTests.cs @@ -212,13 +212,14 @@ public class TelegramBotTests var api = Substitute.For(); string? sent = null; - await api.SendMessageAsync( - Arg.Any(), - Arg.Any(), - Arg.Do(text => sent = text), - Arg.Any>?>(), - Arg.Any() - ); + api.SendMessageAsync( + Arg.Any(), + Arg.Any(), + Arg.Do(text => sent = text), + Arg.Any>?>(), + Arg.Any() + ) + .Returns(4242L); await using var db = fixture.New(); await new TelegramBotService(db, api).HandleAsync( @@ -230,6 +231,12 @@ public class TelegramBotTests Assert.NotNull(sent); Assert.Contains("Симпсоны", sent, StringComparison.Ordinal); + // Программа — одноразовое сообщение: её идентификатор запомнен, чтобы снять при следующем + // показе. Без этого чат копит одинаковые программы на разное время. + await using (var check = fixture.New()) + { + Assert.Equal(4242L, check.TelegramSubscribers.Single().NoticeMessageId); + } Assert.Contains("Футурама", sent, StringComparison.Ordinal); // Две подряд идущие серии Симпсонов дают одну строку, а не две. Assert.Equal(1, sent!.Split("Симпсоны").Length - 1);