diff --git a/PepperBot/Application/Interfaces/ITelegramNotifier.cs b/PepperBot/Application/Interfaces/ITelegramNotifier.cs index 2b88485..d855dcf 100644 --- a/PepperBot/Application/Interfaces/ITelegramNotifier.cs +++ b/PepperBot/Application/Interfaces/ITelegramNotifier.cs @@ -12,7 +12,7 @@ public interface ITelegramNotifier /// /// Уведомление о скидке в конкретный Telegram-чат (личная/персональная рассылка). /// - Task NotifyDealToChatAsync(Deal deal, string chatId, CancellationToken cancellationToken); + Task NotifyDealToChatAsync(Deal deal, string chatId, string? selectionRule, CancellationToken cancellationToken); /// /// Запустить обработку входящих сообщений Telegram и регистрацию подписок. diff --git a/PepperBot/Application/Services/DealService.cs b/PepperBot/Application/Services/DealService.cs index b7e8f93..7389188 100644 --- a/PepperBot/Application/Services/DealService.cs +++ b/PepperBot/Application/Services/DealService.cs @@ -85,7 +85,7 @@ public class DealService continue; } - var isMatch = IsMatch(searchableText, keywords); + var isMatch = IsMatch(searchableText, keywords, out var matchedKeyword); Logger.Debug( "Результат фильтрации скидки {DealId} по подписке {SubscriptionId}: {IsMatch}. Текст: \"{Text}\"; ключевые слова: {Keywords}", deal.Id, @@ -96,7 +96,9 @@ public class DealService if (isMatch) { - await _notifier.NotifyDealToChatAsync(deal, subscription.ChatId, cancellationToken); + var selectionRule = + $"совпадение по ключевому слову \"{matchedKeyword}\" (подписка [{subscription.Id}]: {subscription.Keywords})"; + await _notifier.NotifyDealToChatAsync(deal, subscription.ChatId, selectionRule, cancellationToken); } } } @@ -111,8 +113,9 @@ public class DealService .ToList(); } - private static bool IsMatch(string text, List keywords) + private static bool IsMatch(string text, List keywords, out string matchedKeyword) { + matchedKeyword = string.Empty; if (keywords.Count == 0) { return false; @@ -122,6 +125,7 @@ public class DealService { if (text.Contains(keyword, StringComparison.OrdinalIgnoreCase)) { + matchedKeyword = keyword; return true; } } diff --git a/PepperBot/Infrastructure/Telegram/TelegramNotifier.cs b/PepperBot/Infrastructure/Telegram/TelegramNotifier.cs index e79abe9..f32cd98 100644 --- a/PepperBot/Infrastructure/Telegram/TelegramNotifier.cs +++ b/PepperBot/Infrastructure/Telegram/TelegramNotifier.cs @@ -50,17 +50,17 @@ public class TelegramNotifier : ITelegramNotifier return Task.CompletedTask; } - return SendDealAsync(deal, _broadcastChatId, cancellationToken); + return SendDealAsync(deal, _broadcastChatId, selectionRule: null, cancellationToken); } - public Task NotifyDealToChatAsync(Deal deal, string chatId, CancellationToken cancellationToken) + public Task NotifyDealToChatAsync(Deal deal, string chatId, string? selectionRule, CancellationToken cancellationToken) { if (string.IsNullOrWhiteSpace(chatId)) { return Task.CompletedTask; } - return SendDealAsync(deal, chatId, cancellationToken); + return SendDealAsync(deal, chatId, selectionRule, cancellationToken); } public Task StartAsync(CancellationToken cancellationToken) @@ -92,10 +92,8 @@ public class TelegramNotifier : ITelegramNotifier if (update.Type == UpdateType.CallbackQuery) { await HandleCallbackQueryAsync(botClient, update.CallbackQuery!, cancellationToken); - { return; } - } if (update.Type != UpdateType.Message) { @@ -256,11 +254,17 @@ public class TelegramNotifier : ITelegramNotifier return Task.CompletedTask; } - private async Task SendDealAsync(Deal deal, string chatId, CancellationToken cancellationToken) + private async Task SendDealAsync(Deal deal, string chatId, string? selectionRule, CancellationToken cancellationToken) { var textBuilder = new StringBuilder(); textBuilder.AppendLine($"🔥 {deal.Title}"); + if (!string.IsNullOrWhiteSpace(selectionRule)) + { + textBuilder.AppendLine($"Правило отбора: {selectionRule}"); + textBuilder.AppendLine(); + } + if (deal.CurrentPrice is not null) { textBuilder.AppendLine($"Цена: {deal.CurrentPrice:0.##} ₽");