Files
Telegrator/src/Telegrator.Hosting/Hosting/TelegramBotHost.cs
T

120 lines
4.3 KiB
C#
Raw Normal View History

2025-07-24 23:19:59 +04:00
using Microsoft.Extensions.DependencyInjection;
2026-03-07 02:35:53 +04:00
using Microsoft.Extensions.DependencyInjection.Extensions;
2025-07-24 23:19:59 +04:00
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
{
/// <summary>
/// Represents a hosted telegram bot
/// </summary>
2026-03-07 02:35:53 +04:00
public class TelegramBotHost : IHost, ITelegratorBot
2025-07-24 23:19:59 +04:00
{
private readonly IHost _innerHost;
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/>
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>
/// <param name="hostApplicationBuilder">The proxied instance of host builder.</param>
2026-03-07 02:35:53 +04:00
public TelegramBotHost(HostApplicationBuilder hostApplicationBuilder)
2025-07-24 23:19:59 +04:00
{
// Registering this host in services for easy access
2026-03-07 00:17:31 +04:00
hostApplicationBuilder.Services.AddSingleton<ITelegratorBot>(this);
// Building proxy hoster
2025-07-24 23:19:59 +04:00
_innerHost = hostApplicationBuilder.Build();
_serviceProvider = _innerHost.Services;
2026-03-06 21:02:19 +04:00
_innerHost.UseTelegrator();
// Reruesting services for this host
2025-07-24 23:19:59 +04:00
_updateRouter = Services.GetRequiredService<IUpdateRouter>();
_logger = Services.GetRequiredService<ILogger<TelegramBotHost>>();
}
/// <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()
{
HostApplicationBuilder innerBuilder = new HostApplicationBuilder(settings: null);
TelegramBotHostBuilder builder = new TelegramBotHostBuilder(innerBuilder, null);
2025-07-24 23:19:59 +04:00
return builder;
}
/// <summary>
/// Creates new <see cref="TelegramBotHostBuilder"/> with default services and long-polling update receiving scheme
/// </summary>
/// <returns></returns>
2026-03-07 02:35:53 +04:00
public static TelegramBotHostBuilder CreateBuilder(HostApplicationBuilderSettings? settings)
2025-07-24 23:19:59 +04:00
{
2026-03-07 02:35:53 +04:00
HostApplicationBuilder innerBuilder = new HostApplicationBuilder(settings);
TelegramBotHostBuilder builder = new TelegramBotHostBuilder(innerBuilder, settings);
2025-07-24 23:19:59 +04:00
return builder;
}
/// <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>
2026-03-07 02:35:53 +04:00
public static TelegramBotHostBuilder CreateEmptyBuilder(HostApplicationBuilderSettings? 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;
_innerHost.Dispose();
2025-07-24 23:19:59 +04:00
GC.SuppressFinalize(this);
_disposed = true;
}
}
}