From fbe18bbb775d4060e3ca4aa05b044a1769fa1e9a Mon Sep 17 00:00:00 2001 From: kislovdm Date: Wed, 11 Mar 2026 09:03:34 +0300 Subject: [PATCH] image --- PepperBot/Domain/Deal.cs | 1 + .../Infrastructure/Pepper/PepperClient.cs | 4 +++ .../Telegram/TelegramNotifier.cs | 34 ++++++++++++++++++- 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/PepperBot/Domain/Deal.cs b/PepperBot/Domain/Deal.cs index 7c6dcff..a3d89c2 100644 --- a/PepperBot/Domain/Deal.cs +++ b/PepperBot/Domain/Deal.cs @@ -10,6 +10,7 @@ public class Deal public DateTime CreatedAt { get; set; } public long CreatedAtInMillis { get; set; } public string DealUrl { get; set; } = string.Empty; + public string ImageMedium { get; set; } = string.Empty; public string Permalink { get; set; } = string.Empty; public string StoreName { get; set; } = string.Empty; public string StorePermalink { get; set; } = string.Empty; diff --git a/PepperBot/Infrastructure/Pepper/PepperClient.cs b/PepperBot/Infrastructure/Pepper/PepperClient.cs index a2bb10e..828743c 100644 --- a/PepperBot/Infrastructure/Pepper/PepperClient.cs +++ b/PepperBot/Infrastructure/Pepper/PepperClient.cs @@ -81,6 +81,7 @@ public class PepperClient : IPepperClient CreatedAt = dto.CreatedAt ?? DateTimeOffset.FromUnixTimeMilliseconds(dto.CreatedAtInMillis).UtcDateTime, CreatedAtInMillis = dto.CreatedAtInMillis, DealUrl = dto.DealUrl ?? string.Empty, + ImageMedium = dto.ImageMedium ?? string.Empty, Permalink = dto.Permalink ?? string.Empty, StoreName = dto.Store?.Name ?? string.Empty, StorePermalink = dto.Store?.Permalink ?? string.Empty @@ -119,6 +120,9 @@ public class PepperClient : IPepperClient [JsonPropertyName("deal_url")] public string? DealUrl { get; set; } + [JsonPropertyName("image_medium")] + public string? ImageMedium { get; set; } + [JsonPropertyName("permalink")] public string? Permalink { get; set; } diff --git a/PepperBot/Infrastructure/Telegram/TelegramNotifier.cs b/PepperBot/Infrastructure/Telegram/TelegramNotifier.cs index 1852ca2..e79abe9 100644 --- a/PepperBot/Infrastructure/Telegram/TelegramNotifier.cs +++ b/PepperBot/Infrastructure/Telegram/TelegramNotifier.cs @@ -288,9 +288,22 @@ public class TelegramNotifier : ITelegramNotifier try { + var text = textBuilder.ToString(); + + if (!string.IsNullOrWhiteSpace(deal.ImageMedium) + && Uri.TryCreate(deal.ImageMedium, UriKind.Absolute, out var imageUri)) + { + await _botClient.SendPhoto( + chatId: chatId, + photo: InputFile.FromUri(imageUri), + caption: text, + cancellationToken: cancellationToken); + return; + } + await _botClient.SendMessage( chatId: chatId, - text: textBuilder.ToString(), + text: text, cancellationToken: cancellationToken); } catch (ApiRequestException ex) @@ -301,6 +314,25 @@ public class TelegramNotifier : ITelegramNotifier chatId, ex.ErrorCode, ex.Message); + + // Фолбэк: если фото не отправилось (например, Telegram не смог скачать URL), + // пробуем хотя бы текст. + try + { + await _botClient.SendMessage( + chatId: chatId, + text: textBuilder.ToString(), + cancellationToken: cancellationToken); + } + catch (ApiRequestException fallbackEx) + { + Logger.Warn( + fallbackEx, + "Не удалось отправить фолбэк-текст в Telegram (чат {ChatId}): [{Code}] {Message}", + chatId, + fallbackEx.ErrorCode, + fallbackEx.Message); + } } }