Skip to content

Commit

Permalink
Merge pull request #148 from netflie/multiple-entries-webhook
Browse files Browse the repository at this point in the history
Multiple entries webhook
  • Loading branch information
aalbarca authored Nov 19, 2023
2 parents 049a2c1 + 6e205be commit 270868c
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 12 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,11 @@ fwrite(STDOUT, print_r($payload, true) . "\n");
// Instantiate the Webhook super class.
$webhook = new WebHook();

// Read the first message
fwrite(STDOUT, print_r($webhook->read(json_decode($payload, true)), true) . "\n");

//Read all messages in case Meta decided to batch them
fwrite(STDOUT, print_r($webhook->readAll(json_decode($payload, true)), true) . "\n");
```

The `Webhook::read` function will return a `Notification` instance. Please, [explore](https://github.com/netflie/whatsapp-cloud-api/tree/main/src/WebHook/Notification "explore") the different notifications availables.
Expand Down
12 changes: 12 additions & 0 deletions src/WebHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,16 @@ public function read(array $payload): ?Notification
return (new NotificationFactory())
->buildFromPayload($payload);
}

/**
* Get all notifications from incoming webhook messages.
*
* @param array $payload Payload received in your endpoint URL.
* @return Notification[] A PHP representation of WhatsApp webhook notifications
*/
public function readAll(array $payload): array
{
return (new NotificationFactory())
->buildAllFromPayload($payload);
}
}
43 changes: 31 additions & 12 deletions src/WebHook/NotificationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,44 @@ public function __construct()
}

public function buildFromPayload(array $payload): ?Notification
{
$notifications = $this->buildAllFromPayload($payload);

return $notifications[0] ?? null;
}

/**
* @return Notification[]
*/
public function buildAllFromPayload(array $payload): array
{
if (!is_array($payload['entry'] ?? null)) {
return null;
return [];
}

$entry = $payload['entry'][0] ?? [];
$message = $entry['changes'][0]['value']['messages'][0] ?? [];
$status = $entry['changes'][0]['value']['statuses'][0] ?? [];
$contact = $entry['changes'][0]['value']['contacts'][0] ?? [];
$metadata = $entry['changes'][0]['value']['metadata'] ?? [];
$notifications = [];

if ($message) {
return $this->message_notification_factory->buildFromPayload($metadata, $message, $contact);
}
foreach ($payload['entry'] as $entry) {
if (!is_array($entry['changes'])) {
continue;
}

foreach ($entry['changes'] as $change) {
$message = $change['value']['messages'][0] ?? [];
$status = $change['value']['statuses'][0] ?? [];
$contact = $change['value']['contacts'][0] ?? [];
$metadata = $change['value']['metadata'] ?? [];

if ($message) {
$notifications[] = $this->message_notification_factory->buildFromPayload($metadata, $message, $contact);
}

if ($status) {
return $this->status_notification_factory->buildFromPayload($metadata, $status);
if ($status) {
$notifications[] = $this->status_notification_factory->buildFromPayload($metadata, $status);
}
}
}

return null;
return $notifications;
}
}
69 changes: 69 additions & 0 deletions tests/Unit/WebHook/NotificationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,75 @@ public function test_build_from_payload_can_build_a_text_notification()
$this->assertEquals('MESSAGE_BODY', $notification->message());
}

public function test_build_from_payload_can_build_multiple_text_notification()
{
$payload = json_decode('{
"object": "whatsapp_business_account",
"entry": [{
"id": "WHATSAPP_BUSINESS_ACCOUNT_ID",
"changes": [{
"value": {
"messaging_product": "whatsapp",
"metadata": {
"display_phone_number": "PHONE_NUMBER",
"phone_number_id": "PHONE_NUMBER_ID"
},
"contacts": [{
"profile": {
"name": "NAME"
},
"wa_id": "PHONE_NUMBER"
}],
"messages": [{
"from": "PHONE_NUMBER",
"id": "wamid.ID",
"timestamp": "1669233778",
"text": {
"body": "MESSAGE_BODY"
},
"type": "text"
}]
},
"field": "messages"
},
{
"value": {
"messaging_product": "whatsapp",
"metadata": {
"display_phone_number": "PHONE_NUMBER",
"phone_number_id": "PHONE_NUMBER_ID"
},
"contacts": [{
"profile": {
"name": "NAME"
},
"wa_id": "PHONE_NUMBER"
}],
"messages": [{
"from": "PHONE_NUMBER",
"id": "wamid.ID",
"timestamp": "1669233779",
"text": {
"body": "MESSAGE_BODY2"
},
"type": "text"
}]
},
"field": "messages"
}]
}]
}', true);

$notifications = $this->notification_factory->buildAllFromPayload($payload);

$this->assertCount(2, $notifications);

$this->assertInstanceOf(Notification\Text::class, $notifications[0]);
$this->assertInstanceOf(Notification\Text::class, $notifications[1]);
$this->assertEquals('MESSAGE_BODY', $notifications[0]->message());
$this->assertEquals('MESSAGE_BODY2', $notifications[1]->message());
}

public function test_build_from_payload_can_build_a_reaction_notification()
{
$payload = json_decode('{
Expand Down

0 comments on commit 270868c

Please sign in to comment.