Доработан адаптер телеграма
This commit is contained in:
@@ -15,7 +15,4 @@ public sealed class ChatContext
|
||||
|
||||
/// <summary>Идентификатор треда (опционально).</summary>
|
||||
public string? ThreadId { get; init; }
|
||||
|
||||
/// <summary>Возможности мессенджера.</summary>
|
||||
public Capabilities Capabilities { get; init; } = new();
|
||||
}
|
||||
|
||||
@@ -12,11 +12,15 @@ public static class BotPagesAppExtension
|
||||
/// </summary>
|
||||
/// <param name="app"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <param name="messengerType"></param>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
/// <summary>Создать адаптер Telegram.</summary>
|
||||
public TelegramAdapter(ILogger logger, string token)
|
||||
{
|
||||
_logger = logger;
|
||||
_token = token;
|
||||
_messagerType = "Telegram: " + Guid.NewGuid().ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///Идентификатор мессенджера / адаптера
|
||||
/// </summary>
|
||||
public string MessagerType => _messagerType;
|
||||
public string MessengerType { get; set; } = "Telegram: " + Guid.NewGuid().ToString();
|
||||
|
||||
/// <summary>
|
||||
/// Доступные возможности адаптера.
|
||||
/// </summary>
|
||||
public Capabilities Capabilities => _capabilities;
|
||||
|
||||
/// <summary>
|
||||
/// Запустить 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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -19,7 +19,7 @@ public static class TelegramUpdateMapper
|
||||
/// <summary>
|
||||
/// Маппинг Telegram Update в UpdateContext BotPages.
|
||||
/// </summary>
|
||||
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,
|
||||
|
||||
@@ -23,7 +23,7 @@ namespace Demo
|
||||
.MapCommand<WelcomePage>("/start")
|
||||
.AddMiddleware(new ErrorHandlingMiddleware(logger))
|
||||
.AddMiddleware(new LoggingMiddleware(logger))
|
||||
.AddTelegramAdapter(token)
|
||||
.AddTelegramAdapter(token, "Telegram")
|
||||
.Build(cts.Token);
|
||||
|
||||
Console.ReadKey();
|
||||
|
||||
Reference in New Issue
Block a user