using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Telegram.Bot; using Telegram.Bot.Polling; using Telegrator.Hosting.Components; using Telegrator.Hosting.Configuration; using Telegrator.Hosting.Providers; using Telegrator.MadiatorCore; #pragma warning disable IDE0001 namespace Telegrator.Hosting.Web { /// /// Represents a web hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more. /// public class TelegramBotWebHostBuilder : ITelegramBotHostBuilder { private readonly WebApplicationBuilder _innerBuilder; private readonly TelegramBotWebOptions _settings; private readonly HostHandlersCollection _handlers; /// public IHandlersCollection Handlers => _handlers; /// public IConfigurationManager Configuration => _innerBuilder.Configuration; /// public ILoggingBuilder Logging => _innerBuilder.Logging; /// public IServiceCollection Services => _innerBuilder.Services; /// public IHostEnvironment Environment => _innerBuilder.Environment; internal TelegramBotWebHostBuilder(WebApplicationBuilder webApplicationBuilder, TelegramBotWebOptions settings) { _innerBuilder = webApplicationBuilder; _settings = settings ?? throw new ArgumentNullException(nameof(settings)); _handlers = new HostHandlersCollection(Services, _settings); Services.AddSingleton>(Options.Create(settings)); Services.Configure(Configuration.GetSection(nameof(TelegratorOptions))); Services.Configure(Configuration.GetSection(nameof(TelegramBotClientOptions)), new TelegramBotClientOptionsProxy()); } /// /// Builds the host. /// /// public TelegramBotWebHost Build() { foreach (PreBuildingRoutine preBuildRoutine in _handlers.PreBuilderRoutines) { try { preBuildRoutine.Invoke(this); } catch (NotImplementedException) { _ = 0xBAD + 0xC0DE; } } return new TelegramBotWebHost(_innerBuilder, _handlers); } } }