phpbotgram

UserContextMiddleware extends BaseMiddleware
in package

FinalYes

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:

  1. Upstream raises RuntimeError when invoked with anything other than an Update. The port accepts either an Update (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.
  2. Upstream conditionally writes event_from_user / event_chat / event_thread_id only when they're non-null. The port writes all four keys unconditionally (set to null when absent) so handlers can rely on array_key_exists semantics. 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

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
EventContext

unwrapUpdate()

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
Return values
TelegramObject|null
On this page

Search results