Login & Sessions
PAM Telegram Web logs in exactly like Telegram Web in a browser: you scan a QR code from the Telegram app on your phone, then type your two-step verification password if the account has one. The browser profile keeps the login, so later runs connect without scanning again.
QR code login
Section titled “QR code login”onQrCode() fires each time Telegram Web shows a new login QR code, roughly every 30 seconds until you scan one. The event carries the decoded login link and can render it for a terminal:
use Pam\TelegramWeb\Event\QrCodeReceived;
$client->onQrCode(static function (QrCodeReceived $event): void { echo "\033[2J\033[H"; // clear the screen so the new code replaces the old one echo "Open Telegram > Settings > Devices > Link Desktop Device\n\n"; echo $event->terminal(), "\n";});$event->link holds the raw tg://login?... link; render it with any QR library to show it on a web page instead. Render the code only to the account owner, never to a log.
Two-step verification
Section titled “Two-step verification”If the account has a cloud password, onPasswordRequired() fires after the scan. Ask for the password and pass it to submitPassword(). In a terminal, hide the input:
$client->onPasswordRequired(static function () use ($client): void { echo "\nTwo-step verification password: "; system('stty -echo'); $password = fgets(STDIN); system('stty echo'); echo "\n";
if ($password !== false) { $client->submitPassword(rtrim($password, "\r\n")); }});The password is marked #[SensitiveParameter], so it does not appear in stack traces.
Waiting for the login
Section titled “Waiting for the login”run() keeps going until you stop it, delivering Ready when the login completes. If you prefer a script that logs in and then does something, block until the account is ready:
$client->waitUntilReady(timeout: 120.0); // throws TelegramWebException on timeout
$me = $client->getMe();echo "Logged in as {$me->name()} (@{$me->username})\n";$client->isReady() tells you whether the account is currently logged in.
The session directory
Section titled “The session directory”By default the browser profile, and with it the login, is stored in .pam-telegram-web/session under the current working directory. Choose the location explicitly in production:
use Pam\TelegramWeb\Client;use Pam\TelegramWeb\ClientOptions;
$client = new Client(new ClientOptions( sessionDirectory: '/srv/acme/telegram/alice',));The directory is created with 0700 permissions. Keep it on persistent storage, out of Git, and out of backups you do not control.
Closing and logging out
Section titled “Closing and logging out”| Method | Effect |
|---|---|
$client->close() |
Closes the browser. The login stays saved, so the next run connects directly. |
$client->logout() |
Ends the Telegram session; the device disappears from Settings > Devices. The next run needs a new QR scan. |
When the session ends for another reason, for example because you terminated it from your phone, onLoggedOut() fires:
$client->onLoggedOut(static function () use ($client): void { echo "Telegram ended this session. Run again to log in.\n"; $client->stop();});Several accounts
Section titled “Several accounts”Each account needs its own session directory and runs its own headless Chrome. Drive them from one loop with pump():
use Pam\TelegramWeb\Client;use Pam\TelegramWeb\ClientOptions;use Pam\TelegramWeb\Event\MessageReceived;
$accounts = [ 'sales' => new Client(new ClientOptions(sessionDirectory: __DIR__.'/.sessions/sales')), 'support' => new Client(new ClientOptions(sessionDirectory: __DIR__.'/.sessions/support')),];
foreach ($accounts as $name => $client) { $client->onMessage(fn (MessageReceived $event) => print "[{$name}] {$event->message->text}\n");}
while (true) { foreach ($accounts as $client) { $client->pump(0.2); }}pump() initializes the client on first use, so you do not need to call initialize() yourself.