Telegram.Bot.Controllers 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Telegram.Bot.Controllers --version 1.1.0
                    
NuGet\Install-Package Telegram.Bot.Controllers -Version 1.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Telegram.Bot.Controllers" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Telegram.Bot.Controllers" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Telegram.Bot.Controllers" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Telegram.Bot.Controllers --version 1.1.0
                    
#r "nuget: Telegram.Bot.Controllers, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Telegram.Bot.Controllers@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Telegram.Bot.Controllers&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Telegram.Bot.Controllers&version=1.1.0
                    
Install as a Cake Tool

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

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.

GitHub: https://github.com/niquitos/Telegram.Bot.Controllers

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.5 225 7/14/2025
1.1.0 198 7/14/2025
1.0.0 203 7/6/2025