phpbotgram

CallbackAnswerMiddleware extends BaseMiddleware
in package

FinalYes

Dispatcher-side middleware that automatically answers callback queries.

Mirrors the CallbackAnswerMiddleware class from upstream aiogram/utils/callback_answer.py.

For every incoming CallbackQuery event the middleware builds a CallbackAnswer DTO from its own constructor defaults, optionally overridden by per-handler flags (flags: ['callback_answer' => [...]]), and injects it into $data['callback_answer'] so handlers can further customise the response.

Two modes are available:

  • Post mode (default, pre = false): the answerCallbackQuery API call is made after the handler returns, in a finally block. This ensures the query is always answered even if the handler throws.
  • Pre mode (pre = true): the API call is made before the handler runs, giving the user instant feedback on slow handlers.

Setting $callbackAnswer->disabled = true (or calling ->disable()) inside the handler suppresses the automatic answer entirely — the handler then owns the responsibility of calling answerCallbackQuery manually.

Handler-flag schema:

$router->callbackQuery->register($handler, flags: [
    'callback_answer' => [
        'pre'        => true,
        'disabled'   => false,
        'text'       => 'Processing…',
        'show_alert' => false,
        'url'        => null,
        'cache_time' => 0,
    ],
]);

Any key may be omitted; absent keys fall back to the middleware defaults.

Non-CallbackQuery events are passed through unchanged.

Table of Contents

Constants

FLAG_NAME  : string = 'callback_answer'

Properties

$cacheTime  : int|null
$pre  : bool
$showAlert  : bool|null
$text  : string|null
$url  : string|null

Methods

__construct()  : mixed
__invoke()  : mixed
constructCallbackAnswer()  : CallbackAnswer
Build a {@see CallbackAnswer} by merging middleware defaults with any per-handler overrides supplied via the `callback_answer` flag.
resolveFlag()  : null|mixed
Returns the raw `callback_answer` flag value from the handler object, or `null` when the flag is absent / the handler object is not present.
sendAnswer()  : void
Calls `answerCallbackQuery` on the event and marks the DTO as answered.

Constants

Properties

Methods

__construct()

public __construct([bool $pre = false ][, string|null $text = null ][, bool|null $showAlert = null ][, string|null $url = null ][, int|null $cacheTime = null ]) : mixed
Parameters
$pre : bool = false
$text : string|null = null
$showAlert : bool|null = null
$url : string|null = null
$cacheTime : int|null = null

__invoke()

public __invoke(callable(object, array<string, mixed>): mixed $handler, object $event, array<string, mixed> $data) : mixed
Parameters
$handler : callable(object, array<string, mixed>): mixed
$event : object
$data : array<string, mixed>

constructCallbackAnswer()

Build a {@see CallbackAnswer} by merging middleware defaults with any per-handler overrides supplied via the `callback_answer` flag.

private constructCallbackAnswer(mixed $properties) : CallbackAnswer
Parameters
$properties : mixed

Raw flag value (expected to be an associative array when set, otherwise ignored).

Return values
CallbackAnswer

resolveFlag()

Returns the raw `callback_answer` flag value from the handler object, or `null` when the flag is absent / the handler object is not present.

private resolveFlag(array<string, mixed> $data) : null|mixed
Parameters
$data : array<string, mixed>
Return values
null|mixed
On this page

Search results