Files
BotPages/BotPages.Core/Messaging/MessageBuilder.cs
FrigaT d97fcaaa20
All checks were successful
CI / build-test (push) Successful in 35s
Release / pack-and-publish (release) Successful in 38s
Доработан формат подписи файлов
2025-12-05 20:13:27 +03:00

154 lines
5.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using BotPages.Core.Abstractions;
namespace BotPages.Core.Messaging;
/// <summary>
/// Fluentбилдер для отправки сообщений (текст, кнопки, файлы, альбомы, прогресс).
/// </summary>
public sealed class MessageBuilder
{
private readonly PageContext _ctx;
private string? _text = null;
private MessageFormat _format = MessageFormat.Plain;
private readonly List<List<InlineButton>> _inline = new();
private readonly List<List<ReplyButton>> _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;
/// <summary>Создать билдер сообщений.</summary>
public MessageBuilder(PageContext ctx) => _ctx = ctx;
/// <summary>Текст сообщения.</summary>
public MessageBuilder Text(string text, MessageFormat format = MessageFormat.Plain)
{
_text = text;
_format = format;
return this;
}
/// <summary>Добавить inlineкнопку.</summary>
public MessageBuilder Inline(string label, string value)
{
_inline.Add(new() { new(label, value) });
return this;
}
/// <summary>Добавить inlineкнопку.</summary>
public MessageBuilder Inline(InlineButton button)
{
_inline.Add(new() { button });
return this;
}
/// <summary>Добавить inlineкнопку.</summary>
public MessageBuilder Inline(params InlineButton[] buttons)
{
_inline.Add(buttons.ToList());
return this;
}
/// <summary>Добавить строку inlineкнопок.</summary>
public MessageBuilder Inline(IEnumerable<InlineButton> row)
{
_inline.Add(row.ToList());
return this;
}
/// <summary>Добавить строку inlineкнопок.</summary>
public MessageBuilder Inline(IEnumerable<IEnumerable<InlineButton>> row)
{
_inline.AddRange(row.Select(t => t.ToList()).ToList());
return this;
}
/// <summary>Добавить replyкнопку.</summary>
public MessageBuilder Reply(params ReplyButton[] label)
{
_reply.Add(label.ToList());
return this;
}
/// <summary>Добавить строку replyкнопок.</summary>
public MessageBuilder Reply(IEnumerable<ReplyButton> row)
{
_reply.Add(row.ToList());
return this;
}
/// <summary>Добавить строку replyкнопок.</summary>
public MessageBuilder Reply(IEnumerable<IEnumerable<ReplyButton>> row)
{
_reply.AddRange(row.Select(t => t.ToList()).ToList());
return this;
}
/// <summary>Добавить файл для отправки.</summary>
public MessageBuilder File(FileDescriptor file, string? caption = null, MessageFormat? captionFormat = null)
{
_files.Add((file, caption, captionFormat));
return this;
}
/// <summary>Добавить файл в альбом.</summary>
public MessageBuilder Album(FileDescriptor file, string? caption = null, MessageFormat? captionFormat = null)
{
_album.Add((file, caption, captionFormat));
return this;
}
/// <summary>Установить прогресс операции.</summary>
public MessageBuilder Progress(string title, int percent = 0)
{
_progressTitle = title;
_progressPercent = percent;
return this;
}
/// <summary>Отправить собранное сообщение.</summary>
public async Task SendAsync(CancellationToken ct = default)
{
// Текст
if (!string.IsNullOrWhiteSpace(_text))
{
await _ctx.SendTextAsync(_text, _format, _inline, _reply, 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, captionFormat) in _album)
builder.Add(file, caption, captionFormat);
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;
}
}
}