CallbackAnswerMiddleware
extends BaseMiddleware
in package
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): theanswerCallbackQueryAPI call is made after the handler returns, in afinallyblock. 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
FLAG_NAME
public
string
FLAG_NAME
= 'callback_answer'
Properties
$cacheTime read-only
private
int|null
$cacheTime
= null
$pre read-only
private
bool
$pre
= false
$showAlert read-only
private
bool|null
$showAlert
= null
$text read-only
private
string|null
$text
= null
$url read-only
private
string|null
$url
= null
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
CallbackAnswerresolveFlag()
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|mixedsendAnswer()
Calls `answerCallbackQuery` on the event and marks the DTO as answered.
private
sendAnswer(CallbackQuery $event, CallbackAnswer $callbackAnswer) : void
Parameters
- $event : CallbackQuery
- $callbackAnswer : CallbackAnswer