Добавлена возможность отсылать сообщения в другие чаты
All checks were successful
CI / build-test (push) Successful in 33s
All checks were successful
CI / build-test (push) Successful in 33s
This commit is contained in:
@@ -9,17 +9,19 @@ namespace BotPages.Core.Abstractions;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IMessengerAdapter
|
public interface IMessengerAdapter
|
||||||
{
|
{
|
||||||
|
Capabilities Capabilities { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Отправить текстовое сообщение в чат.
|
/// Отправить текстовое сообщение в чат.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Task SendTextAsync(PageContext ctx, string text, MessageFormat format,
|
Task SendTextAsync(string chatId, string text, MessageFormat format,
|
||||||
IEnumerable<IEnumerable<InlineButton>>? inline,
|
IEnumerable<IEnumerable<InlineButton>>? inline,
|
||||||
IEnumerable<IEnumerable<ReplyButton>>? reply, CancellationToken ct);
|
IEnumerable<IEnumerable<ReplyButton>>? reply, CancellationToken ct);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Отправить файл в чат.
|
/// Отправить файл в чат.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Task SendFileAsync(PageContext ctx, FileDescriptor file, string? caption, CancellationToken ct);
|
Task SendFileAsync(string chatId, FileDescriptor file, string? caption, CancellationToken ct);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Создать билдер альбома для отправки медиагруппы.
|
/// Создать билдер альбома для отправки медиагруппы.
|
||||||
|
|||||||
@@ -49,13 +49,13 @@ public sealed class PageContext
|
|||||||
IEnumerable<IEnumerable<InlineButton>>? inline = null,
|
IEnumerable<IEnumerable<InlineButton>>? inline = null,
|
||||||
IEnumerable<IEnumerable<ReplyButton>>? reply = null,
|
IEnumerable<IEnumerable<ReplyButton>>? reply = null,
|
||||||
CancellationToken ct = default)
|
CancellationToken ct = default)
|
||||||
=> Adapter.SendTextAsync(this, text, format, inline, reply, ct);
|
=> Adapter.SendTextAsync(this.Update.Chat.Id, text, format, inline, reply, ct);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Отправить файл.
|
/// Отправить файл.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Task SendFileAsync(FileDescriptor file, string? caption = null, CancellationToken ct = default)
|
public Task SendFileAsync(FileDescriptor file, string? caption = null, CancellationToken ct = default)
|
||||||
=> Adapter.SendFileAsync(this, file, caption, ct);
|
=> Adapter.SendFileAsync(this.Update.Chat.Id, file, caption, ct);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Получить билдер альбомов.
|
/// Получить билдер альбомов.
|
||||||
|
|||||||
@@ -27,6 +27,17 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup
|
|||||||
private TelegramBotClient? _client;
|
private TelegramBotClient? _client;
|
||||||
private string _token;
|
private string _token;
|
||||||
private string _messagerType;
|
private string _messagerType;
|
||||||
|
private static Capabilities _capabilities = new()
|
||||||
|
{
|
||||||
|
SupportsInlineButtons = true,
|
||||||
|
SupportsReplyButtons = true,
|
||||||
|
SupportsAlbums = true,
|
||||||
|
SupportsFormattingMarkdown = true,
|
||||||
|
SupportsFormattingHtml = true,
|
||||||
|
MaxMessageLength = 4096,
|
||||||
|
};
|
||||||
|
|
||||||
|
public Capabilities Capabilities => _capabilities;
|
||||||
|
|
||||||
/// <summary>Создать адаптер Telegram.</summary>
|
/// <summary>Создать адаптер Telegram.</summary>
|
||||||
public TelegramAdapter(ILogger logger, string token)
|
public TelegramAdapter(ILogger logger, string token)
|
||||||
@@ -73,7 +84,7 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public async Task SendTextAsync(PageContext ctx, string text, MessageFormat format,
|
public async Task SendTextAsync(string chatId, string text, MessageFormat format,
|
||||||
IEnumerable<IEnumerable<InlineButton>>? inline,
|
IEnumerable<IEnumerable<InlineButton>>? inline,
|
||||||
IEnumerable<IEnumerable<ReplyButton>>? reply, CancellationToken ct)
|
IEnumerable<IEnumerable<ReplyButton>>? reply, CancellationToken ct)
|
||||||
{
|
{
|
||||||
@@ -129,14 +140,14 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Длина сообщения
|
// Длина сообщения
|
||||||
if (text.Length > ctx.Update.Chat.Capabilities.MaxMessageLength)
|
if (text.Length > Capabilities.MaxMessageLength)
|
||||||
{
|
{
|
||||||
_logger.Log(LogLevel.Warn, $"Message too long ({text.Length}). Truncated to {ctx.Update.Chat.Capabilities.MaxMessageLength}.");
|
_logger.Log(LogLevel.Warn, $"Message too long ({text.Length}). Truncated to {Capabilities.MaxMessageLength}.");
|
||||||
text = text.Substring(0, ctx.Update.Chat.Capabilities.MaxMessageLength);
|
text = text.Substring(0, Capabilities.MaxMessageLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
await _client.SendMessage(
|
await _client.SendMessage(
|
||||||
chatId: long.Parse(ctx.Update.Chat.Id),
|
chatId: long.Parse(chatId),
|
||||||
text: text,
|
text: text,
|
||||||
parseMode: parseMode,
|
parseMode: parseMode,
|
||||||
replyMarkup: markup,
|
replyMarkup: markup,
|
||||||
@@ -145,7 +156,7 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public async Task SendFileAsync(PageContext ctx, FileDescriptor file, string? caption, CancellationToken ct)
|
public async Task SendFileAsync(string chatId, FileDescriptor file, string? caption, CancellationToken ct)
|
||||||
{
|
{
|
||||||
if (_client is null)
|
if (_client is null)
|
||||||
{
|
{
|
||||||
@@ -153,8 +164,6 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var chatId = long.Parse(ctx.Update.Chat.Id);
|
|
||||||
|
|
||||||
// Получаем поток, если он задан
|
// Получаем поток, если он задан
|
||||||
Stream? stream = null;
|
Stream? stream = null;
|
||||||
if (file.GetStreamAsync is not null)
|
if (file.GetStreamAsync is not null)
|
||||||
@@ -182,16 +191,16 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup
|
|||||||
switch (file.Kind)
|
switch (file.Kind)
|
||||||
{
|
{
|
||||||
case FileKind.Photo:
|
case FileKind.Photo:
|
||||||
await _client.SendPhoto(chatId, inputFile, caption ?? "", cancellationToken: ct);
|
await _client.SendPhoto(long.Parse(chatId), inputFile, caption ?? "", cancellationToken: ct);
|
||||||
break;
|
break;
|
||||||
case FileKind.Video:
|
case FileKind.Video:
|
||||||
await _client.SendVideo(chatId, inputFile, caption: caption ?? "", cancellationToken: ct);
|
await _client.SendVideo(long.Parse(chatId), inputFile, caption: caption ?? "", cancellationToken: ct);
|
||||||
break;
|
break;
|
||||||
case FileKind.Audio:
|
case FileKind.Audio:
|
||||||
await _client.SendAudio(chatId, inputFile, caption ?? "", cancellationToken: ct);
|
await _client.SendAudio(long.Parse(chatId), inputFile, caption ?? "", cancellationToken: ct);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
await _client.SendDocument(chatId, inputFile, caption ?? "", cancellationToken: ct);
|
await _client.SendDocument(long.Parse(chatId), inputFile, caption ?? "", cancellationToken: ct);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ public sealed class TelegramAlbumBuilder : IAlbumBuilder
|
|||||||
{
|
{
|
||||||
_logger.Log(LogLevel.Warn, "Albums not supported. Degraded to sequential sends.");
|
_logger.Log(LogLevel.Warn, "Albums not supported. Degraded to sequential sends.");
|
||||||
foreach (var (file, caption) in _items)
|
foreach (var (file, caption) in _items)
|
||||||
await _adapter.SendFileAsync(_ctx, file, caption, ct);
|
await _adapter.SendFileAsync(_ctx.Update.Chat.Id, file, caption, ct);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,7 +95,7 @@ public sealed class TelegramAlbumBuilder : IAlbumBuilder
|
|||||||
{
|
{
|
||||||
// Telegram не поддерживает document в альбомах — деградация
|
// Telegram не поддерживает document в альбомах — деградация
|
||||||
_logger.Log(LogLevel.Warn, $"Document '{file.Kind}' in album not supported. Sending document separately.");
|
_logger.Log(LogLevel.Warn, $"Document '{file.Kind}' in album not supported. Sending document separately.");
|
||||||
await _adapter.SendFileAsync(_ctx, file, caption, ct);
|
await _adapter.SendFileAsync(_ctx.Update.Chat.Id, file, caption, ct);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -34,15 +34,6 @@ public static class TelegramUpdateMapper
|
|||||||
{
|
{
|
||||||
Id = chat?.Id.ToString() ?? "unknown",
|
Id = chat?.Id.ToString() ?? "unknown",
|
||||||
Title = chat?.Title,
|
Title = chat?.Title,
|
||||||
Capabilities = new Capabilities
|
|
||||||
{
|
|
||||||
SupportsInlineButtons = true,
|
|
||||||
SupportsReplyButtons = true,
|
|
||||||
SupportsAlbums = true,
|
|
||||||
SupportsFormattingMarkdown = true,
|
|
||||||
SupportsFormattingHtml = true,
|
|
||||||
MaxMessageLength = 4096,
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
string? text = null;
|
string? text = null;
|
||||||
|
|||||||
Reference in New Issue
Block a user