Skip to main content
Telegram requires bots to specify an allowed_updates array when configuring long polling or registering webhooks. Rather than requiring you to maintain this list manually, PolyBot analyzes your declared handlers and conversational await sites at compile time to infer the minimum necessary set of UpdateType values. The source generator emits this set directly into BotRouter.AllowedUpdates and registers it in dependency injection.

How inference works

During compilation, the PolyBot source generator scans your project to determine which update types your bot can receive:
  1. Handler parameter types: Handlers decorated with attributes like [MessageHandler] or [CallbackQueryHandler] contribute their respective update type (e.g. UpdateType.Message, UpdateType.CallbackQuery).
  2. Conversational await sites: Types waited upon within conversation steps (such as await context.NextMessageAsync()) contribute their mapped update types to ensure multi-turn dialogs receive expected responses.
  3. Filtering: The UpdateType.Unknown member is ignored and automatically excluded from the inferred set.
PolyBot logs diagnostic CUR039 at compile time detailing the exact update types and total count inferred for your project.

Raw Update handlers

If any handler accepts a raw Telegram.Bot.Types.Update parameter instead of a strongly-typed payload, the generator cannot predict which payload types the handler inspects. When this occurs, PolyBot issues warning CUR037 and automatically falls back to requesting all known UpdateType members from Telegram.

The [AllowedUpdates] attribute

To override or augment the compile-time inference without changing handler signatures, declare [assembly: AllowedUpdates] in any source file (such as AssemblyInfo.cs).

Properties at a glance

Excluding update types

When an update type is specified in Exclude, the source generator removes it from the final array. If any handler exists in your project that handles the excluded type, the compiler issues warning CUR038 to alert you that the handler will never receive updates.

Requesting all updates with IncludeAll

Setting IncludeAll = true instructs the generator to emit an empty UpdateType[] array. In the Telegram Bot API, an empty allowed updates array signals that the bot requests all update types except ChatMember.

Runtime and hosting integration

The source generator generates BotRouter.AllowedUpdates as a static field and implements IAllowedUpdatesProvider on the generated BotRouter:
When registering services via services.AddPolyBotRouter() or services.AddPolyBotDefaults(), IAllowedUpdatesProvider is registered as a singleton resolving to BotRouter.

Long polling

When using hosted polling (AddPolyBotHostedPolling()) or PolyBotClient.RunPollingAsync(), ReceiverOptions.AllowedUpdates automatically defaults to BotRouter.AllowedUpdates unless you explicitly set your own:

Webhooks

When configuring hosted webhooks via AddPolyBotWebhook(), PolyBotWebhookHostedService sets SetWebhookRequest.AllowedUpdates to _options.AllowedUpdates ?? BotRouter.AllowedUpdates:

Automated tests

When testing handlers using PolyBotClient.RunTest(), UpdateMocker automatically sets PolyBotOptions.AllowedUpdates from IAllowedUpdatesProvider.AllowedUpdates:

Diagnostics reference

Severity: WarningA handler method accepts a parameter of type Telegram.Bot.Types.Update. Because the specific update types cannot be determined statically, PolyBot falls back to requesting all known update types.
Severity: WarningA handler method expects an update type that has been excluded using [assembly: AllowedUpdates(Exclude = [...])]. The handler will not receive updates from Telegram.
Severity: InfoInformational diagnostic emitted during compilation reporting the count and names of update types inferred from declared handlers and conversation await sites.