Skip to main content
This page covers everything you need to get PolyBot running in a new or existing project: the NuGet install, project requirements, PolyBotOptions configuration, and the minimal service-registration calls.

Install the NuGet package

PolyBot is distributed as a single NuGet package. Install it with the .NET CLI:
Or add it to your .csproj directly:
PolyBot.SourceGenerators is bundled inside the PolyBot package as an analyzer. There is no separate PolyBot.SourceGenerators package to install. The source generator becomes active automatically as soon as you reference PolyBot.

Project requirements

Configure PolyBotOptions

PolyBotOptions is the single configuration class for PolyBot. It covers the bot token, API endpoint, polling behaviour, and webhook settings.

Option 1 — constructor injection (simplest)

Pass a PolyBotOptions instance directly to PolyBotClient:
Constructor values take precedence over any configuration section.

Option 2 — configuration section

PolyBotClient.Configuration is a ConfigurationManager. Load any standard Microsoft.Extensions.Configuration source and put your values under the "PolyBot" key:

Option 3 — register manually (Generic Host / webhook)

When you manage the IServiceCollection yourself (e.g. with Generic Host or ASP.NET Core), register a PolyBotOptions singleton before calling AddPolyBotDefaults():

PolyBotOptions reference

Service wiring

Minimal wiring with PolyBotClient

PolyBotClient calls AddPolyBotDefaults() internally, so the minimum setup is:

Minimal wiring with a plain IServiceCollection

When you supply your own service collection (Generic Host, ASP.NET Core, or unit tests), call both methods:
AddPolyBotDefaults() registers the following with try-add semantics (your registrations win if added first):
To use a custom state storage backend (required for multi-instance bots), register your IStateStorage before calling AddPolyBotDefaults() or AddPolyBotRouter(). The try-add semantics mean the first registration wins.

Generic Host integration

When Microsoft.Extensions.Hosting.Abstractions is referenced, the generator emits a PolyBotPollingHostedService. Register it with:

ASP.NET Core webhook integration

When any Microsoft.AspNetCore.* package is referenced, the generator emits webhook infrastructure:
AddPolyBotWebhook() registers a hosted service that calls SetWebhook on startup and DeleteWebhook on shutdown (when DeleteWebhookOnStop is true). The mapped endpoint validates the X-Telegram-Bot-Api-Secret-Token header, deserializes the update, routes it through BotRouter, and returns 200 OK.

Namespace collision warning

Do not declare a type named PolyBotExtensions in the PolyBot namespace anywhere in your project. The source generator emits a static class PolyBotExtensions in namespace PolyBot containing AddPolyBotRouter(). A hand-written class with the same name in the same namespace will cause a compile error.

Verify the installation

After adding the package and building once, confirm the generator ran correctly:
A successful build with at least one [XxxHandler]-decorated method produces no CUR0xx errors. You can also inspect the generated files under obj/ in your project directory if you want to see the emitted router code.
AddPolyBotRouter() is emitted by the source generator and will not exist until after the first successful build. New projects may show an IDE “method not found” squiggle until you run dotnet build once.