BotLib 2.1.1-pre
dotnet add package BotLib --version 2.1.1-pre
NuGet\Install-Package BotLib -Version 2.1.1-pre
<PackageReference Include="BotLib" Version="2.1.1-pre" />
<PackageVersion Include="BotLib" Version="2.1.1-pre" />
<PackageReference Include="BotLib" />
paket add BotLib --version 2.1.1-pre
#r "nuget: BotLib, 2.1.1-pre"
#:package BotLib@2.1.1-pre
#addin nuget:?package=BotLib&version=2.1.1-pre&prerelease
#tool nuget:?package=BotLib&version=2.1.1-pre&prerelease
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
BotStateprocesses 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
TelegramCommandobjects; - creates and destroys per-user
BotMachineinstances; - 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()andPostNonPriorityMessage(); - switches to another state with
ActivateState().
Main file:
Command Model
Incoming Telegram updates are converted to strongly-typed command objects, for example:
TelegramTextCommandTelegramButtonPressedCommandTelegramPhotoCommandTelegramFileCommandTelegramGeoCommand
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:
TelegramTextMessageTelegramTextMessageWithKeyboardTelegramTextMessageWithKeyboardEditedTelegramPictureMessageTelegramFileMessageTelegramPaymentMessageTelegramTypingMessage
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
- Put your bot token into
apikey.txtin theTestConsoleAppworking directory. - Build the solution:
dotnet build BotLib.slnx
- 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 xxxhandling; - file-too-large event hook.
Repository Layout
BotLib/
BotLib/
Engine/
AdminTasks/
Commands/
Exceptions/
Messages/
Fsm/
HelperStates/
FsmEventArgs/
TestConsoleApp/
Notes
BotLibcurrently derives directly fromTelegramBotClient.- The library contains a compatibility layer for newer
Telegram.BotAPIs in BotLib/Engine/TelegramBotClientCompatExtensions.cs. - Package generation is enabled in BotLib/BotLib.csproj, so build also produces NuGet packages.
License
MIT
| Product | Versions 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. |
-
net10.0
- Telegram.Bot (>= 22.10.1)
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 | |
| 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 |