Build an inline keyboard as rows of buttons, receive each press in a callback_query update, call answerCallbackQuery promptly, then edit the message text or keyboard as needed. Telegram limits each button’s callback_data to 1–64 bytes, so use it for a compact action or state reference—not large or sensitive data.
How inline keyboards and callback queries work
An inline keyboard is an InlineKeyboardMarkup object whose inline_keyboard value is an array of rows; each row contains button objects. A callback button has visible text and a callback_data value. When someone presses it, Telegram sends an Update containing a callback_query with a query ID, the user, the callback data, and information about the message that carried the button.
The callback query may contain a message when the button belongs to an accessible bot-sent chat message, or an inline_message_id when it belongs to a message sent through inline mode. The originating message may no longer have a button with the received data. Treat callback data as a request to consider an action, not proof that the current keyboard still permits it; validate the action and relevant application state on your server. See Telegram’s Bot API reference.
Choose how PHP receives updates
| Delivery method | How it fits | Important constraint |
|---|---|---|
getUpdates long polling |
Use a polling worker that repeatedly requests updates from Telegram. | Advance the offset beyond the highest processed update_id to avoid receiving the same updates again. It cannot be used while an outgoing webhook is configured. |
HTTPS webhook with setWebhook |
Use a PHP endpoint reachable by Telegram over HTTPS; Telegram sends it a JSON-serialized Update. | Polling and a configured outgoing webhook are mutually exclusive. Telegram supports an optional secret_token header to help verify webhook requests. |
Both delivery methods provide Update objects. If you explicitly restrict update types with allowed_updates, include callback_query or your bot will not receive those button presses. The setup and request details are in Telegram’s Bot API documentation.
Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →#1 Best Overall
Handle a callback in a PHP webhook
Telegram’s Bot API accepts HTTPS GET and POST requests, including JSON request parameters, and returns JSON with an ok status and a result on success. The following is a PHP-oriented outline, not tested executable code:
<?php
$update = json_decode(file_get_contents('php://input'), true);
if (isset($update['callback_query'])) {
$query = $update['callback_query'];
$queryId = $query['id'];
$data = $query['data'] ?? '';
// Validate the action and any related state in your application.
telegramApi('answerCallbackQuery', [
'callback_query_id' => $queryId,
]);
// Then edit the message, using the available message context.
}
?>
telegramApi() is a placeholder for your own HTTP-client function, not a Telegram-provided PHP function. It must send parameters to an endpoint of the form https://api.telegram.org/bot<TOKEN>/METHOD_NAME, handle HTTP and JSON errors, and keep the bot token out of public source and logs. Telegram specifies the HTTP API, not a required PHP SDK, framework, router, or client; no PHP library is assumed here.
Rank #2
Acknowledge every button press
Call answerCallbackQuery with the callback query’s id for every callback, even when you have no message to show. Until that call is made, Telegram clients display a progress indicator. The optional text parameter can show a notification or alert; omit it for a quiet acknowledgement. If further work may take time, acknowledge first when the workflow allows it, then do the slower work. Telegram’s guidance for bot buttons likewise advises answering promptly and calling the answer method even when no message or URL is returned: Bot buttons.
Update the message or its keyboard
After validating and handling the action, choose the edit method that matches the result:
editMessageReplyMarkupchanges only the keyboard.editMessageTextchanges the message text and can include replacementreply_markup.
For a regular bot message, provide its chat and message identifiers. For an inline-mode message, the edit methods accept inline_message_id instead. Editing the existing message can keep the chat from filling with a new bot message for every button press.
Keep callback data compact and safe
- Keep
callback_datawithin Telegram’s 1–64 byte limit; the limit is in bytes, not necessarily characters. - Use short action identifiers or compact references to server-side state rather than embedding large or sensitive state.
- Validate callback data and check that the requested action remains valid for the current application state.
- Do not assume the originating message still displays the button or action represented by the received data.
Telegram’s current Bot API reference documents the field limits and callback-query behavior: Telegram Bot API.
Quick Recap
Rank #4
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




