Добавлены новые методы отправки сообщений
This commit is contained in:
@@ -4,7 +4,7 @@ namespace BotPages.Core.Messaging;
|
||||
|
||||
/// <summary>
|
||||
/// Fluent‑билдер для отправки сообщений (текст, кнопки, файлы, альбомы, прогресс).
|
||||
/// Поддерживает указание адаптер-специфичных опций через `WithAdapterOption`.
|
||||
/// Поддерживает указание адаптер-специфичных опций через <see cref="WithAdapterOption{T}(string, T)"/>.
|
||||
/// </summary>
|
||||
public sealed class MessageBuilder
|
||||
{
|
||||
@@ -17,11 +17,77 @@ public sealed class MessageBuilder
|
||||
private readonly List<(FileDescriptor file, string? caption, MessageFormat? captionFormat)> _album = new();
|
||||
private bool _disableReplyKeyboard;
|
||||
private AdapterOptionsBag? _adapterOptions = null;
|
||||
private string? _chatId = 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>
|
||||
@@ -32,13 +98,6 @@ public sealed class MessageBuilder
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>ID чата куда отрпавить сообщение.</summary>
|
||||
public MessageBuilder ChatId(string chatId)
|
||||
{
|
||||
_chatId = chatId;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>Текст сообщения.</summary>
|
||||
public MessageBuilder Text(string text, MessageFormat format = MessageFormat.Plain)
|
||||
{
|
||||
@@ -47,6 +106,71 @@ public sealed class MessageBuilder
|
||||
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)
|
||||
{
|
||||
@@ -132,7 +256,104 @@ public sealed class MessageBuilder
|
||||
/// <summary>Удалить сообщение.</summary>
|
||||
public async Task DeleteAsync(string messageId, CancellationToken ct = default)
|
||||
{
|
||||
await _ctx.DeleteAsync(this._chatId ?? _ctx.Update.Chat.Id, messageId, ct);
|
||||
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>
|
||||
@@ -142,27 +363,43 @@ public sealed class MessageBuilder
|
||||
/// <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 = _chatId ?? _ctx.Update.Chat.Id,
|
||||
ChatId = targetChatId,
|
||||
Text = _text,
|
||||
TextFormat = _format,
|
||||
Inline = _inline,
|
||||
Reply = reply,
|
||||
MessageId = string.IsNullOrWhiteSpace(messageId) ? null : messageId,
|
||||
MessageId = _messageId,
|
||||
ReplyToMessageId = _replyToMessageId,
|
||||
QuoteReply = _quoteReply,
|
||||
QuoteTitle = _quoteTitle,
|
||||
DisableWebPagePreview = _disableWebPagePreview,
|
||||
DisableNotification = _disableNotification,
|
||||
ProtectContent = _protectContent,
|
||||
Style = _style,
|
||||
ScheduleDate = _scheduleDate,
|
||||
Topic = _topic,
|
||||
AdapterOptions = _adapterOptions
|
||||
};
|
||||
|
||||
outMessageId = await _ctx.SendAsync(req, ct);
|
||||
outMessageId = await adapter.SendAsync(req, ct);
|
||||
}
|
||||
|
||||
// Файлы
|
||||
@@ -170,16 +407,25 @@ public sealed class MessageBuilder
|
||||
{
|
||||
var req = new SendRequest
|
||||
{
|
||||
ChatId = _chatId ?? _ctx.Update.Chat.Id,
|
||||
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 _ctx.SendAsync(req, ct);
|
||||
var res = await adapter.SendAsync(req, ct);
|
||||
// сохранить первый возвращённый id сообщения
|
||||
if (outMessageId is null && res is not null) outMessageId = res;
|
||||
}
|
||||
@@ -187,22 +433,58 @@ public sealed class MessageBuilder
|
||||
// Альбом
|
||||
if (_album.Count > 0)
|
||||
{
|
||||
var builder = _ctx.Albums;
|
||||
// Для альбома нужен 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);
|
||||
}
|
||||
|
||||
_text = null;
|
||||
_files.Clear();
|
||||
_album.Clear();
|
||||
|
||||
_adapterOptions = null;
|
||||
_chatId = null;
|
||||
|
||||
_reply.Clear();
|
||||
_inline.Clear();
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user