From 634a9292dc3c7db1961e9673de1ffa40be8e11a1 Mon Sep 17 00:00:00 2001 From: FrigaT Date: Fri, 5 Dec 2025 19:45:59 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D1=8C=20=D0=BE=D1=82=D1=81=D1=8B=D0=BB=D0=B0=D1=82?= =?UTF-8?q?=D1=8C=20=D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?=20=D0=B2=20=D0=B4=D1=80=D1=83=D0=B3=D0=B8=D0=B5=20=D1=87=D0=B0?= =?UTF-8?q?=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Abstractions/IMessengerAdapter.cs | 6 ++-- BotPages.Core/Context/PageContext.cs | 4 +-- BotPages.Telegram/TelegramAdapter.cs | 33 ++++++++++++------- BotPages.Telegram/TelegramAlbumBuilder.cs | 4 +-- BotPages.Telegram/TelegramUpdateMapper.cs | 9 ----- 5 files changed, 29 insertions(+), 27 deletions(-) diff --git a/BotPages.Core/Abstractions/IMessengerAdapter.cs b/BotPages.Core/Abstractions/IMessengerAdapter.cs index b1a1b99..937a404 100644 --- a/BotPages.Core/Abstractions/IMessengerAdapter.cs +++ b/BotPages.Core/Abstractions/IMessengerAdapter.cs @@ -9,17 +9,19 @@ namespace BotPages.Core.Abstractions; /// public interface IMessengerAdapter { + Capabilities Capabilities { get; } + /// /// Отправить текстовое сообщение в чат. /// - Task SendTextAsync(PageContext ctx, string text, MessageFormat format, + Task SendTextAsync(string chatId, string text, MessageFormat format, IEnumerable>? inline, IEnumerable>? reply, CancellationToken ct); /// /// Отправить файл в чат. /// - Task SendFileAsync(PageContext ctx, FileDescriptor file, string? caption, CancellationToken ct); + Task SendFileAsync(string chatId, FileDescriptor file, string? caption, CancellationToken ct); /// /// Создать билдер альбома для отправки медиагруппы. diff --git a/BotPages.Core/Context/PageContext.cs b/BotPages.Core/Context/PageContext.cs index 9a88de7..0baa02c 100644 --- a/BotPages.Core/Context/PageContext.cs +++ b/BotPages.Core/Context/PageContext.cs @@ -49,13 +49,13 @@ public sealed class PageContext IEnumerable>? inline = null, IEnumerable>? reply = null, CancellationToken ct = default) - => Adapter.SendTextAsync(this, text, format, inline, reply, ct); + => Adapter.SendTextAsync(this.Update.Chat.Id, text, format, inline, reply, ct); /// /// Отправить файл. /// public Task SendFileAsync(FileDescriptor file, string? caption = null, CancellationToken ct = default) - => Adapter.SendFileAsync(this, file, caption, ct); + => Adapter.SendFileAsync(this.Update.Chat.Id, file, caption, ct); /// /// Получить билдер альбомов. diff --git a/BotPages.Telegram/TelegramAdapter.cs b/BotPages.Telegram/TelegramAdapter.cs index 3c3b94e..e99db2f 100644 --- a/BotPages.Telegram/TelegramAdapter.cs +++ b/BotPages.Telegram/TelegramAdapter.cs @@ -27,6 +27,17 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup private TelegramBotClient? _client; private string _token; private string _messagerType; + private static Capabilities _capabilities = new() + { + SupportsInlineButtons = true, + SupportsReplyButtons = true, + SupportsAlbums = true, + SupportsFormattingMarkdown = true, + SupportsFormattingHtml = true, + MaxMessageLength = 4096, + }; + + public Capabilities Capabilities => _capabilities; /// Создать адаптер Telegram. public TelegramAdapter(ILogger logger, string token) @@ -73,7 +84,7 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup } /// - public async Task SendTextAsync(PageContext ctx, string text, MessageFormat format, + public async Task SendTextAsync(string chatId, string text, MessageFormat format, IEnumerable>? inline, IEnumerable>? reply, CancellationToken ct) { @@ -129,14 +140,14 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup } // Длина сообщения - if (text.Length > ctx.Update.Chat.Capabilities.MaxMessageLength) + if (text.Length > Capabilities.MaxMessageLength) { - _logger.Log(LogLevel.Warn, $"Message too long ({text.Length}). Truncated to {ctx.Update.Chat.Capabilities.MaxMessageLength}."); - text = text.Substring(0, ctx.Update.Chat.Capabilities.MaxMessageLength); + _logger.Log(LogLevel.Warn, $"Message too long ({text.Length}). Truncated to {Capabilities.MaxMessageLength}."); + text = text.Substring(0, Capabilities.MaxMessageLength); } await _client.SendMessage( - chatId: long.Parse(ctx.Update.Chat.Id), + chatId: long.Parse(chatId), text: text, parseMode: parseMode, replyMarkup: markup, @@ -145,7 +156,7 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup } /// - public async Task SendFileAsync(PageContext ctx, FileDescriptor file, string? caption, CancellationToken ct) + public async Task SendFileAsync(string chatId, FileDescriptor file, string? caption, CancellationToken ct) { if (_client is null) { @@ -153,8 +164,6 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup return; } - var chatId = long.Parse(ctx.Update.Chat.Id); - // Получаем поток, если он задан Stream? stream = null; if (file.GetStreamAsync is not null) @@ -182,16 +191,16 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup switch (file.Kind) { case FileKind.Photo: - await _client.SendPhoto(chatId, inputFile, caption ?? "", cancellationToken: ct); + await _client.SendPhoto(long.Parse(chatId), inputFile, caption ?? "", cancellationToken: ct); break; case FileKind.Video: - await _client.SendVideo(chatId, inputFile, caption: caption ?? "", cancellationToken: ct); + await _client.SendVideo(long.Parse(chatId), inputFile, caption: caption ?? "", cancellationToken: ct); break; case FileKind.Audio: - await _client.SendAudio(chatId, inputFile, caption ?? "", cancellationToken: ct); + await _client.SendAudio(long.Parse(chatId), inputFile, caption ?? "", cancellationToken: ct); break; default: - await _client.SendDocument(chatId, inputFile, caption ?? "", cancellationToken: ct); + await _client.SendDocument(long.Parse(chatId), inputFile, caption ?? "", cancellationToken: ct); break; } } diff --git a/BotPages.Telegram/TelegramAlbumBuilder.cs b/BotPages.Telegram/TelegramAlbumBuilder.cs index 2dc5f20..728e25b 100644 --- a/BotPages.Telegram/TelegramAlbumBuilder.cs +++ b/BotPages.Telegram/TelegramAlbumBuilder.cs @@ -53,7 +53,7 @@ public sealed class TelegramAlbumBuilder : IAlbumBuilder { _logger.Log(LogLevel.Warn, "Albums not supported. Degraded to sequential sends."); foreach (var (file, caption) in _items) - await _adapter.SendFileAsync(_ctx, file, caption, ct); + await _adapter.SendFileAsync(_ctx.Update.Chat.Id, file, caption, ct); return; } @@ -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, file, caption, ct); + await _adapter.SendFileAsync(_ctx.Update.Chat.Id, file, caption, ct); } } diff --git a/BotPages.Telegram/TelegramUpdateMapper.cs b/BotPages.Telegram/TelegramUpdateMapper.cs index f1dfabb..cb737ba 100644 --- a/BotPages.Telegram/TelegramUpdateMapper.cs +++ b/BotPages.Telegram/TelegramUpdateMapper.cs @@ -34,15 +34,6 @@ public static class TelegramUpdateMapper { Id = chat?.Id.ToString() ?? "unknown", Title = chat?.Title, - Capabilities = new Capabilities - { - SupportsInlineButtons = true, - SupportsReplyButtons = true, - SupportsAlbums = true, - SupportsFormattingMarkdown = true, - SupportsFormattingHtml = true, - MaxMessageLength = 4096, - } }; string? text = null;