diff --git a/BotPages.Core/Abstractions/IMessengerAdapter.cs b/BotPages.Core/Abstractions/IMessengerAdapter.cs
index e28b82d..5ba9c22 100644
--- a/BotPages.Core/Abstractions/IMessengerAdapter.cs
+++ b/BotPages.Core/Abstractions/IMessengerAdapter.cs
@@ -14,9 +14,12 @@ public interface IMessengerAdapter
///
/// Отправить текстовое сообщение в чат.
///
- Task SendTextAsync(string chatId, string text, MessageFormat format,
+ Task SendTextAsync(string chatId, string text, MessageFormat format,
IEnumerable>? inline,
- IEnumerable>? reply, CancellationToken ct);
+ IEnumerable>? reply,
+ string? messageId,
+ CancellationToken ct
+ );
///
/// Отправить файл в чат.
@@ -28,16 +31,6 @@ public interface IMessengerAdapter
///
IAlbumBuilder CreateAlbumBuilder(PageContext ctx);
- ///
- /// Начать отображение прогресса операции.
- ///
- Task StartProgressAsync(PageContext ctx, string title, CancellationToken ct);
-
- ///
- /// Обновить прогресс операции.
- ///
- Task UpdateProgressAsync(PageContext ctx, string messageId, string title, int percent, CancellationToken ct);
-
///
/// Вызывается при выходе со страницы.
///
diff --git a/BotPages.Core/Context/PageContext.cs b/BotPages.Core/Context/PageContext.cs
index fdebe40..ea28518 100644
--- a/BotPages.Core/Context/PageContext.cs
+++ b/BotPages.Core/Context/PageContext.cs
@@ -27,47 +27,4 @@ public sealed class PageContext
///
public IAlbumBuilder Albums => Adapter.CreateAlbumBuilder(this);
- ///
- /// Начать прогресс операции.
- ///
- public async Task StartProgressAsync(string title, CancellationToken ct)
- {
- var messageId = await Adapter.StartProgressAsync(this, title, ct);
-
- if (messageId != null)
- {
- _progressMessageId = messageId;
- _progressTitle = title;
- }
-
- return messageId;
- }
-
- ///
- /// Обновить прогресс операции.
- ///
- public Task UpdateProgressAsync(int percent, CancellationToken ct)
- {
- if (_progressMessageId != null)
- {
- return Adapter.UpdateProgressAsync(this, _progressMessageId, _progressTitle ?? "", percent, ct);
- }
- else
- {
- return Task.CompletedTask;
- }
- }
-
- ///
- /// Обновить прогресс операции.
- ///
- public Task UpdateProgressAsync(string messageId, int percent, CancellationToken ct)
- {
- return Adapter.UpdateProgressAsync(this, messageId, _progressTitle ?? "", percent, ct);
- }
-
-
- private string? _progressMessageId = null;
- private string? _progressTitle = null;
-
}
\ No newline at end of file
diff --git a/BotPages.Core/Context/PageContextAdapterExtensions.cs b/BotPages.Core/Context/PageContextAdapterExtensions.cs
index 246f52d..4024791 100644
--- a/BotPages.Core/Context/PageContextAdapterExtensions.cs
+++ b/BotPages.Core/Context/PageContextAdapterExtensions.cs
@@ -11,11 +11,12 @@ public static class PageContextAdapterExtensions
///
/// Отправить текстовое сообщение.
///
- public static Task SendTextAsync(this PageContext ctx, string text, MessageFormat format = MessageFormat.Plain,
+ public static Task SendTextAsync(this PageContext ctx, string text, MessageFormat format = MessageFormat.Plain,
IEnumerable>? inline = null,
IEnumerable>? reply = null,
+ string? messageId = null,
CancellationToken ct = default)
- => ctx.Adapter.SendTextAsync(ctx.Update.Chat.Id, text, format, inline, reply, ct);
+ => ctx.Adapter.SendTextAsync(ctx.Update.Chat.Id, text, format, inline, reply, messageId, ct);
///
/// Отправить файл.
diff --git a/BotPages.Core/Messaging/MessageBuilder.cs b/BotPages.Core/Messaging/MessageBuilder.cs
index c32ef0b..c401f06 100644
--- a/BotPages.Core/Messaging/MessageBuilder.cs
+++ b/BotPages.Core/Messaging/MessageBuilder.cs
@@ -14,10 +14,8 @@ public sealed class MessageBuilder
private readonly List> _reply = 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;
private bool _disableReplyKeyboard;
+ private string? _editMessageId = null;
/// Создать билдер сообщений.
public MessageBuilder(PageContext ctx) => _ctx = ctx;
@@ -30,6 +28,13 @@ public sealed class MessageBuilder
return this;
}
+ /// Редактировать сообщение.
+ public MessageBuilder EditMessage(string messagId)
+ {
+ _editMessageId = messagId;
+ return this;
+ }
+
/// Добавить inline‑кнопку.
public MessageBuilder Inline(string label, string value)
{
@@ -113,17 +118,11 @@ public sealed class MessageBuilder
return this;
}
- /// Установить прогресс операции.
- public MessageBuilder Progress(string title, int percent = 0)
- {
- _progressTitle = title;
- _progressPercent = percent;
- return this;
- }
-
/// Отправить собранное сообщение.
- public async Task SendAsync(CancellationToken ct = default)
+ public async Task SendAsync(CancellationToken ct = default)
{
+ string? messageId = null;
+
// Текст
if (!string.IsNullOrWhiteSpace(_text))
{
@@ -131,7 +130,7 @@ public sealed class MessageBuilder
if (_disableReplyKeyboard) reply = new();
else if (_reply.Any()) reply = _reply;
- await _ctx.SendTextAsync(_text, _format, _inline, reply, ct);
+ messageId = await _ctx.SendTextAsync(_text, _format, _inline, reply, _editMessageId, ct);
}
// Файлы
@@ -147,25 +146,12 @@ public sealed class MessageBuilder
await builder.SendAsync(ct);
}
- // Прогресс
- if (_progressTitle is not null)
- {
- if (_progressMessageId is null)
- _progressMessageId = await _ctx.StartProgressAsync(_progressTitle, ct);
-
- if (_progressPercent > 0 && !string.IsNullOrEmpty(_progressMessageId))
- await _ctx.UpdateProgressAsync(_progressMessageId, _progressPercent.Value, ct);
- }
-
_text = null;
_files.Clear();
_album.Clear();
- if (_progressPercent >= 100)
- {
- _progressTitle = null;
- _progressMessageId = null;
- _progressPercent = null;
- }
+ _editMessageId = null;
+
+ return messageId;
}
}
diff --git a/BotPages.Telegram/TelegramAdapter.cs b/BotPages.Telegram/TelegramAdapter.cs
index 495a254..62065a4 100644
--- a/BotPages.Telegram/TelegramAdapter.cs
+++ b/BotPages.Telegram/TelegramAdapter.cs
@@ -85,21 +85,24 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup
}
///
- public async Task SendTextAsync(string chatId, string text, MessageFormat format,
+ public async Task SendTextAsync(string chatId, string text, MessageFormat format,
IEnumerable>? inline,
- IEnumerable>? reply, CancellationToken ct)
+ IEnumerable>? reply,
+ string? messageId,
+ CancellationToken ct)
{
if (_client is null)
{
_logger.Log(LogLevel.Critical, $"{MessengerType} client is not initialized.");
- return;
+ return null;
}
+ InlineKeyboardMarkup? inlineMarkup = null;
ReplyMarkup? markup = null;
if (inline is not null && inline.Any())
{
- markup = new InlineKeyboardMarkup(
+ inlineMarkup = new InlineKeyboardMarkup(
inline.Select(row => row.Select(b => new InlineKeyboardButton(b.Label, b.Value)).ToArray())
.ToArray()
);
@@ -154,13 +157,33 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup
text = text.Substring(0, Capabilities.MaxMessageLength);
}
- await _client.SendMessage(
- chatId: long.Parse(chatId),
- text: text,
- parseMode: parseMode,
- replyMarkup: markup,
- cancellationToken: ct
- );
+ if (!string.IsNullOrWhiteSpace(messageId))
+ {
+ await _client.EditMessageText(
+ messageId: int.Parse(messageId),
+ chatId: long.Parse(chatId),
+ text: text,
+ parseMode: parseMode,
+ replyMarkup: inlineMarkup,
+ cancellationToken: ct
+ );
+
+ return messageId;
+ }
+ else
+ {
+ if (inlineMarkup is not null) markup = inlineMarkup;
+
+ var message = await _client.SendMessage(
+ chatId: long.Parse(chatId),
+ text: text,
+ parseMode: parseMode,
+ replyMarkup: markup,
+ cancellationToken: ct
+ );
+
+ return message.Id.ToString();
+ }
}
///
@@ -247,62 +270,6 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup
///
public IAlbumBuilder CreateAlbumBuilder(PageContext ctx) => new TelegramAlbumBuilder(this, ctx, _logger, _client);
- ///
- public async Task StartProgressAsync(PageContext ctx, string title, CancellationToken ct)
- {
- if (_client is null)
- {
- _logger.Log(LogLevel.Critical, $"{MessengerType} client is not initialized.");
- return null;
- }
-
- string text = "0%";
- if (!string.IsNullOrEmpty(title))
- {
- text = title + Environment.NewLine + text;
- }
-
- var message = await _client.SendMessage(
- chatId: long.Parse(ctx.Update.Chat.Id),
- text: text,
- cancellationToken: ct
- );
-
- return message.Id.ToString();
- }
-
- ///
- public async Task UpdateProgressAsync(PageContext ctx, string messageId, string title, int percent, CancellationToken ct)
- {
- if (_client is null)
- {
- _logger.Log(LogLevel.Critical, $"{MessengerType} client is not initialized.");
- return;
- }
-
- percent = Math.Clamp(percent, 0, 100);
-
- string text = $"{percent}%";
- if (!string.IsNullOrEmpty(title))
- {
- text = title + Environment.NewLine + text;
- }
-
- try
- {
- await _client.EditMessageText(
- messageId: int.Parse(messageId),
- chatId: long.Parse(ctx.Update.Chat.Id),
- text: text,
- cancellationToken: ct
- );
- }
- catch (Exception ex)
- {
- _logger.Log(LogLevel.Critical, ex.Message, ex);
- }
- }
-
///
public Task OnLeaveAsync(PageContext ctx, CancellationToken ct) => Task.CompletedTask;
}
\ No newline at end of file
diff --git a/Demo/Pages/SubmitPage.cs b/Demo/Pages/SubmitPage.cs
index 830371b..1f3ce75 100644
--- a/Demo/Pages/SubmitPage.cs
+++ b/Demo/Pages/SubmitPage.cs
@@ -12,8 +12,8 @@ public sealed class SubmitPage : SingletonPage
{
var progress = new MessageBuilder(ctx);
- await progress
- .Progress("Отправка заявки", 7)
+ var messageId = await progress
+ .Text("Отправка заявки\n7%")
.SendAsync(ct);
int i = 7;
@@ -22,7 +22,8 @@ public sealed class SubmitPage : SingletonPage
i += 25;
Thread.Sleep(TimeSpan.FromMilliseconds(200));
await progress
- .Progress("Отправка заявки", i)
+ .Text($"Отправка заявки\n{i}%")
+ .EditMessage(messageId!)
.SendAsync(ct);
}
while (i < 100);