Skip to content

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.

The package requires PHP 8.3 or newer. With the PAM runtime installed (see Installation):

Terminal window
mkdir acme-bot && cd acme-bot
pam composer init --no-interaction
pam composer require pushinbr/pam-telegram:^0.1

Open @BotFather in Telegram, send /newbot, and follow the prompts. Copy the token it gives you and keep it out of your code:

Terminal window
export TELEGRAM_BOT_TOKEN='123456789:AA...'
bot.php
<?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:

Terminal window
pam bot.php

run() 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.

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():

  1. The chat’s session is loaded.
  2. If the chat is inside a scene, the current scene step handles the update.
  3. Otherwise the update runs through your middleware and handlers in the order you registered them. The first handler that matches wins.
  4. The session is saved if it changed.