Добавлена возможность отсылать сообщения в другие чаты
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:
@@ -27,6 +27,17 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup
|
||||
private TelegramBotClient? _client;
|
||||
private string _token;
|
||||
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>
|
||||
public TelegramAdapter(ILogger logger, string token)
|
||||
@@ -73,7 +84,7 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup
|
||||
}
|
||||
|
||||
/// <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<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}.");
|
||||
text = text.Substring(0, ctx.Update.Chat.Capabilities.MaxMessageLength);
|
||||
_logger.Log(LogLevel.Warn, $"Message too long ({text.Length}). Truncated to {Capabilities.MaxMessageLength}.");
|
||||
text = text.Substring(0, Capabilities.MaxMessageLength);
|
||||
}
|
||||
|
||||
await _client.SendMessage(
|
||||
chatId: long.Parse(ctx.Update.Chat.Id),
|
||||
chatId: long.Parse(chatId),
|
||||
text: text,
|
||||
parseMode: parseMode,
|
||||
replyMarkup: markup,
|
||||
@@ -145,7 +156,7 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
@@ -153,8 +164,6 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup
|
||||
return;
|
||||
}
|
||||
|
||||
var chatId = long.Parse(ctx.Update.Chat.Id);
|
||||
|
||||
// Получаем поток, если он задан
|
||||
Stream? stream = null;
|
||||
if (file.GetStreamAsync is not null)
|
||||
@@ -182,16 +191,16 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup
|
||||
switch (file.Kind)
|
||||
{
|
||||
case FileKind.Photo:
|
||||
await _client.SendPhoto(chatId, inputFile, caption ?? "", cancellationToken: ct);
|
||||
await _client.SendPhoto(long.Parse(chatId), inputFile, caption ?? "", cancellationToken: ct);
|
||||
break;
|
||||
case FileKind.Video:
|
||||
await _client.SendVideo(chatId, inputFile, caption: caption ?? "", cancellationToken: ct);
|
||||
await _client.SendVideo(long.Parse(chatId), inputFile, caption: caption ?? "", cancellationToken: ct);
|
||||
break;
|
||||
case FileKind.Audio:
|
||||
await _client.SendAudio(chatId, inputFile, caption ?? "", cancellationToken: ct);
|
||||
await _client.SendAudio(long.Parse(chatId), inputFile, caption ?? "", cancellationToken: ct);
|
||||
break;
|
||||
default:
|
||||
await _client.SendDocument(chatId, inputFile, caption ?? "", cancellationToken: ct);
|
||||
await _client.SendDocument(long.Parse(chatId), inputFile, caption ?? "", 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) in _items)
|
||||
await _adapter.SendFileAsync(_ctx, file, caption, ct);
|
||||
await _adapter.SendFileAsync(_ctx.Update.Chat.Id, file, caption, 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, 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",
|
||||
Title = chat?.Title,
|
||||
Capabilities = new Capabilities
|
||||
{
|
||||
SupportsInlineButtons = true,
|
||||
SupportsReplyButtons = true,
|
||||
SupportsAlbums = true,
|
||||
SupportsFormattingMarkdown = true,
|
||||
SupportsFormattingHtml = true,
|
||||
MaxMessageLength = 4096,
|
||||
}
|
||||
};
|
||||
|
||||
string? text = null;
|
||||
|
||||
Reference in New Issue
Block a user