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:.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 aPolyBotOptions instance directly to PolyBotClient:
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 theIServiceCollection 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):
Generic Host integration
WhenMicrosoft.Extensions.Hosting.Abstractions is referenced, the generator emits a PolyBotPollingHostedService. Register it with:
ASP.NET Core webhook integration
When anyMicrosoft.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
Verify the installation
After adding the package and building once, confirm the generator ran correctly:[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.