Overview
The TelegramBotHost class represents a hosted Telegram bot application. It wraps the .NET Generic Host and provides integration with the Telegrator framework for handling updates via long-polling.
Namespace: Telegrator.Hosting
Inheritance: TelegramBotHost implements ITelegramBotHost
Properties
Services
IServiceProvider Services { get; }
Gets the service provider for the host, allowing access to registered services.
UpdateRouter
IUpdateRouter UpdateRouter { get; }
Gets the update router responsible for dispatching incoming Telegram updates to the appropriate handlers.
Logger
ILogger<TelegramBotHost> Logger { get; }
Gets the logger instance for the host application.
Static Methods
CreateBuilder()
public static TelegramBotHostBuilder CreateBuilder()
Creates a new TelegramBotHostBuilder with default configuration, services, and long-polling update receiving scheme.
Returns: A configured TelegramBotHostBuilder instance.
Example:
var builder = TelegramBotHost.CreateBuilder();
builder.Configuration["TelegramBotClientOptions:Token"] = "YOUR_BOT_TOKEN";
var host = builder.Build();
await host.StartAsync();
CreateBuilder(TelegramBotHostBuilderSettings)
public static TelegramBotHostBuilder CreateBuilder(TelegramBotHostBuilderSettings? settings)
Creates a new TelegramBotHostBuilder with custom settings and long-polling update receiving scheme.
settings
TelegramBotHostBuilderSettings
Configuration settings for the host builder, including environment name, content root path, and other host options.
Returns: A configured TelegramBotHostBuilder instance.
Example:
var settings = new TelegramBotHostBuilderSettings
{
EnvironmentName = "Production",
ContentRootPath = "/app",
DisableAutoConfigure = false
};
var builder = TelegramBotHost.CreateBuilder(settings);
var host = builder.Build();
CreateEmptyBuilder()
public static TelegramBotHostBuilder CreateEmptyBuilder()
Creates a new EMPTY TelegramBotHostBuilder WITHOUT any default services or update receiving schemes. Use this when you need full control over service registration.
Returns: An empty TelegramBotHostBuilder instance.
Example:
var builder = TelegramBotHost.CreateEmptyBuilder();
// Manually add required services
builder.Services.AddTelegramBotHostDefaults();
builder.Services.AddTelegramReceiver();
var host = builder.Build();
CreateEmptyBuilder(TelegramBotHostBuilderSettings)
public static TelegramBotHostBuilder CreateEmptyBuilder(TelegramBotHostBuilderSettings? settings)
Creates a new EMPTY TelegramBotHostBuilder with custom settings but WITHOUT any default services or update receiving schemes.
settings
TelegramBotHostBuilderSettings
Configuration settings for the host builder.
Returns: An empty TelegramBotHostBuilder instance.
Instance Methods
StartAsync()
public async Task StartAsync(CancellationToken cancellationToken = default)
Starts the host and begins receiving updates from Telegram.
cancellationToken
CancellationToken
default:"default"
Token to cancel the start operation.
Example:
var host = TelegramBotHost.CreateBuilder().Build();
await host.StartAsync();
// Keep the application running
await Task.Delay(Timeout.Infinite);
StopAsync()
public async Task StopAsync(CancellationToken cancellationToken = default)
Stops the host and halts update receiving.
cancellationToken
CancellationToken
default:"default"
Token to cancel the stop operation.
Example:
Dispose()
Disposes the host and releases all resources.
Example:
using var host = TelegramBotHost.CreateBuilder().Build();
await host.StartAsync();
// Host will be disposed automatically
Complete Example
using Telegrator.Hosting;
using Microsoft.Extensions.DependencyInjection;
// Create and configure the builder
var builder = TelegramBotHost.CreateBuilder();
// Configure bot token
builder.Configuration["TelegramBotClientOptions:Token"] = "YOUR_BOT_TOKEN";
// Register handlers
builder.Handlers.OnMessage(async (context, cancellationToken) =>
{
await context.Client.SendMessage(
context.Update.Message.Chat.Id,
"Hello from Telegrator!"
);
});
// Add custom services
builder.Services.AddSingleton<MyCustomService>();
// Build and start the host
using var host = builder.Build();
await host.StartAsync();
// Keep running
await Task.Delay(Timeout.Infinite);
See Also