Files
BotPages/BotPages.Core/Messaging/MessageBuilder.cs

490 lines
17 KiB
C#
Raw Permalink 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билдер для отправки сообщений (текст, кнопки, файлы, альбомы, прогресс).
/// Поддерживает указание адаптер-специфичных опций через <see cref="WithAdapterOption{T}(string, T)"/>.
/// </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 bool _disableReplyKeyboard;
private AdapterOptionsBag? _adapterOptions = null;
private string? _replyToMessageId = null;
private bool _quoteReply = true;
private string? _quoteTitle = null;
private bool _disableWebPagePreview = false;
private bool _disableNotification = false;
private bool _protectContent = false;
private MessageStyle? _style = null;
private DateTime? _scheduleDate = null;
private string? _topic = null;
private string? _messageId = null;
private IMessengerAdapter? _targetAdapter = null;
private string? _targetChatId = null;
/// <summary>Создать билдер сообщений.</summary>
public MessageBuilder(PageContext ctx) => _ctx = ctx;
/// <summary>
/// Отправить сообщение через указанный адаптер.
/// </summary>
public MessageBuilder WithAdapter(IMessengerAdapter adapter)
{
_targetAdapter = adapter;
return this;
}
/// <summary>
/// Отправить сообщение через адаптер по ID.
/// </summary>
public MessageBuilder WithAdapter(string adapterId)
{
_targetAdapter = _ctx.AdapterFactory.Resolve(adapterId);
return this;
}
/// <summary>
/// Использовать текущий адаптер (по умолчанию).
/// </summary>
public MessageBuilder UseCurrentAdapter()
{
_targetAdapter = null;
return this;
}
/// <summary>
/// Отправить в указанный чат (с указанием адаптера).
/// </summary>
public MessageBuilder ToChat(string chatId, IMessengerAdapter? adapter)
{
_targetChatId = chatId;
_targetAdapter = adapter;
return this;
}
/// <summary>
/// Отправить в указанный чат через адаптер по ID.
/// </summary>
public MessageBuilder ToChat(string chatId, string adapterId)
{
_targetChatId = chatId;
_targetAdapter = _ctx.AdapterFactory.Resolve(adapterId);
return this;
}
/// <summary>
/// Отправить в указанный чат.
/// </summary>
public MessageBuilder ToChat(string chatId)
{
_targetChatId = chatId;
return this;
}
/// <summary>
/// Установить опции для конкретного адаптера. Ключ адаптера определяется адаптером (напр., "telegram").
/// </summary>
public MessageBuilder WithAdapterOption<T>(string adapterType, T options)
{
if (_adapterOptions is null) _adapterOptions = new AdapterOptionsBag();
_adapterOptions.Set(adapterType, options);
return this;
}
/// <summary>Текст сообщения.</summary>
public MessageBuilder Text(string text, MessageFormat format = MessageFormat.Plain)
{
_text = text;
_format = format;
return this;
}
/// <summary>Стиль сообщения.</summary>
public MessageBuilder Style(MessageStyle style)
{
_style = style;
return this;
}
/// <summary>Тема сообщения (для форумов).</summary>
public MessageBuilder Topic(string topic)
{
_topic = topic;
return this;
}
/// <summary>Запланировать отправку на определенную дату.</summary>
public MessageBuilder Schedule(DateTime scheduleDate)
{
_scheduleDate = scheduleDate;
return this;
}
/// <summary>Отключить предпросмотр ссылок.</summary>
public MessageBuilder DisableWebPagePreview()
{
_disableWebPagePreview = true;
return this;
}
/// <summary>Отключить уведомление.</summary>
public MessageBuilder DisableNotification()
{
_disableNotification = true;
return this;
}
/// <summary>Защитить содержимое от пересылки.</summary>
public MessageBuilder ProtectContent()
{
_protectContent = true;
return this;
}
/// <summary>Ответить на сообщение.</summary>
public MessageBuilder ReplyTo(string messageId, bool quote = true, string? quoteTitle = null)
{
_replyToMessageId = messageId;
_quoteReply = quote;
_quoteTitle = quoteTitle;
return this;
}
/// <summary>Ответить на текущее сообщение.</summary>
public MessageBuilder ReplyToCurrent(bool quote = true, string? quoteTitle = null)
{
if (_ctx.Update is not null)
{
// Предполагаем, что Update содержит ID текущего сообщения
// Это может потребовать расширения UpdateContext
_replyToMessageId = _ctx.Update.MessageId;
_quoteReply = quote;
_quoteTitle = quoteTitle;
}
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 DisableReply()
{
_disableReplyKeyboard = true;
return this;
}
/// <summary>Добавить replyкнопку.</summary>
public MessageBuilder Reply(params ReplyButton[] label)
{
_disableReplyKeyboard = false;
_reply.Add(label.ToList());
return this;
}
/// <summary>Добавить строку replyкнопок.</summary>
public MessageBuilder Reply(IEnumerable<ReplyButton> row)
{
_disableReplyKeyboard = false;
_reply.Add(row.ToList());
return this;
}
/// <summary>Добавить строки replyкнопок.</summary>
public MessageBuilder Reply(IEnumerable<IEnumerable<ReplyButton>> row)
{
_disableReplyKeyboard = false;
_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 async Task DeleteAsync(string messageId, CancellationToken ct = default)
{
await _ctx.Adapter.DeleteAsync(GetEffectiveChatId(), messageId, ct);
}
/// <summary>Удалить несколько сообщений.</summary>
public async Task<bool> DeleteMultipleAsync(IEnumerable<string> messageIds, CancellationToken ct = default)
{
return await _ctx.Adapter.DeleteMultipleAsync(GetEffectiveChatId(), messageIds, ct);
}
/// <summary>Редактировать только текст сообщения.</summary>
public async Task<string?> EditTextAsync(string messageId, string newText,
MessageFormat? format = null, CancellationToken ct = default)
{
return await _ctx.Adapter.EditTextAsync(
GetEffectiveChatId(),
messageId,
newText,
format ?? _format,
ct);
}
/// <summary>Редактировать только кнопки сообщения.</summary>
public async Task<string?> EditButtonsAsync(string messageId,
IEnumerable<IEnumerable<InlineButton>>? inlineButtons = null,
CancellationToken ct = default)
{
return await _ctx.Adapter.EditButtonsAsync(
GetEffectiveChatId(),
messageId,
inlineButtons ?? _inline,
ct);
}
/// <summary>Закрепить сообщение.</summary>
public async Task<bool> PinAsync(string messageId, bool disableNotification = false,
CancellationToken ct = default)
{
return await _ctx.Adapter.PinMessageAsync(
GetEffectiveChatId(),
messageId,
disableNotification,
ct);
}
/// <summary>Открепить сообщение.</summary>
public async Task<bool> UnpinAsync(string messageId, CancellationToken ct = default)
{
return await _ctx.Adapter.UnpinMessageAsync(
GetEffectiveChatId(),
messageId,
ct);
}
/// <summary>Получить информацию о сообщении.</summary>
public async Task<MessageInfo?> GetInfoAsync(string messageId, CancellationToken ct = default)
{
return await _ctx.Adapter.GetMessageInfoAsync(
GetEffectiveChatId(),
messageId,
ct);
}
/// <summary>Переслать сообщение.</summary>
public async Task<string?> ForwardAsync(string fromChatId, string messageId, string toChatId,
bool disableNotification = false, CancellationToken ct = default)
{
return await _ctx.Adapter.ForwardMessageAsync(
fromChatId,
messageId,
toChatId ?? GetEffectiveChatId(),
disableNotification,
ct);
}
/// <summary>Копировать сообщение.</summary>
public async Task<string?> CopyAsync(string fromChatId, string messageId, string toChatId,
string? caption = null, MessageFormat? captionFormat = null,
bool disableNotification = false, CancellationToken ct = default)
{
return await _ctx.Adapter.CopyMessageAsync(
fromChatId,
messageId,
toChatId ?? GetEffectiveChatId(),
caption,
captionFormat,
disableNotification,
ct);
}
private string GetEffectiveChatId()
{
return _targetChatId ?? _ctx.Update.Chat.Id;
}
private IMessengerAdapter GetEffectiveAdapter()
{
return _targetAdapter ?? _ctx.Adapter
?? throw new InvalidOperationException("No adapter specified for sending message");
}
/// <summary>Отправить собранное сообщение.</summary>
public async Task<string?> SendAsync(CancellationToken ct = default)
=> await SendAsync(string.Empty, ct);
/// <summary>Редактировать сообщение.</summary>
public async Task<string?> SendAsync(string messageId, CancellationToken ct = default)
{
_messageId = string.IsNullOrWhiteSpace(messageId) ? null : messageId;
string? outMessageId = null;
List<List<ReplyButton>>? reply = null;
if (_disableReplyKeyboard) reply = new();
else if (_reply.Any()) reply = _reply;
// Определяем целевой адаптер
var adapter = GetEffectiveAdapter();
// Определяем целевой чат
var targetChatId = GetEffectiveChatId();
// Текст
if (!string.IsNullOrWhiteSpace(_text))
{
var req = new SendRequest
{
ChatId = targetChatId,
Text = _text,
TextFormat = _format,
Inline = _inline,
Reply = reply,
MessageId = _messageId,
ReplyToMessageId = _replyToMessageId,
QuoteReply = _quoteReply,
QuoteTitle = _quoteTitle,
DisableWebPagePreview = _disableWebPagePreview,
DisableNotification = _disableNotification,
ProtectContent = _protectContent,
Style = _style,
ScheduleDate = _scheduleDate,
Topic = _topic,
AdapterOptions = _adapterOptions
};
outMessageId = await adapter.SendAsync(req, ct);
}
// Файлы
foreach (var (file, caption, captionFormat) in _files)
{
var req = new SendRequest
{
ChatId = targetChatId,
File = file,
Caption = caption,
CaptionFormat = captionFormat,
Inline = _inline,
Reply = reply,
ReplyToMessageId = _replyToMessageId,
QuoteReply = _quoteReply,
QuoteTitle = _quoteTitle,
DisableWebPagePreview = _disableWebPagePreview,
DisableNotification = _disableNotification,
ProtectContent = _protectContent,
Style = _style,
ScheduleDate = _scheduleDate,
Topic = _topic,
AdapterOptions = _adapterOptions
};
var res = await adapter.SendAsync(req, ct);
// сохранить первый возвращённый id сообщения
if (outMessageId is null && res is not null) outMessageId = res;
}
// Альбом
if (_album.Count > 0)
{
// Для альбома нужен PageContext с правильным адаптером
var albumCtx = CreateAlbumContext(_ctx, adapter);
var builder = adapter.CreateAlbumBuilder(albumCtx);
foreach (var (file, caption, captionFormat) in _album)
builder.Add(file, caption, captionFormat);
await builder.SendAsync(ct);
}
Reset();
return outMessageId;
}
private PageContext CreateAlbumContext(PageContext originalCtx, IMessengerAdapter adapter)
{
// Создаем новый контекст для альбома с указанным адаптером
return new PageContext
{
SessionKey = originalCtx.SessionKey,
Update = originalCtx.Update,
StateStorage = originalCtx.StateStorage,
Navigation = originalCtx.Navigation,
AdapterFactory = originalCtx.AdapterFactory,
Adapter = adapter,
};
}
/// <summary>
/// Сбросить состояние билдера для повторного использования.
/// </summary>
public MessageBuilder Reset()
{
_text = null;
_files.Clear();
_album.Clear();
_adapterOptions = null;
_targetAdapter = null;
_targetChatId = null;
_replyToMessageId = null;
_quoteReply = true;
_quoteTitle = null;
_disableWebPagePreview = false;
_disableNotification = false;
_protectContent = false;
_style = null;
_scheduleDate = null;
_topic = null;
_messageId = null;
_reply.Clear();
_inline.Clear();
_disableReplyKeyboard = false;
return this;
}
}