From f29e72a6f2ca018fd1addf901ac81c124c4e7660 Mon Sep 17 00:00:00 2001 From: FrigaT Date: Tue, 13 Jan 2026 20:23:38 +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=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=20=D1=83=D0=B4=D0=B0?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F=20=D1=81=D0=BE=D0=BE=D0=B1=D1=89?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Abstractions/IMessengerAdapter.cs | 5 +++ .../Context/PageContextAdapterExtensions.cs | 8 +++- BotPages.Core/Messaging/MessageBuilder.cs | 40 ++++++++++++------- BotPages.Telegram/TelegramAdapter.cs | 12 ++++++ Demo/Pages/SubmitPage.cs | 3 +- 5 files changed, 50 insertions(+), 18 deletions(-) diff --git a/BotPages.Core/Abstractions/IMessengerAdapter.cs b/BotPages.Core/Abstractions/IMessengerAdapter.cs index 491832e..1fb9b27 100644 --- a/BotPages.Core/Abstractions/IMessengerAdapter.cs +++ b/BotPages.Core/Abstractions/IMessengerAdapter.cs @@ -18,6 +18,11 @@ public interface IMessengerAdapter /// Task SendAsync(SendRequest request, CancellationToken ct = default); + /// + /// Универсальный метод удаления сообщения. + /// + Task DeleteAsync(string chatId, string messageId, CancellationToken ct = default); + /// /// Создать билдер альбома для отправки медиагруппы. /// diff --git a/BotPages.Core/Context/PageContextAdapterExtensions.cs b/BotPages.Core/Context/PageContextAdapterExtensions.cs index d1426bb..5510f69 100644 --- a/BotPages.Core/Context/PageContextAdapterExtensions.cs +++ b/BotPages.Core/Context/PageContextAdapterExtensions.cs @@ -9,8 +9,14 @@ namespace BotPages.Core; public static class PageContextAdapterExtensions { /// - /// Отправить универсальный запрос через привязанный адаптер. + /// Отправить сообщение универсальным запросом через привязанный адаптер. /// public static Task SendAsync(this PageContext ctx, SendRequest request, CancellationToken ct = default) => ctx.Adapter.SendAsync(request, ct); + + /// + /// Удалить сообщение через привязанный адаптер. + /// + public static Task DeleteAsync(this PageContext ctx, string chatId, string messageId, CancellationToken ct = default) + => ctx.Adapter.DeleteAsync(chatId, messageId, ct); } \ No newline at end of file diff --git a/BotPages.Core/Messaging/MessageBuilder.cs b/BotPages.Core/Messaging/MessageBuilder.cs index 546b03d..bfc2a47 100644 --- a/BotPages.Core/Messaging/MessageBuilder.cs +++ b/BotPages.Core/Messaging/MessageBuilder.cs @@ -16,8 +16,8 @@ public sealed class MessageBuilder private readonly List<(FileDescriptor file, string? caption, MessageFormat? captionFormat)> _files = new(); private readonly List<(FileDescriptor file, string? caption, MessageFormat? captionFormat)> _album = new(); private bool _disableReplyKeyboard; - private string? _editMessageId = null; private AdapterOptionsBag? _adapterOptions = null; + private string? _chatId = null; /// Создать билдер сообщений. public MessageBuilder(PageContext ctx) => _ctx = ctx; @@ -32,6 +32,13 @@ public sealed class MessageBuilder return this; } + /// ID чата куда отрпавить сообщение. + public MessageBuilder ChatId(string chatId) + { + _chatId = chatId; + return this; + } + /// Текст сообщения. public MessageBuilder Text(string text, MessageFormat format = MessageFormat.Plain) { @@ -40,13 +47,6 @@ public sealed class MessageBuilder return this; } - /// Редактировать сообщение. - public MessageBuilder EditMessage(string messagId) - { - _editMessageId = messagId; - return this; - } - /// Добавить inline‑кнопку. public MessageBuilder Inline(string label, string value) { @@ -129,10 +129,20 @@ public sealed class MessageBuilder return this; } + /// Удалить сообщение. + public async Task DeleteAsync(string messageId, CancellationToken ct = default) + { + await _ctx.DeleteAsync(this._chatId ?? _ctx.Update.Chat.Id, messageId, ct); + } + /// Отправить собранное сообщение. public async Task SendAsync(CancellationToken ct = default) + => await SendAsync(string.Empty, ct); + + /// Редактировать сообщение. + public async Task SendAsync(string messageId, CancellationToken ct = default) { - string? messageId = null; + string? outMessageId = null; List>? reply = null; if (_disableReplyKeyboard) reply = new(); @@ -143,16 +153,16 @@ public sealed class MessageBuilder { var req = new SendRequest { - ChatId = _ctx.Update.Chat.Id, + ChatId = _chatId ?? _ctx.Update.Chat.Id, Text = _text, TextFormat = _format, Inline = _inline, Reply = reply, - MessageId = _editMessageId, + MessageId = string.IsNullOrWhiteSpace(messageId) ? null : messageId, AdapterOptions = _adapterOptions }; - messageId = await _ctx.SendAsync(req, ct); + outMessageId = await _ctx.SendAsync(req, ct); } // Файлы @@ -160,7 +170,7 @@ public sealed class MessageBuilder { var req = new SendRequest { - ChatId = _ctx.Update.Chat.Id, + ChatId = _chatId ?? _ctx.Update.Chat.Id, File = file, Caption = caption, CaptionFormat = captionFormat, @@ -171,7 +181,7 @@ public sealed class MessageBuilder var res = await _ctx.SendAsync(req, ct); // сохранить первый возвращённый id сообщения - if (messageId is null && res is not null) messageId = res; + if (outMessageId is null && res is not null) outMessageId = res; } // Альбом @@ -187,8 +197,8 @@ public sealed class MessageBuilder _files.Clear(); _album.Clear(); - _editMessageId = null; _adapterOptions = null; + _chatId = null; return messageId; } diff --git a/BotPages.Telegram/TelegramAdapter.cs b/BotPages.Telegram/TelegramAdapter.cs index 2119af0..6f65992 100644 --- a/BotPages.Telegram/TelegramAdapter.cs +++ b/BotPages.Telegram/TelegramAdapter.cs @@ -137,6 +137,18 @@ public sealed class TelegramAdapter : IMessengerAdapterSetup return null; } + /// + public Task DeleteAsync(string chatId, string messageId, CancellationToken ct = default) + { + if (_client is null) + { + _logger.Log(LogLevel.Critical, $"{MessengerType} client is not initialized."); + return null; + } + + return _client.DeleteMessage(chatId, Convert.ToInt32(messageId), ct); + } + // --- Helpers --- private InlineKeyboardMarkup? BuildInlineMarkup(IEnumerable>? inline) diff --git a/Demo/Pages/SubmitPage.cs b/Demo/Pages/SubmitPage.cs index 0c047db..043a2fb 100644 --- a/Demo/Pages/SubmitPage.cs +++ b/Demo/Pages/SubmitPage.cs @@ -21,8 +21,7 @@ public sealed class SubmitPage : SingletonPage Thread.Sleep(TimeSpan.FromMilliseconds(200)); await new MessageBuilder(ctx) .Text($"Отправка заявки\n{i}%") - .EditMessage(messageId!) - .SendAsync(ct); + .SendAsync(messageId, ct); } while (i < 100);