Skip to main content
PolyBot.Routing.UpdateExtensions is a static class of extension methods on Telegram.Bot.Types.Update. A Telegram Update carries one of many optional payload properties (Message, CallbackQuery, PollAnswer, etc.), and different payload types nest their user and chat information in different places. These helpers walk a fixed probe order and return the first match found, so you never need to write property-chain ?? chains by hand.
These are the exact same probe chains that StateKeyResolver uses internally to resolve FSM keys. The probe order is fixed and matches Telegram.Bot 22.10’s Update property declaration order — first match wins.

Methods

GetUserId()

Returns the id of the first user found across all update payload types, or null when none is present.
long?
The user id, or null.

GetChatId()

Returns the id of the first chat found across all update payload types, or null when none is present.
BusinessConnection exposes only UserChatId (a long), not a full Chat object. It contributes to GetChatId() but not to GetChat().
long?
The chat id, or null.

GetSender()

Returns the first User object found across all payload types, or null.
User?
The sender User, or null.

GetChat()

Returns the first Chat object found across all payload types, or null. Unlike GetChatId(), this returns null for BusinessConnection updates because only a UserChatId long is available there.
Chat?
The Chat, or null.

GetMessage()

Returns the first message-shaped payload in the update using a simple four-property chain:
Message?
The Message, or null when none of the four properties is set.

GetText()

Returns the text of the message returned by GetMessage(), falling back to its Caption (media captions are typed like message text in Telegram). Returns null when there is no message-shaped payload or neither Text nor Caption is set.
string?
The message text or caption, or null.

Probe order

The following table shows the full probe order for each method. Within each group, properties are checked top-to-bottom and the first non-null result is returned.

User id / Sender probe order

Chat id / Chat probe order


Usage in handlers

Because parameter injection resolves ITelegramBotClient, CancellationToken, Update, and the specific payload automatically, you will usually receive the typed payload directly. UpdateExtensions is most useful in:
  • Filters — where you receive the raw Update and need a cross-type probe:
  • Exception handlers — there is no typed payload in the error path, but the Update is not available either (see CUR032). Use GetUserId()/GetChatId() on updates you access through other means.
  • State key resolution — IStateMachine.SetAsync calls StateKeyResolver which uses the same probe chains internally. Understanding the probe order tells you exactly which update types are keyed by StateKey.UserId vs. StateKey.ChatId vs. StateKey.UserInChat.