154 lines
4.9 KiB
C#
154 lines
4.9 KiB
C#
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)> _files = new();
|
||
private readonly List<(FileDescriptor file, string? caption)> _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)
|
||
{
|
||
_files.Add((file, caption));
|
||
return this;
|
||
}
|
||
|
||
/// <summary>Добавить файл в альбом.</summary>
|
||
public MessageBuilder Album(FileDescriptor file, string? caption = null)
|
||
{
|
||
_album.Add((file, caption));
|
||
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) in _files)
|
||
await _ctx.SendFileAsync(file, caption, ct);
|
||
|
||
// Альбом
|
||
if (_album.Count > 0)
|
||
{
|
||
var builder = _ctx.Albums;
|
||
foreach (var (file, caption) in _album)
|
||
builder.Add(file, caption);
|
||
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;
|
||
}
|
||
}
|
||
}
|