Refactor TelegramBotService to streamline message sending process
ci / build-backend (push) Successful in 4m34s
ci / build-frontend (push) Successful in 1m7s
ci / tests (push) Successful in 4m22s
ci / sonar (push) Successful in 8m49s

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.
This commit is contained in:
Leonid Pershin
2026-07-31 08:28:42 +03:00
parent d6bc2b15ac
commit 08f56e14c2
2 changed files with 16 additions and 9 deletions
@@ -406,11 +406,11 @@ public sealed class TelegramBotService(IAppDbContext dbContext, ITelegramApi api
lines.Add($"{mark} {local:HH:mm} — {title}"); lines.Add($"{mark} {local:HH:mm} — {title}");
} }
await api.SendMessageAsync( await NoticeAsync(
settings, settings,
subscriber,
chatId, chatId,
string.Join('\n', lines), string.Join('\n', lines),
null,
cancellationToken cancellationToken
); );
} }
@@ -212,13 +212,14 @@ public class TelegramBotTests
var api = Substitute.For<ITelegramApi>(); var api = Substitute.For<ITelegramApi>();
string? sent = null; string? sent = null;
await api.SendMessageAsync( api.SendMessageAsync(
Arg.Any<TelegramSettings>(), Arg.Any<TelegramSettings>(),
Arg.Any<long>(), Arg.Any<long>(),
Arg.Do<string>(text => sent = text), Arg.Do<string>(text => sent = text),
Arg.Any<IReadOnlyList<IReadOnlyList<TelegramButton>>?>(), Arg.Any<IReadOnlyList<IReadOnlyList<TelegramButton>>?>(),
Arg.Any<CancellationToken>() Arg.Any<CancellationToken>()
); )
.Returns(4242L);
await using var db = fixture.New(); await using var db = fixture.New();
await new TelegramBotService(db, api).HandleAsync( await new TelegramBotService(db, api).HandleAsync(
@@ -230,6 +231,12 @@ public class TelegramBotTests
Assert.NotNull(sent); Assert.NotNull(sent);
Assert.Contains("Симпсоны", sent, StringComparison.Ordinal); Assert.Contains("Симпсоны", sent, StringComparison.Ordinal);
// Программа — одноразовое сообщение: её идентификатор запомнен, чтобы снять при следующем
// показе. Без этого чат копит одинаковые программы на разное время.
await using (var check = fixture.New())
{
Assert.Equal(4242L, check.TelegramSubscribers.Single().NoticeMessageId);
}
Assert.Contains("Футурама", sent, StringComparison.Ordinal); Assert.Contains("Футурама", sent, StringComparison.Ordinal);
// Две подряд идущие серии Симпсонов дают одну строку, а не две. // Две подряд идущие серии Симпсонов дают одну строку, а не две.
Assert.Equal(1, sent!.Split("Симпсоны").Length - 1); Assert.Equal(1, sent!.Split("Симпсоны").Length - 1);