Новый api отправки сообщений

This commit is contained in:
FrigaT
2025-12-24 08:43:46 +03:00
parent 833d8c80d9
commit 37cb6599ba
19 changed files with 478 additions and 338 deletions

View File

@@ -4,12 +4,19 @@ using BotPages.Core.Messaging;
namespace BotPages.Core;
/// <summary>
/// Расширения <see cref="PageContext"/> для работы с <see cref="IMessengerAdapter"/>
/// Расширения <see cref="PageContext"/> для работы с адаптером.
/// Упрощают создание универсального `SendRequest`.
/// </summary>
public static class PageContextAdapterExtensions
{
/// <summary>
/// Отправить текстовое сообщение.
/// Отправить универсальный запрос через привязанный адаптер.
/// </summary>
public static Task<string?> SendAsync(this PageContext ctx, SendRequest request, CancellationToken ct = default)
=> ctx.Adapter.SendAsync(request, ct);
/// <summary>
/// Удобная оболочка: отправить текстовое сообщение.
/// </summary>
public static Task<string?> SendTextAsync(this PageContext ctx,
string text,
@@ -17,25 +24,56 @@ public static class PageContextAdapterExtensions
IEnumerable<IEnumerable<InlineButton>>? inline = null,
IEnumerable<IEnumerable<ReplyButton>>? reply = null,
string? messageId = null,
object? adapterOptions = null,
CancellationToken ct = default)
=> ctx.Adapter.SendTextAsync(ctx.Update.Chat.Id, text, format, inline, reply, messageId, ct);
{
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);
}
/// <summary>
/// Отправить файл.
/// Удобная оболочка: отправить файл.
/// </summary>
public static Task SendFileAsync(this PageContext ctx,
public static Task<string?> SendFileAsync(this PageContext ctx,
FileDescriptor file,
string? caption = null,
MessageFormat? captionFormat = null,
IEnumerable<IEnumerable<InlineButton>>? inline = null,
IEnumerable<IEnumerable<ReplyButton>>? reply = null,
CancellationToken ct = default
)
=> ctx.Adapter.SendFileAsync(chatId: ctx.Update.Chat.Id,
file: file,
caption: caption,
captionFormat: captionFormat,
inline: inline,
reply: reply,
ct: ct);
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);
}
}