using BotPages.Core.Abstractions;
using BotPages.Core.Messaging;
namespace BotPages.Core;
///
/// Расширения для работы с адаптером.
/// Упрощают создание универсального `SendRequest`.
///
public static class PageContextAdapterExtensions
{
///
/// Отправить универсальный запрос через привязанный адаптер.
///
public static Task SendAsync(this PageContext ctx, SendRequest request, CancellationToken ct = default)
=> ctx.Adapter.SendAsync(request, ct);
///
/// Удобная оболочка: отправить текстовое сообщение.
///
public static Task SendTextAsync(this PageContext ctx,
string text,
MessageFormat format = MessageFormat.Plain,
IEnumerable>? inline = null,
IEnumerable>? reply = null,
string? messageId = null,
object? adapterOptions = null,
CancellationToken ct = default)
{
var bag = adapterOptions switch
{
null => null,
AdapterOptionsBag b => b,
_ => throw new ArgumentException("adapterOptions must be an AdapterOptionsBag or null. Use MessageBuilder extensions to set adapter options.", nameof(adapterOptions))
};
return ctx.SendAsync(new SendRequest
{
ChatId = ctx.Update.Chat.Id,
Text = text,
TextFormat = format,
Inline = inline,
Reply = reply,
MessageId = messageId,
AdapterOptions = bag
}, ct);
}
///
/// Удобная оболочка: отправить файл.
///
public static Task SendFileAsync(this PageContext ctx,
FileDescriptor file,
string? caption = null,
MessageFormat? captionFormat = null,
IEnumerable>? inline = null,
IEnumerable>? reply = null,
object? adapterOptions = null,
CancellationToken ct = default)
{
var bag = adapterOptions switch
{
null => null,
AdapterOptionsBag b => b,
_ => throw new ArgumentException("adapterOptions must be an AdapterOptionsBag or null. Use MessageBuilder extensions to set adapter options.", nameof(adapterOptions))
};
return ctx.SendAsync(new SendRequest
{
ChatId = ctx.Update.Chat.Id,
File = file,
Caption = caption,
CaptionFormat = captionFormat,
Inline = inline,
Reply = reply,
AdapterOptions = bag
}, ct);
}
}