PAM Telegram
pushinbr/pam-telegram is a framework for bots on the official Telegram Bot API. You register handlers for commands, text, button presses, and any update type; the framework routes each update through a middleware chain and gives your handler a Context with shortcuts to reply, edit, and answer.
On PAM, the bot is a persistent process: sessions and conversations live in memory, the HTTPS connection to Telegram is reused, and an idle bot spends its time waiting in a long poll. The package has no Composer dependencies and also runs on PHP-FPM, Laravel, Symfony, or a plain PHP script.
The API design is inspired by grammY.
Installation
Section titled “Installation”The package requires PHP 8.3 or newer. With the PAM runtime installed (see Installation):
mkdir acme-bot && cd acme-botpam composer init --no-interactionpam composer require pushinbr/pam-telegram:^0.1Getting a token
Section titled “Getting a token”Open @BotFather in Telegram, send /newbot, and follow the prompts. Copy the token it gives you and keep it out of your code:
export TELEGRAM_BOT_TOKEN='123456789:AA...'Your first bot
Section titled “Your first bot”<?php
declare(strict_types=1);
require __DIR__.'/vendor/autoload.php';
use Pam\Telegram\Bot;use Pam\Telegram\Context;use Pam\Telegram\Keyboard\InlineKeyboard;
$bot = new Bot(getenv('TELEGRAM_BOT_TOKEN'));
$bot->command('start', fn (Context $ctx) => $ctx->reply('Hi! Send /balance.'));
$bot->command('balance', fn (Context $ctx) => $ctx->reply('Your balance: $150.00', [ 'reply_markup' => InlineKeyboard::make()->text('Refresh', 'balance:refresh'),]));
$bot->callbackQuery('balance:refresh', function (Context $ctx): void { $ctx->answer('Updated'); $ctx->edit('Your balance: $151.00');});
$bot->on('message:photo', fn (Context $ctx) => $ctx->reply('Nice photo!'));
echo "Bot running. Press Ctrl+C to stop.\n";$bot->run();Run it and send /start to your bot:
pam bot.phprun() uses long polling, so the bot needs no public URL and no open port. Telegram holds each request open for up to 30 seconds while nothing happens, which keeps an idle bot cheap. For production you can switch to webhooks at any time.
How it works
Section titled “How it works”your PHP code │ ▼PAM Telegram ─── middleware, filters, sessions, scenes │ ▼Transport ────── cURL keep-alive · PAM HTTP client · PHP streams │ ▼Telegram Bot API (official HTTPS API)Every update, whether it comes from getUpdates or a webhook, goes through Bot::handle():
- The chat’s session is loaded.
- If the chat is inside a scene, the current scene step handles the update.
- Otherwise the update runs through your middleware and handlers in the order you registered them. The first handler that matches wins.
- The session is saved if it changed.