Telegram.Bot.Controllers
1.1.5
dotnet add package Telegram.Bot.Controllers --version 1.1.5
NuGet\Install-Package Telegram.Bot.Controllers -Version 1.1.5
<PackageReference Include="Telegram.Bot.Controllers" Version="1.1.5" />
<PackageVersion Include="Telegram.Bot.Controllers" Version="1.1.5" />
<PackageReference Include="Telegram.Bot.Controllers" />
paket add Telegram.Bot.Controllers --version 1.1.5
#r "nuget: Telegram.Bot.Controllers, 1.1.5"
#:package Telegram.Bot.Controllers@1.1.5
#addin nuget:?package=Telegram.Bot.Controllers&version=1.1.5
#tool nuget:?package=Telegram.Bot.Controllers&version=1.1.5
Telegram.Bot.Controllers
A lightweight and easy-to-use C# library for building conversational Telegram bots using a controller-based architecture.
What It Does
This library helps you build stateful, multi-step conversations with your Telegram bot in a clean, organized way. It automatically:
- Tracks conversation steps
- Builds dynamic command menus at each step
- Handles cancellation
- Supports override commands that reset the conversation
- Recovers from invalid input gracefully
All you need to do is define your conversation flow using simple controllers and attributes.
Getting Started
Prerequisites
- .NET 8 SDK or later
- A Telegram Bot Token (from @BotFather)
Installation
Install via NuGet:
dotnet add package Telegram.Bot.Controllers
Setup
Default
var host = Host.CreateDefaultBuilder(args)
.ConfigureServices((context, services) =>
{
var token = ""; // Replace with your Telegram bot token
services.AddTelegramBotControllers(token);
})
.Build();
await host.RunAsync();
Customized
var host = Host.CreateDefaultBuilder(args)
.ConfigureServices((context, services) =>
{
var token = "";//your telegram bot token here
services.AddTelegramBotControllers(token, (cfg) =>
{
cfg.SetCancellation("отмена", "Получена команда 'отмена'.");
cfg.SetCommandNotFoundText("Команда не найдена. Доступные команды - в меню");
cfg.SetConversationClosedText("Беседа закрыта");
});
})
.Build();
await host.RunAsync();
- SetCancellation: set a custom command for cancellation and a message that will be dispayed on cancellation.
- SetCommandNotFoundText: set a message the will be displayed when the command was not found. If not set nothing is diplayed
- SetConversationClosedText: set a message that will be displayed when the conversation reaches the last step. If not set nothing is displayed
Writing Controllers
Create classes that inherit from TelegramBotControllerBase, and use the [Command] attribute to define your conversation steps. Path segments correspond the conversation steps.
Example: Commands on Method Level
You can define the full command path directly on a method.
public class StartController : TelegramBotControllerBase
{
[Command("start")] //matches start (top-level command)
public async Task Start(CancellationToken ct)
{
await MessageService.SendMessageAsync(Update.Message.Chat.Id, "Hello! You said 'start'", ct);
}
[Command("about")] //matches about (top-level command)
public async Task Start(CancellationToken ct)
{
await MessageService.SendMessageAsync(Update.Message.Chat.Id, "Hello! You said 'about'", ct);
}
[Command("start/nested")] //matches start/nested
public async Task Start(CancellationToken ct)
{
await MessageService.SendMessageAsync(Update.Message.Chat.Id, "Hello! You said 'start' then 'nested'", ct);
}
}
Example: Base paths
You can extract the base path into the classlevel attribute.
[Command("start/menu")] //sets the base path
public class StartMenuController : TelegramBotControllerBase
{
[Command] //matches start/menu
public async Task Start(CancellationToken ct)
{
await MessageService.SendMessageAsync(Update.Message.Chat.Id, "Hello! You said 'start' then 'menu'", ct);
}
[Command("nested")] //matches start/menu/nested
public async Task Start(CancellationToken ct)
{
await MessageService.SendMessageAsync(Update.Message.Chat.Id, "Hello! You said 'start' then 'menu' then 'nested'", ct);
}
}
Example: Override Commands
You can use Override to execute a command regardless of the current step. The previous conversation will be canceled, and the overridden command will take effect.
[Command("start/menu")] //sets the base path
public class StartMenuController : TelegramBotControllerBase
{
[Command] //matches start/menu
public async Task Start(CancellationToken ct)
{
await MessageService.SendMessageAsync(Update.Message.Chat.Id, "Hello! You said 'start' then 'menu'", ct);
}
[Command("help", Override = true)] //matches help
public async Task Start(CancellationToken ct)
{
await MessageService.SendMessageAsync(Update.Message.Chat.Id, "Hello! You said 'help'", ct);
}
}
Base Class Features
Your controllers inherit useful properties:
MessageService – For sending messages
Update – The current update object
Benefits
- Easy to understand and maintain
- Keeps your bot logic clean and modular
- Built-in conversation management
- Works out of the box with minimal setup
- Fully supports dependency injection
Need Help or Want to Contribute?
Feel free to open issues or PRs on GitHub.
License
MIT License – see the LICENSE file.
Project Links
GitHub: https://github.com/niquitos/Telegram.Bot.Controllers
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Microsoft.Extensions.Hosting (>= 9.0.6)
- Microsoft.Extensions.Logging (>= 9.0.6)
- Telegram.Bot (>= 22.6.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.