diff --git a/BotPages.Core/Context/ChatContext.cs b/BotPages.Core/Context/ChatContext.cs index bc498a5..e962969 100644 --- a/BotPages.Core/Context/ChatContext.cs +++ b/BotPages.Core/Context/ChatContext.cs @@ -15,7 +15,4 @@ public sealed class ChatContext /// Идентификатор треда (опционально). public string? ThreadId { get; init; } - - /// Возможности мессенджера. - public Capabilities Capabilities { get; init; } = new(); } diff --git a/BotPages.Telegram/BotPagesAppExtension.cs b/BotPages.Telegram/BotPagesAppExtension.cs index ba54118..ba24b30 100644 --- a/BotPages.Telegram/BotPagesAppExtension.cs +++ b/BotPages.Telegram/BotPagesAppExtension.cs @@ -12,11 +12,15 @@ public static class BotPagesAppExtension /// /// /// + /// /// - public static BotPagesApp AddTelegramAdapter(this BotPagesApp app, string token) + public static BotPagesApp AddTelegramAdapter(this BotPagesApp app, string token, string messengerType = "") { var telegram = new TelegramAdapter(app.Logger, token); - app.AddAdapter(telegram.MessagerType, telegram); + + if (!string.IsNullOrWhiteSpace(messengerType)) telegram.MessengerType = messengerType; + + app.AddAdapter(telegram.MessengerType, telegram); return app; } } diff --git a/BotPages.Telegram/TelegramAdapter.cs b/BotPages.Telegram/TelegramAdapter.cs index e99db2f..4334600 100644 --- a/BotPages.Telegram/TelegramAdapter.cs +++ b/BotPages.Telegram/TelegramAdapter.cs @@ -26,7 +26,6 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup private readonly ILogger _logger; private TelegramBotClient? _client; private string _token; - private string _messagerType; private static Capabilities _capabilities = new() { SupportsInlineButtons = true, @@ -37,20 +36,22 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup MaxMessageLength = 4096, }; - public Capabilities Capabilities => _capabilities; - /// Создать адаптер Telegram. public TelegramAdapter(ILogger logger, string token) { _logger = logger; _token = token; - _messagerType = "Telegram: " + Guid.NewGuid().ToString(); } /// ///Идентификатор мессенджера / адаптера /// - public string MessagerType => _messagerType; + public string MessengerType { get; set; } = "Telegram: " + Guid.NewGuid().ToString(); + + /// + /// Доступные возможности адаптера. + /// + public Capabilities Capabilities => _capabilities; /// /// Запустить polling для приема обновлений от Telegram. @@ -62,14 +63,14 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup _client.StartReceiving( updateHandler: async (_, update, ct2) => { - var mapped = TelegramUpdateMapper.Map(_messagerType, update, _client); + var mapped = TelegramUpdateMapper.Map(MessengerType, update, _client); if (mapped is not null) await onUpdate(mapped); }, errorHandler: async (_, ex, ct2) => { - _logger.Log(LogLevel.Warn, $"{_messagerType} error.", ex); + _logger.Log(LogLevel.Warn, $"{MessengerType} error.", ex); await Task.CompletedTask; }, @@ -78,7 +79,7 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup var me = await _client.GetMe(); - _logger.Log(LogLevel.Info, $"{_messagerType} started: @{me.Username}"); + _logger.Log(LogLevel.Info, $"{MessengerType} started: @{me.Username}"); return; } @@ -90,7 +91,7 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup { if (_client is null) { - _logger.Log(LogLevel.Critical, $"{_messagerType} client is not initialized."); + _logger.Log(LogLevel.Critical, $"{MessengerType} client is not initialized."); return; } @@ -160,7 +161,7 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup { if (_client is null) { - _logger.Log(LogLevel.Critical, $"{_messagerType} client is not initialized."); + _logger.Log(LogLevel.Critical, $"{MessengerType} client is not initialized."); return; } @@ -213,7 +214,7 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup { if (_client is null) { - _logger.Log(LogLevel.Critical, $"{_messagerType} client is not initialized."); + _logger.Log(LogLevel.Critical, $"{MessengerType} client is not initialized."); return null; } @@ -237,7 +238,7 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup { if (_client is null) { - _logger.Log(LogLevel.Critical, $"{_messagerType} client is not initialized."); + _logger.Log(LogLevel.Critical, $"{MessengerType} client is not initialized."); return; } diff --git a/BotPages.Telegram/TelegramAlbumBuilder.cs b/BotPages.Telegram/TelegramAlbumBuilder.cs index 728e25b..9504539 100644 --- a/BotPages.Telegram/TelegramAlbumBuilder.cs +++ b/BotPages.Telegram/TelegramAlbumBuilder.cs @@ -49,7 +49,7 @@ public sealed class TelegramAlbumBuilder : IAlbumBuilder var chatId = long.Parse(_ctx.Update.Chat.Id); - if (!_ctx.Update.Chat.Capabilities.SupportsAlbums) + if (!_adapter.Capabilities.SupportsAlbums) { _logger.Log(LogLevel.Warn, "Albums not supported. Degraded to sequential sends."); foreach (var (file, caption) in _items) diff --git a/BotPages.Telegram/TelegramUpdateMapper.cs b/BotPages.Telegram/TelegramUpdateMapper.cs index cb737ba..8c5728e 100644 --- a/BotPages.Telegram/TelegramUpdateMapper.cs +++ b/BotPages.Telegram/TelegramUpdateMapper.cs @@ -19,7 +19,7 @@ public static class TelegramUpdateMapper /// /// Маппинг Telegram Update в UpdateContext BotPages. /// - public static UpdateContext Map(string MessagerType, Update update, TelegramBotClient client) + public static UpdateContext Map(string MessengerType, Update update, TelegramBotClient client) { var chat = update.Message?.Chat ?? update.CallbackQuery?.Message?.Chat; var user = update.Message?.From ?? update.CallbackQuery?.From; @@ -122,7 +122,7 @@ public static class TelegramUpdateMapper return new UpdateContext { - MessengerType = MessagerType, + MessengerType = MessengerType, User = userContext, Chat = chatContext, Text = text, diff --git a/Demo/Program.cs b/Demo/Program.cs index 6863801..f6b3e70 100644 --- a/Demo/Program.cs +++ b/Demo/Program.cs @@ -23,7 +23,7 @@ namespace Demo .MapCommand("/start") .AddMiddleware(new ErrorHandlingMiddleware(logger)) .AddMiddleware(new LoggingMiddleware(logger)) - .AddTelegramAdapter(token) + .AddTelegramAdapter(token, "Telegram") .Build(cts.Token); Console.ReadKey();