2025-07-24 23:19:59 +04:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2026-03-06 23:19:24 +04:00
|
|
|
using Telegrator.Core;
|
2025-07-24 23:19:59 +04:00
|
|
|
|
|
|
|
|
namespace Telegrator.Hosting
|
|
|
|
|
{
|
2025-07-26 00:01:46 +04:00
|
|
|
/// <summary>
|
|
|
|
|
/// Represents a hosted telegram bot
|
|
|
|
|
/// </summary>
|
2025-07-24 23:19:59 +04:00
|
|
|
public class TelegramBotHost : ITelegramBotHost
|
|
|
|
|
{
|
|
|
|
|
private readonly IHost _innerHost;
|
2025-08-19 04:33:02 +04:00
|
|
|
private readonly IServiceProvider _serviceProvider;
|
2025-07-24 23:19:59 +04:00
|
|
|
private readonly IUpdateRouter _updateRouter;
|
|
|
|
|
private readonly ILogger<TelegramBotHost> _logger;
|
|
|
|
|
|
|
|
|
|
private bool _disposed;
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
2025-08-19 04:33:02 +04:00
|
|
|
public IServiceProvider Services => _serviceProvider;
|
2025-07-24 23:19:59 +04:00
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public IUpdateRouter UpdateRouter => _updateRouter;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This application's logger
|
|
|
|
|
/// </summary>
|
|
|
|
|
public ILogger<TelegramBotHost> Logger => _logger;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="TelegramBotHost"/> class.
|
|
|
|
|
/// </summary>
|
2025-08-19 04:33:02 +04:00
|
|
|
/// <param name="hostApplicationBuilder">The proxied instance of host builder.</param>
|
2025-07-26 00:01:46 +04:00
|
|
|
/// <param name="handlers"></param>
|
2025-08-19 04:33:02 +04:00
|
|
|
public TelegramBotHost(HostApplicationBuilder hostApplicationBuilder, IHandlersCollection handlers)
|
2025-07-24 23:19:59 +04:00
|
|
|
{
|
2025-08-01 13:50:21 +04:00
|
|
|
// Registering this host in services for easy access
|
2025-08-19 04:33:02 +04:00
|
|
|
RegisterHostServices(hostApplicationBuilder.Services, handlers);
|
2025-08-01 13:50:21 +04:00
|
|
|
|
|
|
|
|
// Building proxy hoster
|
2025-07-24 23:19:59 +04:00
|
|
|
_innerHost = hostApplicationBuilder.Build();
|
2025-08-19 04:33:02 +04:00
|
|
|
_serviceProvider = _innerHost.Services;
|
2026-03-06 21:02:19 +04:00
|
|
|
_innerHost.UseTelegrator();
|
2025-08-01 13:50:21 +04:00
|
|
|
|
|
|
|
|
// Reruesting services for this host
|
2025-07-24 23:19:59 +04:00
|
|
|
_updateRouter = Services.GetRequiredService<IUpdateRouter>();
|
|
|
|
|
_logger = Services.GetRequiredService<ILogger<TelegramBotHost>>();
|
|
|
|
|
|
2025-08-01 13:50:21 +04:00
|
|
|
// Logging registering handlers in DEBUG purposes
|
2026-03-06 21:02:19 +04:00
|
|
|
_logger.LogHandlers(handlers);
|
2025-07-24 23:19:59 +04:00
|
|
|
}
|
|
|
|
|
|
2025-07-26 00:01:46 +04:00
|
|
|
/// <summary>
|
|
|
|
|
/// Creates new <see cref="TelegramBotHostBuilder"/> with default configuration, services and long-polling update receiving scheme
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2025-07-24 23:19:59 +04:00
|
|
|
public static TelegramBotHostBuilder CreateBuilder()
|
|
|
|
|
{
|
2025-07-26 00:01:46 +04:00
|
|
|
HostApplicationBuilder innerBuilder = new HostApplicationBuilder(settings: null);
|
|
|
|
|
TelegramBotHostBuilder builder = new TelegramBotHostBuilder(innerBuilder, null);
|
2025-07-24 23:19:59 +04:00
|
|
|
builder.Services.AddTelegramBotHostDefaults();
|
|
|
|
|
builder.Services.AddTelegramReceiver();
|
|
|
|
|
return builder;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-26 00:01:46 +04:00
|
|
|
/// <summary>
|
|
|
|
|
/// Creates new <see cref="TelegramBotHostBuilder"/> with default services and long-polling update receiving scheme
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2025-07-24 23:19:59 +04:00
|
|
|
public static TelegramBotHostBuilder CreateBuilder(TelegramBotHostBuilderSettings? settings)
|
|
|
|
|
{
|
2025-07-26 00:01:46 +04:00
|
|
|
HostApplicationBuilder innerBuilder = new HostApplicationBuilder(settings?.ToApplicationBuilderSettings());
|
|
|
|
|
TelegramBotHostBuilder builder = new TelegramBotHostBuilder(innerBuilder, settings);
|
2025-07-24 23:19:59 +04:00
|
|
|
builder.Services.AddTelegramBotHostDefaults();
|
|
|
|
|
builder.Services.AddTelegramReceiver();
|
|
|
|
|
return builder;
|
|
|
|
|
}
|
2025-07-26 00:01:46 +04:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates new EMPTY <see cref="TelegramBotHostBuilder"/> WITHOUT any services or update receiving schemes
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static TelegramBotHostBuilder CreateEmptyBuilder()
|
|
|
|
|
{
|
|
|
|
|
HostApplicationBuilder innerBuilder = Host.CreateEmptyApplicationBuilder(null);
|
|
|
|
|
return new TelegramBotHostBuilder(innerBuilder, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates new EMPTY <see cref="TelegramBotHostBuilder"/> WITHOUT any services or update receiving schemes
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static TelegramBotHostBuilder CreateEmptyBuilder(TelegramBotHostBuilderSettings? settings)
|
|
|
|
|
{
|
|
|
|
|
HostApplicationBuilder innerBuilder = Host.CreateEmptyApplicationBuilder(null);
|
|
|
|
|
return new TelegramBotHostBuilder(innerBuilder, settings);
|
|
|
|
|
}
|
2025-07-24 23:19:59 +04:00
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public async Task StartAsync(CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
await _innerHost.StartAsync(cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public async Task StopAsync(CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
await _innerHost.StopAsync(cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Disposes the host.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (_disposed)
|
|
|
|
|
return;
|
|
|
|
|
|
2025-07-28 20:35:48 +04:00
|
|
|
_innerHost.Dispose();
|
|
|
|
|
|
2025-07-24 23:19:59 +04:00
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
_disposed = true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-19 04:33:02 +04:00
|
|
|
private void RegisterHostServices(IServiceCollection services, IHandlersCollection handlers)
|
2025-07-24 23:19:59 +04:00
|
|
|
{
|
2025-08-19 04:33:02 +04:00
|
|
|
//services.RemoveAll<IHost>();
|
|
|
|
|
//services.AddSingleton<IHost>(this);
|
2025-07-24 23:19:59 +04:00
|
|
|
|
2025-08-19 04:33:02 +04:00
|
|
|
services.AddSingleton<ITelegramBotHost>(this);
|
|
|
|
|
services.AddSingleton<ITelegratorBot>(this);
|
2025-07-24 23:19:59 +04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|