This commit is contained in:
kislovdm
2026-03-11 13:10:31 +03:00
parent fbe18bbb77
commit 30acb201c6
3 changed files with 18 additions and 10 deletions
@@ -12,7 +12,7 @@ public interface ITelegramNotifier
/// <summary> /// <summary>
/// Уведомление о скидке в конкретный Telegram-чат (личная/персональная рассылка). /// Уведомление о скидке в конкретный Telegram-чат (личная/персональная рассылка).
/// </summary> /// </summary>
Task NotifyDealToChatAsync(Deal deal, string chatId, CancellationToken cancellationToken); Task NotifyDealToChatAsync(Deal deal, string chatId, string? selectionRule, CancellationToken cancellationToken);
/// <summary> /// <summary>
/// Запустить обработку входящих сообщений Telegram и регистрацию подписок. /// Запустить обработку входящих сообщений Telegram и регистрацию подписок.
@@ -85,7 +85,7 @@ public class DealService
continue; continue;
} }
var isMatch = IsMatch(searchableText, keywords); var isMatch = IsMatch(searchableText, keywords, out var matchedKeyword);
Logger.Debug( Logger.Debug(
"Результат фильтрации скидки {DealId} по подписке {SubscriptionId}: {IsMatch}. Текст: \"{Text}\"; ключевые слова: {Keywords}", "Результат фильтрации скидки {DealId} по подписке {SubscriptionId}: {IsMatch}. Текст: \"{Text}\"; ключевые слова: {Keywords}",
deal.Id, deal.Id,
@@ -96,7 +96,9 @@ public class DealService
if (isMatch) 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(); .ToList();
} }
private static bool IsMatch(string text, List<string> keywords) private static bool IsMatch(string text, List<string> keywords, out string matchedKeyword)
{ {
matchedKeyword = string.Empty;
if (keywords.Count == 0) if (keywords.Count == 0)
{ {
return false; return false;
@@ -122,6 +125,7 @@ public class DealService
{ {
if (text.Contains(keyword, StringComparison.OrdinalIgnoreCase)) if (text.Contains(keyword, StringComparison.OrdinalIgnoreCase))
{ {
matchedKeyword = keyword;
return true; return true;
} }
} }
@@ -50,17 +50,17 @@ public class TelegramNotifier : ITelegramNotifier
return Task.CompletedTask; 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)) if (string.IsNullOrWhiteSpace(chatId))
{ {
return Task.CompletedTask; return Task.CompletedTask;
} }
return SendDealAsync(deal, chatId, cancellationToken); return SendDealAsync(deal, chatId, selectionRule, cancellationToken);
} }
public Task StartAsync(CancellationToken cancellationToken) public Task StartAsync(CancellationToken cancellationToken)
@@ -92,10 +92,8 @@ public class TelegramNotifier : ITelegramNotifier
if (update.Type == UpdateType.CallbackQuery) if (update.Type == UpdateType.CallbackQuery)
{ {
await HandleCallbackQueryAsync(botClient, update.CallbackQuery!, cancellationToken); await HandleCallbackQueryAsync(botClient, update.CallbackQuery!, cancellationToken);
{
return; return;
} }
}
if (update.Type != UpdateType.Message) if (update.Type != UpdateType.Message)
{ {
@@ -256,11 +254,17 @@ public class TelegramNotifier : ITelegramNotifier
return Task.CompletedTask; 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(); var textBuilder = new StringBuilder();
textBuilder.AppendLine($"🔥 {deal.Title}"); textBuilder.AppendLine($"🔥 {deal.Title}");
if (!string.IsNullOrWhiteSpace(selectionRule))
{
textBuilder.AppendLine($"Правило отбора: {selectionRule}");
textBuilder.AppendLine();
}
if (deal.CurrentPrice is not null) if (deal.CurrentPrice is not null)
{ {
textBuilder.AppendLine($"Цена: {deal.CurrentPrice:0.##} ₽"); textBuilder.AppendLine($"Цена: {deal.CurrentPrice:0.##} ₽");