From d97fcaaa20fbf4d0579450429cce11f6689ca980 Mon Sep 17 00:00:00 2001 From: FrigaT Date: Fri, 5 Dec 2025 20:13:27 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=D0=BD=20=D1=84=D0=BE=D1=80=D0=BC=D0=B0=D1=82=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=B4=D0=BF=D0=B8=D1=81=D0=B8=20=D1=84=D0=B0=D0=B9=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BotPages.Core/Abstractions/IAlbumBuilder.cs | 2 +- .../Abstractions/IMessengerAdapter.cs | 2 +- BotPages.Core/Context/PageContext.cs | 8 +++++++- BotPages.Core/Messaging/MessageBuilder.cs | 20 +++++++++---------- BotPages.Telegram/TelegramAlbumBuilder.cs | 14 ++++++------- 5 files changed, 26 insertions(+), 20 deletions(-) diff --git a/BotPages.Core/Abstractions/IAlbumBuilder.cs b/BotPages.Core/Abstractions/IAlbumBuilder.cs index 4566fc8..a2718f3 100644 --- a/BotPages.Core/Abstractions/IAlbumBuilder.cs +++ b/BotPages.Core/Abstractions/IAlbumBuilder.cs @@ -6,7 +6,7 @@ public interface IAlbumBuilder { /// Добавить элемент в альбом. - IAlbumBuilder Add(FileDescriptor file, string? caption = null); + IAlbumBuilder Add(FileDescriptor file, string? caption = null, MessageFormat? captionFormat = null); /// Отправить альбом. Task SendAsync(CancellationToken ct = default); } \ No newline at end of file diff --git a/BotPages.Core/Abstractions/IMessengerAdapter.cs b/BotPages.Core/Abstractions/IMessengerAdapter.cs index 937a404..e28b82d 100644 --- a/BotPages.Core/Abstractions/IMessengerAdapter.cs +++ b/BotPages.Core/Abstractions/IMessengerAdapter.cs @@ -21,7 +21,7 @@ public interface IMessengerAdapter /// /// Отправить файл в чат. /// - Task SendFileAsync(string chatId, FileDescriptor file, string? caption, CancellationToken ct); + Task SendFileAsync(string chatId, FileDescriptor file, string? caption, MessageFormat? captionFormat, CancellationToken ct); /// /// Создать билдер альбома для отправки медиагруппы. diff --git a/BotPages.Core/Context/PageContext.cs b/BotPages.Core/Context/PageContext.cs index 0baa02c..2b4b008 100644 --- a/BotPages.Core/Context/PageContext.cs +++ b/BotPages.Core/Context/PageContext.cs @@ -51,11 +51,17 @@ public sealed class PageContext CancellationToken ct = default) => Adapter.SendTextAsync(this.Update.Chat.Id, text, format, inline, reply, ct); + /// + /// Отправить файл. + /// + public Task SendFileAsync(FileDescriptor file, string? caption = null, MessageFormat? captionFormat = null, CancellationToken ct = default) + => Adapter.SendFileAsync(this.Update.Chat.Id, file, caption, captionFormat, ct); + /// /// Отправить файл. /// public Task SendFileAsync(FileDescriptor file, string? caption = null, CancellationToken ct = default) - => Adapter.SendFileAsync(this.Update.Chat.Id, file, caption, ct); + => Adapter.SendFileAsync(this.Update.Chat.Id, file, caption, null, ct); /// /// Получить билдер альбомов. diff --git a/BotPages.Core/Messaging/MessageBuilder.cs b/BotPages.Core/Messaging/MessageBuilder.cs index 5c6c5c0..2e17fc9 100644 --- a/BotPages.Core/Messaging/MessageBuilder.cs +++ b/BotPages.Core/Messaging/MessageBuilder.cs @@ -12,8 +12,8 @@ public sealed class MessageBuilder private MessageFormat _format = MessageFormat.Plain; private readonly List> _inline = new(); private readonly List> _reply = new(); - private readonly List<(FileDescriptor file, string? caption)> _files = new(); - private readonly List<(FileDescriptor file, string? caption)> _album = new(); + private readonly List<(FileDescriptor file, string? caption, MessageFormat? captionFormat)> _files = new(); + private readonly List<(FileDescriptor file, string? caption, MessageFormat? captionFormat)> _album = new(); private string? _progressTitle = null; private int? _progressPercent = null; private string? _progressMessageId = null; @@ -86,16 +86,16 @@ public sealed class MessageBuilder } /// Добавить файл для отправки. - public MessageBuilder File(FileDescriptor file, string? caption = null) + public MessageBuilder File(FileDescriptor file, string? caption = null, MessageFormat? captionFormat = null) { - _files.Add((file, caption)); + _files.Add((file, caption, captionFormat)); return this; } /// Добавить файл в альбом. - public MessageBuilder Album(FileDescriptor file, string? caption = null) + public MessageBuilder Album(FileDescriptor file, string? caption = null, MessageFormat? captionFormat = null) { - _album.Add((file, caption)); + _album.Add((file, caption, captionFormat)); return this; } @@ -117,15 +117,15 @@ public sealed class MessageBuilder } // Файлы - foreach (var (file, caption) in _files) - await _ctx.SendFileAsync(file, caption, ct); + foreach (var (file, caption, captionFormat) in _files) + await _ctx.SendFileAsync(file, caption, captionFormat, ct); // Альбом if (_album.Count > 0) { var builder = _ctx.Albums; - foreach (var (file, caption) in _album) - builder.Add(file, caption); + foreach (var (file, caption, captionFormat) in _album) + builder.Add(file, caption, captionFormat); await builder.SendAsync(ct); } diff --git a/BotPages.Telegram/TelegramAlbumBuilder.cs b/BotPages.Telegram/TelegramAlbumBuilder.cs index 9504539..f204eec 100644 --- a/BotPages.Telegram/TelegramAlbumBuilder.cs +++ b/BotPages.Telegram/TelegramAlbumBuilder.cs @@ -20,7 +20,7 @@ public sealed class TelegramAlbumBuilder : IAlbumBuilder private readonly PageContext _ctx; private readonly ILogger _logger; private readonly TelegramBotClient? _client; - private readonly List<(FileDescriptor file, string? caption)> _items = new(); + private readonly List<(FileDescriptor file, string? caption, MessageFormat? captionFormat)> _items = new(); /// Создать билдер альбома. public TelegramAlbumBuilder(TelegramAdapter adapter, PageContext ctx, ILogger logger, TelegramBotClient? client) @@ -32,9 +32,9 @@ public sealed class TelegramAlbumBuilder : IAlbumBuilder } /// - public IAlbumBuilder Add(FileDescriptor file, string? caption = null) + public IAlbumBuilder Add(FileDescriptor file, string? caption = null, MessageFormat? captionFormat = null) { - _items.Add((file, caption)); + _items.Add((file, caption, captionFormat)); return this; } @@ -52,13 +52,13 @@ public sealed class TelegramAlbumBuilder : IAlbumBuilder if (!_adapter.Capabilities.SupportsAlbums) { _logger.Log(LogLevel.Warn, "Albums not supported. Degraded to sequential sends."); - foreach (var (file, caption) in _items) - await _adapter.SendFileAsync(_ctx.Update.Chat.Id, file, caption, ct); + foreach (var (file, caption, captionFormat) in _items) + await _adapter.SendFileAsync(_ctx.Update.Chat.Id, file, caption, captionFormat, ct); return; } var media = new List(); - foreach (var (file, caption) in _items) + foreach (var (file, caption, captionFormat) in _items) { Stream? stream = null; if (file.GetStreamAsync is not null) @@ -95,7 +95,7 @@ public sealed class TelegramAlbumBuilder : IAlbumBuilder { // Telegram не поддерживает document в альбомах — деградация _logger.Log(LogLevel.Warn, $"Document '{file.Kind}' in album not supported. Sending document separately."); - await _adapter.SendFileAsync(_ctx.Update.Chat.Id, file, caption, ct); + await _adapter.SendFileAsync(_ctx.Update.Chat.Id, file, caption, captionFormat, ct); } }