PolyBot 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package PolyBot --version 1.1.0
                    
NuGet\Install-Package PolyBot -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="PolyBot" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PolyBot" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="PolyBot" />
                    
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 PolyBot --version 1.1.0
                    
#r "nuget: PolyBot, 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 PolyBot@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=PolyBot&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=PolyBot&version=1.1.0
                    
Install as a Cake Tool

PolyBot - The most magicky Telegram.Bot framework

PolyBot is a generative, DI-friendly routing framework for Telegram.Bot. Instead of hand-written switch statements over Update.Type, you write minimal-API-style handler methods decorated with attributes; a Roslyn source generator compiles them into a hard-coded, optimized BotRouter implementing Telegram.Bot.Polling.IUpdateHandler.


Learn and Docs

Learn Telegrator on Official documentation site.


Quick start

[MessageHandler, Command(Aliases = ["start"])]
public static async ValueTask<Result> StartHandler(Message msg, ITelegramBotClient client, CancellationToken ct)
{
    client.SendMessage(msg.Chat, "Hello, " + msg.From!.UserName + "!", cancellationToken: ct);
    return Result.StopRouting;
}

public static async void Main()
{
    await using PolyBotClient client = new PolyBotClient();
    client.Services.AddPolyBotRouter();
    await client.RunPollingAsync();
}

Features

Attribute routing

One handler attribute per UpdateType: [MessageHandler], [CallbackQueryHandler], [InlineQueryHandler], etc. Handlers return Task<Result> / ValueTask<Result>; Result.ContinueRouting falls through, Result.StopRouting ends routing. Priority or source order arranges handlers within an update type.

Dependency injection

Parameters resolve automatically: ITelegramBotClient, CancellationToken, Update, the update payload (Message, CallbackQuery, ...), [Key("x")] T keyed services, and anything else via GetRequiredService<T>().

Commands

[Command(Aliases = ["start"])] matches messages whose first entity is a / command. The @botname suffix is verified against the current bot's username. Optional metadata (Description, LanguageCode, Scope, IsHidden) is synchronized with BotFather automatically.

Filters

[XxxFilter] attributes gate handlers with custom IUpdateFilter implementations. All filters must pass in attribute order. Generated bases such as MessageFilter let filters work directly with the extracted payload.

Keyboards

InlineKeyboardMarkup keyboard = new InlineKeyboardBuilder()
    .WithCallbackButton("Open", "open")
    .WithCallbackButton("Close", "close")
    .Adjust(2);

Or generate keyboards from [CallbackButton] attributes on a partial method returning InlineKeyboardMarkup.

[CallbackButton("First", "first:{val}"), CallbackButton("Second", "second:{val}")]
[CallbackButton("Cancel", "cancel"), CallbackButton("Apply", "apply:{val}")]
private static partial InlineKeyboardMarkup ActionsKeyboard(int val);

Finite state machines

[MessageHandler, State<RegistrationStep>(RegistrationStep.AwaitingAge)]
public static async Task<Result> AskAge(IStateMachine fsm)
{
    await fsm.SetAsync(RegistrationStep.Completed);
    return Result.Handled();
}

Inject IStateMachine to transition state; register your own IStateStorage before AddPolyBotRouter() for custom persistence.

Inline awaits

Suspend a handler and resume on a later update without blocking the polling loop:

[MessageHandler, Command(Aliases = ["register"])]
public static async Task<Result> Register(IUpdateAwaiter awaiter, CancellationToken ct)
{
    Message? age = await awaiter.WaitForMessageAsync()
        .TextMatches(@"my age is \d+")
        .ByUserId(ct);
        
    return age is null ? Result.Handled() : Result.ContinueRouting;
}

Rate limiting

[MessageHandler, Command(Aliases = ["search"])]
[Throttled(Limit = 3, PeriodMilliseconds = 60_000, Scope = ThrottleScope.User)]
public static Task<Result> Search(Message msg, [Rest] string query) { ... }

Callback-data patterns

[CallbackQueryHandler, Pattern("item:{id}:{action}")]
public static Task<Result> OnItem(CallbackQuery query, [Arg] int id, [Arg] string action)
{
    return Result.Handled();
}

Exception handling

[ExceptionHandler]
public static Task OnError(Exception exception, HandleErrorSource source, ILogger log)
{
    log.LogError("{Source}: {Message}", source, exception.Message);
    return Task.CompletedTask;
}

Hosting and webhooks

Integration is emitted only when the matching package is referenced:

  • Generic host — services.AddPolyBotHostedPolling(...).
  • ASP.NET Core — services.AddPolyBotWebhook() and app.MapPolyBotWebhook("/bot").

Testing

Test without network or tokens:

PolyBotClient client = new PolyBotClient();
client.Services.AddPolyBotRouter();
UpdateMocker mock = client.RunTest();

await mock.Message("hello");
await mock.Callback("item:1:open");

PolyTests is also available as an in-memory ITelegramBotClient for driving the host or polling service manually.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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 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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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.6.5 99 10/16/2026
1.6.3 96 10/6/2026
1.6.1 108 10/4/2026
1.6.0 94 10/3/2026
1.5.1 99 9/30/2026
1.5.0 101 9/30/2026
1.4.0 103 9/26/2026
1.3.0 103 9/23/2026
1.2.0 106 9/21/2026
1.1.0 103 9/20/2026
1.0.0 107 9/19/2026