Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk3 min

Build Interactive Telegram Inline Keyboards in PHP

Build Telegram inline keyboards with nested PHP arrays, route callback data safely, answer button presses, and update the message in place.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To add interactive buttons to a Telegram bot message in PHP, send the message with a reply_markup object containing an inline_keyboard array. Each nested array is one row of buttons. Use callback_data when the bot should handle a button press, then process the resulting callback query, answer it, and optionally edit the message.

How Telegram inline keyboards are structured

Telegram defines an inline keyboard as an InlineKeyboardMarkup object. Its inline_keyboard property is an array of button rows, and each row is an array of InlineKeyboardButton objects. In PHP, nested associative arrays map naturally to this structure. See Telegram’s inline keyboard structure and Bot API reference.

Each button needs visible text and one action field. A callback_data button sends data to your bot when tapped; a url button opens a link in the user’s client. Telegram documents other action types, including Mini App buttons, with availability restrictions; consult the current Bot API before using them.

Do not confuse inline keyboards with reply keyboards. Inline buttons are attached to a message and can invoke a bot callback without posting an ordinary text reply. Reply keyboards instead offer suggested replies in the chat’s input interface. Telegram explains the distinction in its keyboards documentation.

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

Build and send a message with buttons

The example below creates two rows: one bot-handled action and one link action. Replace the example URL and connect the JSON request to your existing Bot API HTTP helper.

<?php

$keyboard = [
    'inline_keyboard' => [
        [
            ['text' => 'Show details', 'callback_data' => 'details'],
            ['text' => 'Open guide', 'url' => 'https://example.com/guide'],
        ],
        [
            ['text' => 'Next', 'callback_data' => 'next'],
        ],
    ],
];

$params = [
    'chat_id' => $chatId,
    'text' => 'Choose an action:',
    'reply_markup' => $keyboard,
];

$json = json_encode($params, JSON_THROW_ON_ERROR);
// POST $json to the Telegram Bot API sendMessage method.

Telegram accepts Bot API method parameters as JSON as well as other supported request formats. Its request documentation describes the supported formats, and the official PHP Hellobot sample demonstrates JSON encoding in a webhook-oriented PHP implementation. The sample is an implementation example, not a requirement to adopt its architecture.

Keep callback payloads short and meaningful

Telegram limits callback_data to 1–64 bytes. Count encoded bytes rather than assuming a character count, especially if payloads contain non-ASCII text. Use compact identifiers such as details or next, not arbitrary user input, credentials, or sensitive state. The Bot API specifies the limit in its InlineKeyboardButton definition.

Maintain a stable mapping from these identifiers to application actions on the server. The callback value tells your code which route to consider; it does not establish that the sender is allowed to perform that action.

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

Process callback queries safely

When a user taps a callback button, Telegram sends a callback query in an update. Your update handler should distinguish callback queries from ordinary messages, check that expected fields exist, and route the callback’s data through application logic. Telegram documents callback queries and the answer and edit methods in the Bot API.

  1. Parse and validate the update. Decode the incoming JSON with error handling. Check whether the update contains a callback_query or a message; validate the fields your handler needs before using them.
  2. Authorize the requested action. Resolve the callback identifier through your server-side mapping, then check the user and current application state. Reject unknown identifiers and actions the user is not permitted to take.
  3. Answer the callback query. Call answerCallbackQuery using the callback query’s ID. This lets the Telegram client stop displaying its progress indicator; provide a short notification when useful.
  4. Update the interaction. If the action changes a menu or its state, call an edit method such as editMessageText or editMessageReplyMarkup for the existing message. Use a new message instead when a separate conversational step is clearer.

Telegram notes that editing messages is useful for messages with inline keyboards. Editing keeps navigation in place rather than adding a new chat message for each menu state. See the Bot API methods for updating messages.

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

Protect a PHP webhook

If Telegram delivers updates to a PHP webhook, treat the endpoint as an input boundary: reject malformed payloads, validate the request against your configured webhook protection, and keep the bot token out of public code and logs. Telegram’s Bot FAQ recommends using a secret path in the webhook URL to help verify that requests came from Telegram. Read the webhook verification guidance and choose a secret, unguessable path for your deployment.

The official PHP sample demonstrates webhook-oriented request handling, but a webhook is not the only possible architecture. Whatever transport you use, ensure that only valid updates reach the handler and that authorization decisions are made by your application.

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

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 *

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
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

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.