Keyboards, Files & Formatting
Telegram messages can carry buttons, media, and rich text. PAM Telegram gives you fluent builders for both keyboard types, an InputFile class for uploads, and escaping helpers for formatted text.
Inline keyboards
Section titled “Inline keyboards”Inline keyboards are buttons attached to a message. Build one with InlineKeyboard and pass it as reply_markup:
use Pam\Telegram\Keyboard\InlineKeyboard;
$keyboard = InlineKeyboard::make() ->text('Yes', 'vote:yes') ->text('No', 'vote:no') ->row() ->url('Read the docs', 'https://example.com/docs');
$ctx->reply('Do you like the new design?', ['reply_markup' => $keyboard]);Buttons are added to the current row; row() starts a new one.
| Method | Button |
|---|---|
text($label, $data) |
Sends $data back as a callback query. Data must be 1 to 64 bytes. |
url($label, $url) |
Opens a URL. |
webApp($label, $url) |
Opens a Telegram Mini App. |
copyText($label, $text) |
Copies text to the clipboard. |
switchInline($label, $query = '') |
Starts an inline query in another chat. |
pay($label) |
Pay button; must be the first button of a sendInvoice keyboard. |
button(array $button) |
Any raw InlineKeyboardButton array. |
Handle presses with callbackQuery(). To change or remove the buttons later, call $ctx->editMarkup($newKeyboard) or $ctx->editMarkup(null).
Reply keyboards
Section titled “Reply keyboards”Reply keyboards replace the user’s keyboard with buttons that send their label as a message:
use Pam\Telegram\Keyboard\Keyboard;
$ctx->reply('How can we help?', ['reply_markup' => Keyboard::make() ->text('Opening hours') ->text('Talk to a human') ->row() ->requestLocation('Find the nearest store') ->resized() ->oneTime() ->placeholder('Choose an option'),]);
$bot->hears('Opening hours', fn (Context $ctx) => $ctx->reply('9:00 to 18:00.', [ 'reply_markup' => Keyboard::remove(),]));| Method | Description |
|---|---|
text($label) |
A button that sends its label. |
requestContact($label) |
Asks the user to share their phone number. |
requestLocation($label) |
Asks the user to share their location. |
webApp($label, $url) |
Opens a Mini App. |
button(array $button) |
Any raw KeyboardButton array. |
row() |
Starts a new row. |
resized(), oneTime(), persistent() |
Set resize_keyboard, one_time_keyboard, and is_persistent. |
placeholder($text) |
Placeholder shown in the input field. |
Keyboard::remove() |
Markup that removes the reply keyboard. |
Keyboard::forceReply($placeholder = null) |
Markup that opens a reply to the bot’s message. |
Sending files
Section titled “Sending files”Files that Telegram already has are sent by file_id or URL, as plain strings. Use InputFile only for new uploads:
use Pam\Telegram\InputFile;
$ctx->replyWithPhoto(InputFile::fromPath(__DIR__.'/storage/chart.png'), ['caption' => 'Weekly sales']);$ctx->replyWithDocument(InputFile::fromContents($csv, 'report.csv'));
$ctx->replyWithPhoto('https://example.com/banner.jpg'); // Telegram downloads the URL$ctx->replyWithDocument($fileId); // re-send a file by its file_idInputFile::fromPath() reads the file lazily, when the request is sent. Any Bot API method accepts InputFile values, including nested ones: they are uploaded as multipart/form-data and referenced as attach:// automatically.
$bot->api->sendMediaGroup(chat_id: $chatId, media: [ ['type' => 'photo', 'media' => InputFile::fromPath('before.jpg'), 'caption' => 'Before'], ['type' => 'photo', 'media' => InputFile::fromPath('after.jpg')],]);Downloading files
Section titled “Downloading files”Received media carries a file_id. Api::download() resolves the file path with getFile and returns the bytes:
$bot->on('message:document', function (Context $ctx): void { $document = $ctx->message()['document']; $bytes = $ctx->api->download($document['file_id']);
file_put_contents(__DIR__.'/storage/inbox/'.basename($document['file_name'] ?? 'file'), $bytes); $ctx->reply('Saved!');});The public Bot API serves files up to 20 MB, which is also the default $maxBytes limit of download(). A self-hosted Bot API server can serve larger files.
Formatting messages
Section titled “Formatting messages”Use Telegram’s parse_mode for bold, links, and code. Escape any text that comes from users or external data before you put it in a formatted message:
use Pam\Telegram\Format;
$name = Format::html($ctx->from()['first_name'] ?? 'there');$ctx->reply("<b>Hello, {$name}!</b> Your order is <i>on its way</i>.", ['parse_mode' => 'HTML']);
$safe = Format::markdownV2('Price: $9.99 (incl. tax)');$ctx->reply("*Today only* {$safe}", ['parse_mode' => 'MarkdownV2']);Format::html() escapes &, <, >, and ". Format::markdownV2() escapes every character that MarkdownV2 reserves.