2025-07-24 23:19:59 +04:00
using Microsoft.Extensions.Configuration ;
using Microsoft.Extensions.DependencyInjection ;
using Microsoft.Extensions.Hosting ;
using Microsoft.Extensions.Logging ;
2025-08-02 02:32:38 +04:00
using Microsoft.Extensions.Options ;
2025-07-24 23:19:59 +04:00
using Telegram.Bot ;
using Telegram.Bot.Polling ;
using Telegrator.Hosting ;
using Telegrator.Hosting.Components ;
2025-07-28 20:35:48 +04:00
using Telegrator.Hosting.Configuration ;
2025-07-24 23:19:59 +04:00
using Telegrator.Hosting.Providers ;
2025-08-19 04:33:02 +04:00
using Telegrator.Hosting.Providers.Components ;
2025-07-24 23:19:59 +04:00
using Telegrator.MadiatorCore ;
#pragma warning disable IDE0001
namespace Telegrator.Hosting
{
2025-07-26 00:01:46 +04:00
/// <summary>
/// Represents a hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more.
/// </summary>
2025-07-24 23:19:59 +04:00
public class TelegramBotHostBuilder : ITelegramBotHostBuilder
{
private readonly HostApplicationBuilder _innerBuilder ;
private readonly TelegramBotHostBuilderSettings _settings ;
2025-08-19 04:33:02 +04:00
private readonly IHandlersCollection _handlers ;
2025-07-24 23:19:59 +04:00
/// <inheritdoc/>
public IHandlersCollection Handlers = > _handlers ;
/// <inheritdoc/>
public IServiceCollection Services = > _innerBuilder . Services ;
/// <inheritdoc/>
public IConfigurationManager Configuration = > _innerBuilder . Configuration ;
/// <inheritdoc/>
public ILoggingBuilder Logging = > _innerBuilder . Logging ;
/// <inheritdoc/>
public IHostEnvironment Environment = > _innerBuilder . Environment ;
/// <summary>
/// Initializes a new instance of the <see cref="TelegramBotHostBuilder"/> class.
/// </summary>
2025-07-26 00:01:46 +04:00
/// <param name="hostApplicationBuilder"></param>
/// <param name="settings"></param>
2025-08-19 04:33:02 +04:00
public TelegramBotHostBuilder ( HostApplicationBuilder hostApplicationBuilder , TelegramBotHostBuilderSettings ? settings = null )
2025-07-24 23:19:59 +04:00
{
2025-08-19 04:33:02 +04:00
_innerBuilder = hostApplicationBuilder ? ? throw new ArgumentNullException ( nameof ( hostApplicationBuilder ) ) ;
2025-07-24 23:19:59 +04:00
_settings = settings ? ? new TelegramBotHostBuilderSettings ( ) ;
_handlers = new HostHandlersCollection ( Services , _settings ) ;
2025-07-28 01:40:04 +04:00
_innerBuilder . Logging . ClearProviders ( ) ;
2025-07-24 23:19:59 +04:00
}
2025-08-19 04:33:02 +04:00
/// <summary>
/// Initializes a new instance of the <see cref="TelegramBotHostBuilder"/> class.
/// </summary>
/// <param name="hostApplicationBuilder"></param>
/// <param name="handlers"></param>
/// <param name="settings"></param>
public TelegramBotHostBuilder ( HostApplicationBuilder hostApplicationBuilder , IHandlersCollection handlers , TelegramBotHostBuilderSettings ? settings = null )
{
_innerBuilder = hostApplicationBuilder ? ? throw new ArgumentNullException ( nameof ( hostApplicationBuilder ) ) ;
_settings = settings ? ? new TelegramBotHostBuilderSettings ( ) ;
_handlers = handlers ? ? throw new ArgumentNullException ( nameof ( handlers ) ) ;
_innerBuilder . Logging . ClearProviders ( ) ;
}
2025-07-24 23:19:59 +04:00
/// <summary>
/// Builds the host.
/// </summary>
/// <returns></returns>
public TelegramBotHost Build ( )
{
2025-08-19 04:33:02 +04:00
if ( _handlers is IHostHandlersCollection hostHandlers )
2025-07-24 23:19:59 +04:00
{
2025-08-19 04:33:02 +04:00
foreach ( PreBuildingRoutine preBuildRoutine in hostHandlers . PreBuilderRoutines )
2025-07-24 23:19:59 +04:00
{
2025-08-19 04:33:02 +04:00
try
{
preBuildRoutine . Invoke ( this ) ;
}
catch ( NotImplementedException )
{
_ = 0xBAD + 0xC0DE ;
}
2025-07-24 23:19:59 +04:00
}
}
2025-08-02 00:27:23 +04:00
if ( ! _settings . DisableAutoConfigure )
{
Services . Configure < ReceiverOptions > ( Configuration . GetSection ( nameof ( ReceiverOptions ) ) ) ;
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<ReceiverOptions>)))
throw new MissingMemberException("Auto configuration disabled, yet no options of type 'ReceiverOptions' 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 00:27:23 +04:00
2025-08-02 02:32:38 +04:00
Services . AddSingleton < IOptions < TelegratorOptions > > ( Options . Create ( _settings ) ) ;
2025-08-02 03:19:52 +04:00
Services . AddSingleton < IConfigurationManager > ( Configuration ) ;
2025-07-24 23:19:59 +04:00
return new TelegramBotHost ( _innerBuilder , _handlers ) ;
}
}
}