Insight.TelegramBot is a powerful and flexible SDK for implementing Telegram bot infrastructure in C#. It provides a set of tools and abstractions to simplify the development of Telegram bots, including update handling, state management, and easy integration with ASP.NET Core applications.
- Wrapper over Telegram.Bot with additional functionality
- Support for both polling and webhook update reception methods
- Flexible update handling system with matchers and handlers
- State management for bot conversations
- Easy integration with dependency injection
- Support for various message types (text, photo, document, etc.)
- Extensible architecture for custom implementations
Before installation, you shoud add nuget source for Telegram.Bot package:
dotnet nuget add source https://pkgs.dev.azure.com/tgbots/Telegram.Bot/_packaging/release/nuget/v3/index.json -n Telegram.Bot
You can install Insight.TelegramBot via NuGet Package Manager:
dotnet add package Insight.TelegramBot
Depending on your needs, you may also want to install additional packages:
dotnet add package Insight.TelegramBot.Handling
dotnet add package Insight.TelegramBot.DependencyInjection
dotnet add package Insight.TelegramBot.DependencyInjection.Polling
dotnet add package Insight.TelegramBot.DependencyInjection.WebHook
Here's a basic example of how to set up a simple Telegram bot using Insight.TelegramBot:
using Insight.TelegramBot.DependencyInjection.Infrastructure;
using Insight.TelegramBot.DependencyInjection.Polling;
using Microsoft.Extensions.DependencyInjection;
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddTelegramBot(builder => builder
.WithTelegramBotClient(client => client
.WithMicrosoftHttpClientFactory()
.WithLifetime(ServiceLifetime.Singleton))
.WithOptions(opt => opt.FromConfiguration(Configuration))
.WithPolling(polling => polling
.WithExceptionHandler<LoggingPollingExceptionHandler>()));
}
}
Insight.TelegramBot provides a flexible system for handling updates. You can create custom handlers and matchers to process different types of updates:
public class StartMessageHandler : IMatchingUpdateHandler<StartMessageMatcher>
{
public Task Handle(Update update, CancellationToken cancellationToken = default)
{
// Handle the /start command
return Task.CompletedTask;
}
}
public class StartMessageMatcher : TextEqualsUpdateMatcher
{
public StartMessageMatcher()
{
Template = "/start";
}
}
Insight.TelegramBot supports both webhook and polling methods for receiving updates. You can choose the appropriate method based on your deployment environment and requirements.
services.AddTelegramBot(builder => builder
.WithTelegramBotClient(/* ... */)
.WithOptions(/* ... */)
.WithWebHook(webhook => webhook
.WithDefaultUpdateController()));
services.AddTelegramBot(builder => builder
.WithTelegramBotClient(/* ... */)
.WithOptions(/* ... */)
.WithPolling(polling => polling
.WithExceptionHandler<LoggingPollingExceptionHandler>()));
For more advanced usage, including custom update processors, state management, and complex message handling, please refer to the samples in the repository:
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.