Добавлена возможность отсылать сообщения в другие чаты
All checks were successful
CI / build-test (push) Successful in 33s

This commit is contained in:
2025-12-05 19:45:59 +03:00
parent a94327f0c8
commit 634a9292dc
5 changed files with 29 additions and 27 deletions

View File

@@ -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;
}
}

View File

@@ -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);
}
}

View File

@@ -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;