Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d6f54bb0e6 |
@@ -9,22 +9,33 @@ namespace BotPages.Core.Abstractions;
|
||||
/// </summary>
|
||||
public interface IMessengerAdapter
|
||||
{
|
||||
/// <summary>
|
||||
/// Доступные возможности мессенджера.
|
||||
/// </summary>
|
||||
Capabilities Capabilities { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Отправить текстовое сообщение в чат.
|
||||
/// </summary>
|
||||
Task<string?> SendTextAsync(string chatId, string text, MessageFormat format,
|
||||
IEnumerable<IEnumerable<InlineButton>>? inline,
|
||||
IEnumerable<IEnumerable<ReplyButton>>? reply,
|
||||
string? messageId,
|
||||
CancellationToken ct
|
||||
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, MessageFormat? captionFormat, CancellationToken ct);
|
||||
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);
|
||||
|
||||
/// <summary>
|
||||
/// Создать билдер альбома для отправки медиагруппы.
|
||||
|
||||
@@ -11,7 +11,9 @@ public static class PageContextAdapterExtensions
|
||||
/// <summary>
|
||||
/// Отправить текстовое сообщение.
|
||||
/// </summary>
|
||||
public static Task<string?> SendTextAsync(this PageContext ctx, string text, MessageFormat format = MessageFormat.Plain,
|
||||
public static Task<string?> SendTextAsync(this PageContext ctx,
|
||||
string text,
|
||||
MessageFormat format = MessageFormat.Plain,
|
||||
IEnumerable<IEnumerable<InlineButton>>? inline = null,
|
||||
IEnumerable<IEnumerable<ReplyButton>>? reply = null,
|
||||
string? messageId = null,
|
||||
@@ -21,12 +23,19 @@ public static class PageContextAdapterExtensions
|
||||
/// <summary>
|
||||
/// Отправить файл.
|
||||
/// </summary>
|
||||
public static Task SendFileAsync(this PageContext ctx, FileDescriptor file, string? caption = null, MessageFormat? captionFormat = null, CancellationToken ct = default)
|
||||
=> ctx.Adapter.SendFileAsync(ctx.Update.Chat.Id, file, caption, captionFormat, ct);
|
||||
|
||||
/// <summary>
|
||||
/// Отправить файл.
|
||||
/// </summary>
|
||||
public static Task SendFileAsync(this PageContext ctx, FileDescriptor file, string? caption = null, CancellationToken ct = default)
|
||||
=> ctx.Adapter.SendFileAsync(ctx.Update.Chat.Id, file, caption, null, ct);
|
||||
public static Task 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);
|
||||
}
|
||||
@@ -123,19 +123,25 @@ public sealed class MessageBuilder
|
||||
{
|
||||
string? messageId = null;
|
||||
|
||||
List<List<ReplyButton>>? reply = null;
|
||||
if (_disableReplyKeyboard) reply = new();
|
||||
else if (_reply.Any()) reply = _reply;
|
||||
|
||||
// Текст
|
||||
if (!string.IsNullOrWhiteSpace(_text))
|
||||
{
|
||||
List<List<ReplyButton>>? reply = null;
|
||||
if (_disableReplyKeyboard) reply = new();
|
||||
else if (_reply.Any()) reply = _reply;
|
||||
|
||||
messageId = await _ctx.SendTextAsync(_text, _format, _inline, reply, _editMessageId, ct);
|
||||
}
|
||||
|
||||
// Файлы
|
||||
foreach (var (file, caption, captionFormat) in _files)
|
||||
await _ctx.SendFileAsync(file, caption, captionFormat, ct);
|
||||
await _ctx.SendFileAsync(file: file
|
||||
, caption: caption
|
||||
, captionFormat: captionFormat
|
||||
, reply: reply
|
||||
, inline: _inline
|
||||
, ct: ct
|
||||
);
|
||||
|
||||
// Альбом
|
||||
if (_album.Count > 0)
|
||||
|
||||
@@ -85,11 +85,12 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<string?> SendTextAsync(string chatId, string text, MessageFormat format,
|
||||
IEnumerable<IEnumerable<InlineButton>>? inline,
|
||||
IEnumerable<IEnumerable<ReplyButton>>? reply,
|
||||
string? messageId,
|
||||
CancellationToken ct)
|
||||
public async 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)
|
||||
{
|
||||
if (_client is null)
|
||||
{
|
||||
@@ -187,7 +188,14 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task SendFileAsync(string chatId, FileDescriptor file, string? caption, MessageFormat? captionFormat, CancellationToken ct)
|
||||
public async 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
|
||||
)
|
||||
{
|
||||
if (_client is null)
|
||||
{
|
||||
@@ -249,20 +257,46 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup
|
||||
}
|
||||
}
|
||||
|
||||
ReplyMarkup? markup = null;
|
||||
|
||||
if (inline is not null && inline.Any())
|
||||
{
|
||||
markup = new InlineKeyboardMarkup(
|
||||
inline.Select(row => row.Select(b => new InlineKeyboardButton(b.Label, b.Value)).ToArray())
|
||||
.ToArray()
|
||||
);
|
||||
}
|
||||
else if (reply is not null)
|
||||
{
|
||||
if (reply.Any())
|
||||
{
|
||||
markup = new ReplyKeyboardMarkup(
|
||||
reply.Select(row => row.Select(b => new KeyboardButton(b.Label)).ToArray()).ToArray()
|
||||
)
|
||||
{
|
||||
ResizeKeyboard = true
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
markup = new ReplyKeyboardRemove();
|
||||
}
|
||||
}
|
||||
|
||||
// В зависимости от FileKind выбираем подходящий метод
|
||||
switch (file.Kind)
|
||||
{
|
||||
case FileKind.Photo:
|
||||
await _client.SendPhoto(long.Parse(chatId), inputFile, caption ?? "", parseMode, cancellationToken: ct);
|
||||
await _client.SendPhoto(long.Parse(chatId), inputFile, caption ?? "", parseMode, replyMarkup: markup, cancellationToken: ct);
|
||||
break;
|
||||
case FileKind.Video:
|
||||
await _client.SendVideo(long.Parse(chatId), inputFile, caption: caption ?? "", parseMode, cancellationToken: ct);
|
||||
await _client.SendVideo(long.Parse(chatId), inputFile, caption: caption ?? "", parseMode, replyMarkup: markup, cancellationToken: ct);
|
||||
break;
|
||||
case FileKind.Audio:
|
||||
await _client.SendAudio(long.Parse(chatId), inputFile, caption ?? "", parseMode, cancellationToken: ct);
|
||||
await _client.SendAudio(long.Parse(chatId), inputFile, caption ?? "", parseMode, replyMarkup: markup, cancellationToken: ct);
|
||||
break;
|
||||
default:
|
||||
await _client.SendDocument(long.Parse(chatId), inputFile, caption ?? "", parseMode, cancellationToken: ct);
|
||||
await _client.SendDocument(long.Parse(chatId), inputFile, caption ?? "", parseMode, replyMarkup: markup, cancellationToken: ct);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ public sealed class TelegramAlbumBuilder : IAlbumBuilder
|
||||
{
|
||||
_logger.Log(LogLevel.Warn, "Albums not supported. Degraded to sequential sends.");
|
||||
foreach (var (file, caption, captionFormat) in _items)
|
||||
await _adapter.SendFileAsync(_ctx.Update.Chat.Id, file, caption, captionFormat, ct);
|
||||
await _adapter.SendFileAsync(_ctx.Update.Chat.Id, file, caption, captionFormat, ct: ct);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -95,7 +95,7 @@ public sealed class TelegramAlbumBuilder : IAlbumBuilder
|
||||
{
|
||||
// Telegram не поддерживает document в альбомах — деградация
|
||||
_logger.Log(LogLevel.Warn, $"Document '{file.Kind}' in album not supported. Sending document separately.");
|
||||
await _adapter.SendFileAsync(_ctx.Update.Chat.Id, file, caption, captionFormat, ct);
|
||||
await _adapter.SendFileAsync(_ctx.Update.Chat.Id, file, caption, captionFormat, ct: ct);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ public sealed class FilesPage : SingletonPage
|
||||
{
|
||||
foreach (var file in files)
|
||||
{
|
||||
await ctx.SendFileAsync(file, $"Файл '{file.Name}' получен и отправлен обратно.", ct);
|
||||
await ctx.SendFileAsync(file, $"Файл '{file.Name}' получен и отправлен обратно.", ct: ct);
|
||||
}
|
||||
|
||||
//Обращение через Storage
|
||||
|
||||
Reference in New Issue
Block a user