PolyBotClient is the self-contained runtime for running a PolyBot bot without IHost or ASP.NET Core. It owns a service collection, a configuration manager, and the polling loop — wire up your generated router, call RunPollingAsync, and the client takes care of everything else.
The PolyBotClient class
PolyBotClient implements IAsyncDisposable. Use it with await using so the underlying ServiceProvider is disposed when polling ends.
Users of
PolyBotClient do not need to call AddPolyBotDefaults() manually. PolyBotClient calls it internally inside BuildProvider() (the method invoked by both RunPollingAsync and RunTest).Configuring PolyBotOptions
All options live in a singlePolyBotOptions instance. PolyBotClient reads values from the "PolyBot" configuration section first, then applies any values passed via the constructor — so constructor values take precedence over appsettings.json.
BotToken is the only property required for RunPollingAsync. All others are optional and have sensible defaults.appsettings.json configuration
The configuration section key is exactly"PolyBot". Each property name is used verbatim as the key inside that section. AllowedUpdates is a comma-separated list of UpdateType names.
appsettings.json
RunPollingAsync:
Startup sequence
WhenRunPollingAsync is called, the following happens in order:
1
Build the service provider
PolyBotClient merges configuration values and constructor overrides into a PolyBotOptions, calls AddPolyBotDefaults() to register core services, registers ITelegramBotClient using the bot token, and calls Services.BuildServiceProvider().2
Sync BotFather commands
If
IBotFatherSync is registered (it is when you call AddPolyBotRouter()), SyncCommandsAsync is called. This sends one SetMyCommands request to BotFather per distinct (scope, language) group discovered from your [Command] attributes.3
Resolve BotUsername via GetMe
GetMe is called and the returned username is stored in PolyBotOptions.BotUsername. This enables @botname suffix verification in command routing without hardcoding your username.4
Start polling
ReceiveAsync runs in the foreground, passing each received Update through the generated BotRouter. The call blocks until the provided CancellationToken is cancelled.Cancellation and graceful shutdown
Pass aCancellationToken to RunPollingAsync to stop polling cleanly. The current in-flight update finishes processing before the method returns.
Minimal example
The smallest possible bot: read the token from the environment, register the generated router, poll forever.Program.cs
Full EchoBot example
This is the complete EchoBot sample from the repository. It reads the token from the first command-line argument, falls back to an environment variable, defines a/start command and a plain-message echo handler, and uses a generated inline keyboard.