Новый api отправки сообщений
This commit is contained in:
44
BotPages.Core/Abstractions/AdapterOptionsBag.cs
Normal file
44
BotPages.Core/Abstractions/AdapterOptionsBag.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
namespace BotPages.Core.Abstractions;
|
||||
|
||||
/// <summary>
|
||||
/// Êîíòåéíåð äëÿ àäàïòåð-ñïåöèôè÷íûõ îïöèé, ïîçâîëÿþùèé õðàíèòü ïàðàìåòðû äëÿ íåñêîëüêèõ àäàïòåðîâ.
|
||||
/// Èñïîëüçóåòñÿ âíóòðè `SendRequest.AdapterOptions`.
|
||||
/// </summary>
|
||||
public sealed class AdapterOptionsBag
|
||||
{
|
||||
private readonly Dictionary<string, object?> _map = new(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
/// <summary>
|
||||
/// Óñòàíîâèòü îïöèè äëÿ àäàïòåðà.
|
||||
/// </summary>
|
||||
public void Set<T>(string adapterKey, T options)
|
||||
{
|
||||
if (adapterKey is null) throw new ArgumentNullException(nameof(adapterKey));
|
||||
_map[adapterKey] = options;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ïîïðîáîâàòü ïîëó÷èòü îïöèè äëÿ àäàïòåðà.
|
||||
/// </summary>
|
||||
public bool TryGet<T>(string adapterKey, out T? options)
|
||||
{
|
||||
if (adapterKey is null) throw new ArgumentNullException(nameof(adapterKey));
|
||||
if (_map.TryGetValue(adapterKey, out var o) && o is T t)
|
||||
{
|
||||
options = t;
|
||||
return true;
|
||||
}
|
||||
|
||||
options = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ïîëó÷èòü îïöèè èëè âåðíóòü null.
|
||||
/// </summary>
|
||||
public T? GetOrDefault<T>(string adapterKey)
|
||||
{
|
||||
TryGet(adapterKey, out T? v);
|
||||
return v;
|
||||
}
|
||||
}
|
||||
@@ -15,27 +15,9 @@ public interface IMessengerAdapter
|
||||
Capabilities Capabilities { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Отправить текстовое сообщение в чат.
|
||||
/// Универсальный метод отправки с использованием общего описания запроса.
|
||||
/// </summary>
|
||||
Task<string?> SendTextAsync(string chatId,
|
||||
string text,
|
||||
MessageFormat format = MessageFormat.Plain,
|
||||
IEnumerable<IEnumerable<InlineButton>>? inline = null,
|
||||
IEnumerable<IEnumerable<ReplyButton>>? reply = null,
|
||||
string? messageId = null,
|
||||
CancellationToken ct = default
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Отправить файл в чат.
|
||||
/// </summary>
|
||||
Task SendFileAsync(string chatId,
|
||||
FileDescriptor file,
|
||||
string? caption = null,
|
||||
MessageFormat? captionFormat = null,
|
||||
IEnumerable<IEnumerable<InlineButton>>? inline = null,
|
||||
IEnumerable<IEnumerable<ReplyButton>>? reply = null,
|
||||
CancellationToken ct = default);
|
||||
Task<string?> SendAsync(SendRequest request, CancellationToken ct = default);
|
||||
|
||||
/// <summary>
|
||||
/// Создать билдер альбома для отправки медиагруппы.
|
||||
|
||||
43
BotPages.Core/Abstractions/SendRequest.cs
Normal file
43
BotPages.Core/Abstractions/SendRequest.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using BotPages.Core.Messaging;
|
||||
|
||||
namespace BotPages.Core.Abstractions;
|
||||
|
||||
/// <summary>
|
||||
/// Óíèâåðñàëüíàÿ ñòðóêòóðà çàïðîñà íà îòïðàâêó ñîîáùåíèÿ/ôàéëà, èñïîëüçóåìàÿ àäàïòåðàìè.
|
||||
/// Ïîìåùåíà â Core ÷òîáû áûòü äîñòóïíîé äëÿ âñåõ àäàïòåðîâ.
|
||||
/// </summary>
|
||||
public sealed class SendRequest
|
||||
{
|
||||
/// <summary>Èäåíòèôèêàòîð ÷àòà/ñåññèè â ìåññåíäæåðå.</summary>
|
||||
public required string ChatId { get; init; }
|
||||
|
||||
/// <summary>Òåêñò ñîîáùåíèÿ (åñëè îòïðàâëÿåòñÿ òåêñò).</summary>
|
||||
public string? Text { get; init; }
|
||||
|
||||
/// <summary>Ôîðìàò òåêñòà (HTML/Markdown/Plain).</summary>
|
||||
public MessageFormat? TextFormat { get; init; }
|
||||
|
||||
/// <summary>Inline êíîïêè (ñòðîêè êíîïîê).</summary>
|
||||
public IEnumerable<IEnumerable<InlineButton>>? Inline { get; init; }
|
||||
|
||||
/// <summary>Reply êëàâèàòóðà (ñòðîêè êíîïîê).</summary>
|
||||
public IEnumerable<IEnumerable<ReplyButton>>? Reply { get; init; }
|
||||
|
||||
/// <summary>Id ðåäàêòèðóåìîãî ñîîáùåíèÿ (åñëè ðåäàêòèðóåì).</summary>
|
||||
public string? MessageId { get; init; }
|
||||
|
||||
/// <summary>Ôàéë äëÿ îòïðàâêè (åñëè îòïðàâëÿåòñÿ ôàéë).</summary>
|
||||
public FileDescriptor? File { get; init; }
|
||||
|
||||
/// <summary>Ïîäïèñü äëÿ ôàéëà.</summary>
|
||||
public string? Caption { get; init; }
|
||||
|
||||
/// <summary>Ôîðìàò ïîäïèñè/ïîäïèñè äëÿ ôàéëà.</summary>
|
||||
public MessageFormat? CaptionFormat { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Êîíòåéíåð àäàïòåð-ñïåöèôè÷íûõ îïöèé.
|
||||
/// Ñîäåðæèò èìåíà/êëþ÷è àäàïòåðîâ è ñîîòâåòñòâóþùèå îáúåêòû îïöèé.
|
||||
/// </summary>
|
||||
public AdapterOptionsBag? AdapterOptions { get; init; }
|
||||
}
|
||||
Reference in New Issue
Block a user