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);