UserContextMiddleware
extends BaseMiddleware
in package
Dispatcher-side middleware that resolves the user/chat/thread context for the incoming event and writes the canonical kwargs into `$data` so that downstream handlers, filters, and inner middlewares can bind them via reflection.
Mirrors aiogram's aiogram.dispatcher.middlewares.user_context.UserContextMiddleware.
Differences from upstream:
- Upstream raises
RuntimeErrorwhen invoked with anything other than anUpdate. The port accepts either anUpdate(the dispatcher-wired case) or an already-unwrapped child event —resolveContext()dispatches on the runtime type, so callers that already hold the child can reuse the resolver without rewrapping. - Upstream conditionally writes
event_from_user/event_chat/event_thread_idonly when they're non-null. The port writes all four keys unconditionally (set tonullwhen absent) so handlers can rely onarray_key_existssemantics. Inner middlewares that compute fallbacks (e.g. an FSM context that synthesises a chat) can still overwrite the key — null is just the default.
Table of Contents
Constants
- EVENT_CHAT_KEY : string = 'event_chat'
- EVENT_CONTEXT_KEY : string = 'event_context'
- EVENT_FROM_USER_KEY : string = 'event_from_user'
- EVENT_THREAD_ID_KEY : string = 'event_thread_id'
Methods
- __invoke() : mixed
- resolveContext() : EventContext
- Resolve the `EventContext` for an arbitrary Telegram event.
- unwrapUpdate() : TelegramObject|null
- Return whichever child event slot is populated on the `Update`, or `null` if every optional field is empty. Mirrors the declaration-order priority of `UpdateShortcuts::eventType()`.
Constants
EVENT_CHAT_KEY
public
string
EVENT_CHAT_KEY
= 'event_chat'
EVENT_CONTEXT_KEY
public
string
EVENT_CONTEXT_KEY
= 'event_context'
EVENT_FROM_USER_KEY
public
string
EVENT_FROM_USER_KEY
= 'event_from_user'
EVENT_THREAD_ID_KEY
public
string
EVENT_THREAD_ID_KEY
= 'event_thread_id'
Methods
__invoke()
public
__invoke(Closure $handler, object $event, array<string|int, mixed> $data) : mixed
Parameters
- $handler : Closure
- $event : object
- $data : array<string|int, mixed>
resolveContext()
Resolve the `EventContext` for an arbitrary Telegram event.
public
static resolveContext(object $event) : EventContext
The dispatcher passes an Update here, but the resolver is also useful
for tests and for synthetic invocations that hold the already-unwrapped
child event. When given an Update, the first non-null child slot wins
(matching UpdateShortcuts::eventType()'s declaration-order tie-break).
Non-TelegramObject payloads (e.g. ErrorEvent) carry no user/chat
context themselves; the function returns an empty EventContext for
those so the keys still exist in $data with documented null defaults.
Returns an empty EventContext for events that don't carry any
recognised context (e.g. Poll, unrecognised types, or a bare Update
with every slot null).
Parameters
- $event : object
Return values
EventContextunwrapUpdate()
Return whichever child event slot is populated on the `Update`, or `null` if every optional field is empty. Mirrors the declaration-order priority of `UpdateShortcuts::eventType()`.
private
static unwrapUpdate(Update $update) : TelegramObject|null
Parameters
- $update : Update