2025-07-28 20:35:48 +04:00
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 ;
2025-07-24 23:19:59 +04:00
2025-07-28 20:35:48 +04:00
#pragma warning disable IDE0001
2025-07-24 23:19:59 +04:00
namespace Telegrator.Hosting.Web
{
2025-07-28 20:35:48 +04:00
/// <summary>
/// Represents a web hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more.
/// </summary>
public class TelegramBotWebHostBuilder : ITelegramBotHostBuilder
2025-07-24 23:19:59 +04:00
{
2025-07-28 20:35:48 +04:00
private readonly WebApplicationBuilder _innerBuilder ;
private readonly TelegramBotWebOptions _settings ;
private readonly HostHandlersCollection _handlers ;
2025-07-24 23:19:59 +04:00
2025-07-28 20:35:48 +04:00
/// <inheritdoc/>
public IHandlersCollection Handlers = > _handlers ;
/// <inheritdoc/>
public IConfigurationManager Configuration = > _innerBuilder . Configuration ;
/// <inheritdoc/>
public ILoggingBuilder Logging = > _innerBuilder . Logging ;
/// <inheritdoc/>
public IServiceCollection Services = > _innerBuilder . Services ;
/// <inheritdoc/>
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 ) ;
}
/// <summary>
/// Builds the host.
/// </summary>
/// <returns></returns>
public TelegramBotWebHost Build ( )
{
foreach ( PreBuildingRoutine preBuildRoutine in _handlers . PreBuilderRoutines )
{
try
{
preBuildRoutine . Invoke ( this ) ;
}
catch ( NotImplementedException )
{
_ = 0xBAD + 0xC0DE ;
}
}
2025-08-02 02:32:38 +04:00
if ( ! _settings . DisableAutoConfigure )
{
Services . Configure < TelegratorWebOptions > ( Configuration . GetSection ( nameof ( TelegratorWebOptions ) ) ) ;
Services . Configure < TelegramBotClientOptions > ( Configuration . GetSection ( nameof ( TelegramBotClientOptions ) ) , new TelegramBotClientOptionsProxy ( ) ) ;
}
2025-08-04 04:33:34 +04:00
else
{
if ( null = = Services . SingleOrDefault ( srvc = > srvc . ImplementationType = = typeof ( IOptions < TelegratorWebOptions > ) ) )
throw new MissingMemberException ( "Auto configuration disabled, yet no options of type 'TelegratorWebOptions' wasn't registered. This configuration is runtime required!" ) ;
if ( null = = Services . SingleOrDefault ( srvc = > srvc . ImplementationType = = typeof ( IOptions < TelegramBotClientOptions > ) ) )
throw new MissingMemberException ( "Auto configuration disabled, yet no options of type 'TelegramBotClientOptions' wasn't registered. This configuration is runtime required!" ) ;
}
2025-08-02 02:32:38 +04:00
2025-08-02 03:19:52 +04:00
Services . AddSingleton < IConfigurationManager > ( Configuration ) ;
2025-08-02 02:32:38 +04:00
Services . AddSingleton < IOptions < TelegratorOptions > > ( Options . Create ( _settings ) ) ;
2025-07-28 20:35:48 +04:00
return new TelegramBotWebHost ( _innerBuilder , _handlers ) ;
}
2025-07-24 23:19:59 +04:00
}
}