October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

Manage Telegram Callback Queries and Inline Keyboards in PHP

A practical PHP guide to Telegram inline keyboards: receive callback queries through polling or webhooks, answer button presses, and edit messages safely.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • editMessageReplyMarkup changes only the keyboard.
  • editMessageText changes the message text and can include replacement reply_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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Keep callback data compact and safe

  • Keep callback_data within 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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.