Новый api отправки сообщений
This commit is contained in:
@@ -4,6 +4,7 @@ namespace BotPages.Core.Messaging;
|
||||
|
||||
/// <summary>
|
||||
/// Fluent‑билдер для отправки сообщений (текст, кнопки, файлы, альбомы, прогресс).
|
||||
/// Поддерживает указание адаптер-специфичных опций через `WithAdapterOption`.
|
||||
/// </summary>
|
||||
public sealed class MessageBuilder
|
||||
{
|
||||
@@ -16,10 +17,21 @@ public sealed class MessageBuilder
|
||||
private readonly List<(FileDescriptor file, string? caption, MessageFormat? captionFormat)> _album = new();
|
||||
private bool _disableReplyKeyboard;
|
||||
private string? _editMessageId = null;
|
||||
private AdapterOptionsBag? _adapterOptions = null;
|
||||
|
||||
/// <summary>Создать билдер сообщений.</summary>
|
||||
public MessageBuilder(PageContext ctx) => _ctx = ctx;
|
||||
|
||||
/// <summary>
|
||||
/// Установить опции для конкретного адаптера. Ключ адаптера определяется адаптером (напр., "telegram").
|
||||
/// </summary>
|
||||
public MessageBuilder WithAdapterOption<T>(string adapterKey, T options)
|
||||
{
|
||||
if (_adapterOptions is null) _adapterOptions = new AdapterOptionsBag();
|
||||
_adapterOptions.Set(adapterKey, options);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>Текст сообщения.</summary>
|
||||
public MessageBuilder Text(string text, MessageFormat format = MessageFormat.Plain)
|
||||
{
|
||||
@@ -63,7 +75,7 @@ public sealed class MessageBuilder
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>Добавить строку inline‑кнопок.</summary>
|
||||
/// <summary>Добавить строки inline‑кнопок.</summary>
|
||||
public MessageBuilder Inline(IEnumerable<IEnumerable<InlineButton>> row)
|
||||
{
|
||||
_inline.AddRange(row.Select(t => t.ToList()).ToList());
|
||||
@@ -73,7 +85,6 @@ public sealed class MessageBuilder
|
||||
/// <summary>
|
||||
/// Отключение Reply клавиатуры.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public MessageBuilder DisableReply()
|
||||
{
|
||||
_disableReplyKeyboard = true;
|
||||
@@ -96,7 +107,7 @@ public sealed class MessageBuilder
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>Добавить строку reply‑кнопок.</summary>
|
||||
/// <summary>Добавить строки reply‑кнопок.</summary>
|
||||
public MessageBuilder Reply(IEnumerable<IEnumerable<ReplyButton>> row)
|
||||
{
|
||||
_disableReplyKeyboard = false;
|
||||
@@ -130,18 +141,16 @@ public sealed class MessageBuilder
|
||||
// Текст
|
||||
if (!string.IsNullOrWhiteSpace(_text))
|
||||
{
|
||||
messageId = await _ctx.SendTextAsync(_text, _format, _inline, reply, _editMessageId, ct);
|
||||
messageId = await _ctx.SendTextAsync(_text, _format, _inline, reply, _editMessageId, _adapterOptions, ct);
|
||||
}
|
||||
|
||||
// Файлы
|
||||
foreach (var (file, caption, captionFormat) in _files)
|
||||
await _ctx.SendFileAsync(file: file
|
||||
, caption: caption
|
||||
, captionFormat: captionFormat
|
||||
, reply: reply
|
||||
, inline: _inline
|
||||
, ct: ct
|
||||
);
|
||||
{
|
||||
var res = await _ctx.SendFileAsync(file, caption, captionFormat, _inline, reply, _adapterOptions, ct);
|
||||
// сохранить первый возвращённый id сообщения
|
||||
if (messageId is null && res is not null) messageId = res;
|
||||
}
|
||||
|
||||
// Альбом
|
||||
if (_album.Count > 0)
|
||||
@@ -157,6 +166,7 @@ public sealed class MessageBuilder
|
||||
_album.Clear();
|
||||
|
||||
_editMessageId = null;
|
||||
_adapterOptions = null;
|
||||
|
||||
return messageId;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user