Events
Everything that happens in the linked WhatsApp session reaches PHP as a typed, immutable event object. Register listeners before calling initialize(), then let run() deliver events for as long as the client is alive.
use Pam\WhatsApp\Event\MessageReceived;use Pam\WhatsApp\Event\CallReceived;use Pam\WhatsApp\EventType;
$client->onMessage(static function (MessageReceived $event): void { // ...});
$client->on(EventType::CallReceived, static function (CallReceived $event): void { $event->call->reject();});
$client->initialize();$client->run();Registering listeners
Section titled “Registering listeners”Shortcut methods exist for the most common events, and on() accepts any EventType. All of them return the client, so you can chain them:
| Shortcut | Equivalent |
|---|---|
onQrCode() |
on(EventType::QrCode, ...) |
onReady() |
on(EventType::Ready, ...) |
onMessage() |
on(EventType::MessageReceived, ...) |
onMessageSent(), onMessageDelivered(), onMessageRead(), onMessageFailed() |
Filters of EventType::MessageAcknowledged by delivery stage. See delivery tracking. |
Event reference
Section titled “Event reference”Every listener receives one object from the Pam\WhatsApp\Event namespace (or a domain object where noted).
Session
Section titled “Session”EventType |
Object | Properties |
|---|---|---|
QrCode |
QrCodeReceived |
code |
PairingCodeReceived |
PairingCodeReceived |
code |
Authenticated |
Authenticated |
timestamp, authPayload |
AuthenticationFailure |
ClientError |
message |
Ready |
Ready |
timestamp |
LoadingScreen |
LoadingScreen |
percent, message |
StateChanged |
ConnectionStateChanged |
state (ConnectionState) |
Disconnected |
Disconnected |
reason (DisconnectionReason: Logout, Conflict, Unlaunched, QrRetryLimit) |
Error |
ClientError |
message |
RemoteSessionSaved |
RemoteSessionSaved |
timestamp |
BatteryChanged |
BatteryChanged |
battery, plugged |
Messages
Section titled “Messages”EventType |
Object | Properties |
|---|---|---|
MessageReceived |
MessageReceived |
message |
MessageAcknowledged |
MessageAcknowledged |
message, ack (MessageAck) |
MessageCreated |
MessageLifecycle |
type, message, context |
MessageEdited |
MessageLifecycle |
type, message, context |
MessageRevokedEveryone, MessageRevokedMe |
MessageLifecycle |
type, message, context |
MessageCiphertext, MessageCiphertextFailed, MediaUploaded |
MessageLifecycle |
type, message, context |
MessageReaction |
Pam\WhatsApp\Reaction |
reaction, senderId, msgId, timestamp |
VoteUpdated |
Pam\WhatsApp\PollVote |
voter, selectedOptions, parentMessage |
MessageReceived fires for messages from other people. MessageCreated also fires for messages the linked account sends, including from the phone, which makes it the right hook for logging every message in a chat.
Chats and contacts
Section titled “Chats and contacts”EventType |
Object | Properties |
|---|---|---|
ChatArchived |
ChatArchiveChanged |
chatId, archived, previousArchived |
ChatRemoved |
ChatRemoved |
chatId |
UnreadCountChanged |
UnreadCountChanged |
chatId, unreadCount |
ContactChanged |
ContactChanged |
message, oldId, newId, isContact |
CallReceived |
CallReceived |
call (with reject()), id, peerId, isVideo, isGroup |
Groups
Section titled “Groups”GroupJoined, GroupLeft, GroupAdminChanged, GroupUpdated, and GroupMembershipRequest all deliver a GroupNotification:
use Pam\WhatsApp\Event\GroupNotification;use Pam\WhatsApp\EventType;
$client->on(EventType::GroupJoined, static function (GroupNotification $event): void { $count = count($event->recipientIds);
$event->reply("Welcome to the group, {$count} new member(s)! Please read the pinned rules.");});A GroupNotification has type (GroupNotificationType: Add, Invite, Remove, Leave, Promote, Demote, Subject, Description, Picture, Announce, Restrict), author, body, chatId, recipientIds, and timestamp, plus getChat(), getContact(), getRecipients(), and reply().
The event loop
Section titled “The event loop”run() blocks and pumps events until the client state becomes Closed or Failed, which happens after destroy(), logout(), a disconnection, or an unrecoverable error.
For more control, drive the loop yourself with pump(). It waits up to the given number of seconds for bridge events, handles them, and returns whether anything was handled:
use Pam\WhatsApp\ClientState;
$client->initialize();
while (!in_array($client->state, [ClientState::Closed, ClientState::Failed], true)) { $client->pump(1.0);
// Do other periodic work here, such as draining an outbound queue.}Stopping cleanly
Section titled “Stopping cleanly”run() does not install signal handlers. To close Chrome when the process receives Ctrl+C or SIGTERM, drive the loop yourself and close the client when a signal arrives:
$stop = false;pcntl_async_signals(true);pcntl_signal(SIGINT, static function () use (&$stop): void { $stop = true; });pcntl_signal(SIGTERM, static function () use (&$stop): void { $stop = true; });
$client->initialize();
try { while (!$stop && !in_array($client->state, [ClientState::Closed, ClientState::Failed], true)) { $client->pump(1.0); }} finally { $client->destroy(); // keeps the saved session for the next start}The PCNTL functions are available in the PAM runtime.