using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Telegram.Bot;
using Telegram.Bot.Polling;
using Telegrator.Hosting.Configuration;
using Telegrator.Configuration;
using Telegrator.Hosting;
using Telegrator.Hosting.Components;
using Telegrator.Hosting.Providers;
using Telegrator.MadiatorCore;
#pragma warning disable IDE0001
namespace Telegrator.Hosting
{
///
/// Represents a hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more.
///
public class TelegramBotHostBuilder : ITelegramBotHostBuilder
{
private readonly HostApplicationBuilder _innerBuilder;
private readonly TelegramBotHostBuilderSettings _settings;
private readonly HostHandlersCollection _handlers;
///
public IHandlersCollection Handlers => _handlers;
///
public IServiceCollection Services => _innerBuilder.Services;
///
public IConfigurationManager Configuration => _innerBuilder.Configuration;
///
public ILoggingBuilder Logging => _innerBuilder.Logging;
///
public IHostEnvironment Environment => _innerBuilder.Environment;
///
/// Initializes a new instance of the class.
///
///
///
internal TelegramBotHostBuilder(HostApplicationBuilder hostApplicationBuilder, TelegramBotHostBuilderSettings? settings = null)
{
_innerBuilder = hostApplicationBuilder;
_settings = settings ?? new TelegramBotHostBuilderSettings();
_handlers = new HostHandlersCollection(Services, _settings);
_innerBuilder.Logging.ClearProviders();
Services.Configure(Configuration.GetSection(nameof(TelegramBotOptions)));
Services.Configure(Configuration.GetSection(nameof(ReceiverOptions)));
Services.Configure(Configuration.GetSection(nameof(TelegramBotClientOptions)), new TelegramBotClientOptionsProxy());
}
///
/// Builds the host.
///
///
public TelegramBotHost Build()
{
foreach (PreBuildingRoutine preBuildRoutine in _handlers.PreBuilderRoutines)
{
try
{
preBuildRoutine.Invoke(this);
}
catch (NotImplementedException)
{
_ = 0xBAD + 0xC0DE;
}
}
return new TelegramBotHost(_innerBuilder, _handlers);
}
}
}