BotLib 2.1.1-pre

This is a prerelease version of BotLib.
dotnet add package BotLib --version 2.1.1-pre
                    
NuGet\Install-Package BotLib -Version 2.1.1-pre
                    
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="BotLib" Version="2.1.1-pre" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BotLib" Version="2.1.1-pre" />
                    
Directory.Packages.props
<PackageReference Include="BotLib" />
                    
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 BotLib --version 2.1.1-pre
                    
#r "nuget: BotLib, 2.1.1-pre"
                    
#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 BotLib@2.1.1-pre
                    
#: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=BotLib&version=2.1.1-pre&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=BotLib&version=2.1.1-pre&prerelease
                    
Install as a Cake Tool

BotLib

BotLib is a .NET library for building Telegram bots around a finite-state-machine flow.

The library wraps Telegram.Bot and adds three main concepts:

  • TelegramFsmBot: the bot runtime, update dispatcher, and Telegram client.
  • BotMachine: per-user session state holder.
  • BotState: a single dialog state that receives commands and produces outgoing messages.

The repository also contains TestConsoleApp, a minimal runnable example of a bot built on top of the library.

Target Framework

  • .NET 10

What the Component Does

BotLib is intended for bots where user interaction is modeled as a sequence of states:

  • a user sends a Telegram update;
  • the update is converted into a command such as text, callback button press, file, photo, or geo;
  • the command is routed to that user's BotMachine;
  • the active BotState processes the command;
  • the state emits one or more outgoing messages and may switch to another state.

This is useful for:

  • guided chat flows;
  • menu-based bots;
  • multi-step forms;
  • bots that need separate dialog state per user.

Architecture

TelegramFsmBot

Base class for the bot itself.

Responsibilities:

  • starts Telegram polling;
  • receives Telegram updates;
  • converts updates into TelegramCommand objects;
  • creates and destroys per-user BotMachine instances;
  • queues outgoing messages through TelegramMessageSender;
  • exposes integration events such as payments, parametric /start, and large-file handling.

Main file:

BotMachine

Represents one user session.

Responsibilities:

  • owns the current ActiveState;
  • forwards commands to the active state;
  • switches states when the current state emits StateIsChanged;
  • keeps the last sent Telegram message id;
  • passes generated messages to the sender queue.

Main file:

BotState

Base class for a single state in the dialog.

Responsibilities:

  • initializes UI/message output in Init();
  • handles incoming commands in ProcessCommand();
  • emits messages with PostMessage() and PostNonPriorityMessage();
  • switches to another state with ActivateState().

Main file:

Command Model

Incoming Telegram updates are converted to strongly-typed command objects, for example:

  • TelegramTextCommand
  • TelegramButtonPressedCommand
  • TelegramPhotoCommand
  • TelegramFileCommand
  • TelegramGeoCommand

Main files:

Message Model

States do not call Telegram API directly. Instead, they create message objects that are sent by the internal sender queue.

Examples:

  • TelegramTextMessage
  • TelegramTextMessageWithKeyboard
  • TelegramTextMessageWithKeyboardEdited
  • TelegramPictureMessage
  • TelegramFileMessage
  • TelegramPaymentMessage
  • TelegramTypingMessage

Main files:

Minimal Usage

1. Create a bot class

Inherit from TelegramFsmBot, pass the machine type and initial state types into the base constructor, and start receiving updates.

using BotLib.Engine;
using BotLib.Engine.Commands;
using System.Net.Http;

internal class DemoBot : TelegramFsmBot
{
    public DemoBot(string token, HttpClient? httpClient = null)
        : base(
            token,
            typeof(DemoFirstState),
            typeof(DemoFirstState),
            typeof(DemoMachine),
            httpClient)
    {
        StartReceiving();
    }

    public override bool PerformPreCheckoutQuery(string invoiceId, ref string oopsAnswer)
    {
        return true;
    }

    protected override bool IsException(TelegramCommand command)
    {
        return false;
    }
}

Real example:

2. Create a machine class

Usually this class is small and just passes dependencies into the base constructor.

using BotLib.Engine;
using BotLib.Fsm;
using System;

internal class DemoMachine : BotMachine
{
    public DemoMachine(long userId, TelegramMessageSender sender, Type initStateType, TelegramFsmBot bot)
        : base(userId, sender, initStateType, bot)
    {
    }
}

Real example:

3. Create a state

State initialization sends the first message. Command handling updates the flow.

using BotLib.Engine.Commands;
using BotLib.Engine.Messages;
using BotLib.Fsm;

internal class DemoFirstState : BotState
{
    private const string IncrementCallback = "increment";
    private TelegramTextMessageWithKeyboard activeMessage;
    private int counter = 1;

    public DemoFirstState(long userId, BotMachine machine) : base(userId, machine)
    {
    }

    public override void Init()
    {
        activeMessage = new TelegramTextMessageWithKeyboard(UserId, $"Counter: {counter}");
        activeMessage.AddCallbackButton("Increment", IncrementCallback);
        PostMessage(activeMessage);
    }

    public override void ProcessCommand(TelegramCommand command)
    {
        if (command is TelegramButtonPressedCommand button &&
            button.CallbackData == IncrementCallback)
        {
            counter++;
            PostMessage(new TelegramTextMessage(UserId, $"Counter: {counter}"));
        }
    }
}

Real example:

Running the Example

  1. Put your bot token into apikey.txt in the TestConsoleApp working directory.
  2. Build the solution:
dotnet build BotLib.slnx
  1. Run the sample app:
dotnet run --project TestConsoleApp/TestConsoleApp.csproj

Entry point:

Included Features

  • per-user machine lifecycle;
  • typed commands for text, callbacks, files, photos, and geo;
  • queued message sending;
  • inline keyboard helpers;
  • message editing helpers;
  • payment pre-checkout handling;
  • admin-task queue support;
  • parametric /start xxx handling;
  • file-too-large event hook.

Repository Layout

BotLib/
  BotLib/
    Engine/
      AdminTasks/
      Commands/
      Exceptions/
      Messages/
    Fsm/
      HelperStates/
      FsmEventArgs/
  TestConsoleApp/

Notes

License

MIT

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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
2.1.1-pre 42 7/9/2026
2.0.2 48 7/7/2026
2.0.1 60 7/7/2026
2.0.0 82 7/7/2026 2.0.0 is deprecated because it has critical bugs.
1.2.6 782 7/31/2023
1.2.5 755 4/15/2023
1.2.4 840 12/12/2022
1.2.3 1,067 8/16/2022
1.2.3-changing-startup-type... 126 8/16/2022
1.2.3-alpha.4 259 8/16/2022
1.2.2 895 8/16/2022
1.2.2-telegram-bot-updates.1 140 7/23/2022
1.2.1 1,018 7/23/2022
1.2.1-symbols-problem.1 128 7/23/2022
1.2.0 1,010 7/23/2022
1.1.3-PullRequest0003.25 242 7/23/2022
1.1.3-pipelines.1 160 7/22/2022
1.1.3-cont-versioning.1 168 7/22/2022
1.1.0.4 1,059 5/4/2022
1.1.0.3 949 5/4/2022
Loading failed