Skip to content

Events & the Event Loop

Telegram Web pushes updates to the page as they happen. PAM Telegram Web turns them into typed events and calls your listeners while its event loop runs. Register listeners first, then start the loop with run(), or call pump() from a loop you already own.

Listener Fires when Event properties
onQrCode(fn (QrCodeReceived $e) => ...) A new login QR code is shown. link, terminal()
onPasswordRequired(fn (PasswordRequired $e) => ...) The account has two-step verification.
onReady(fn (Ready $e) => ...) The account is logged in. me (?User)
onMessage(fn (MessageReceived $e) => ...) A new message arrives in any chat, or you send one from another device. message
onMessageEdited(fn (MessageEdited $e) => ...) A message is edited. message
onMessagesDeleted(fn (MessagesDeleted $e) => ...) Messages are deleted. chatId, messageIds
onLoggedOut(fn (LoggedOut $e) => ...) The session ended, by you, another device, or Telegram.
onError(fn (ErrorOccurred $e) => ...) A listener threw an exception. error, event

Event classes live in Pam\TelegramWeb\Event. Every on*() method returns the client, so you can chain them.

onMessage() also fires for messages you send from your phone or another device. Check $message->outgoing before replying, or a bot that answers its own messages will loop:

$client->onMessage(static function (MessageReceived $event): void {
$message = $event->message;
if ($message->outgoing) {
return;
}
// React to messages from other people.
});

Without an onError() listener, an exception thrown by a listener propagates and ends run(). Register onError() to log it and keep the loop running:

use Pam\TelegramWeb\Event\ErrorOccurred;
$client->onError(static function (ErrorOccurred $event): void {
error_log(sprintf('%s while handling %s', $event->error->getMessage(), $event->event::class));
});

run() opens Telegram Web if needed and delivers events until stop() is called, a $shouldStop closure returns true, or a signal arrives. It installs SIGINT and SIGTERM handlers when PCNTL is available (it is in PAM), so Ctrl+C returns from run() instead of killing PHP. Always close the client afterwards so Chrome does not outlive your script:

try {
$client->run(shouldStop: fn (): bool => file_exists(__DIR__.'/stop.flag'));
} finally {
$client->close();
}

For full control, call pump() yourself. It delivers events for up to the given number of seconds, opening Telegram Web on the first call:

$client->initialize(); // optional: pump() does it on first use
while (true) {
$client->pump(1.0);
// Other periodic work, such as sending queued replies.
}

This is also how you run several accounts in one process.

Calls such as sendFile() and downloadMedia() run as jobs inside the page that PHP polls until they finish, so long uploads and downloads are not cut short by DevTools time limits. Their time limit is fileTimeoutSeconds (300 seconds by default). Other calls use requestTimeoutSeconds (30 seconds by default). JavaScript errors surface as TelegramWebException with Telegram’s error text, such as USER_ALREADY_PARTICIPANT.