From 6a32d7cea2e76a5e03df34c4be25613de5907faf Mon Sep 17 00:00:00 2001 From: Arne <1255879+arneee@users.noreply.github.com> Date: Fri, 8 Sep 2023 22:20:44 +0200 Subject: [PATCH 1/3] Update README.md --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index e4caa5f..c8f4b0f 100644 --- a/README.md +++ b/README.md @@ -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. From 8ec015e02723638292328ce463edf0c48ee3b24b Mon Sep 17 00:00:00 2001 From: arneee <1255879+arneee@users.noreply.github.com> Date: Tue, 22 Aug 2023 21:18:30 +0200 Subject: [PATCH 2/3] Allow multiple entries and changes within one webhook call --- src/WebHook.php | 12 ++++ src/WebHook/NotificationFactory.php | 45 ++++++++---- .../Unit/WebHook/NotificationFactoryTest.php | 69 +++++++++++++++++++ 3 files changed, 114 insertions(+), 12 deletions(-) diff --git a/src/WebHook.php b/src/WebHook.php index 77952f7..86ebfd5 100644 --- a/src/WebHook.php +++ b/src/WebHook.php @@ -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); + } } diff --git a/src/WebHook/NotificationFactory.php b/src/WebHook/NotificationFactory.php index f5d7176..359281e 100644 --- a/src/WebHook/NotificationFactory.php +++ b/src/WebHook/NotificationFactory.php @@ -14,25 +14,46 @@ 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; } } diff --git a/tests/Unit/WebHook/NotificationFactoryTest.php b/tests/Unit/WebHook/NotificationFactoryTest.php index 9471536..3164487 100644 --- a/tests/Unit/WebHook/NotificationFactoryTest.php +++ b/tests/Unit/WebHook/NotificationFactoryTest.php @@ -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('{ From 6e205bea959e8f8d75f54d5ef2a5c08c23d53241 Mon Sep 17 00:00:00 2001 From: arneee <1255879+arneee@users.noreply.github.com> Date: Tue, 22 Aug 2023 21:22:07 +0200 Subject: [PATCH 3/3] Code style --- src/WebHook/NotificationFactory.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/WebHook/NotificationFactory.php b/src/WebHook/NotificationFactory.php index 359281e..6a1886b 100644 --- a/src/WebHook/NotificationFactory.php +++ b/src/WebHook/NotificationFactory.php @@ -31,14 +31,12 @@ public function buildAllFromPayload(array $payload): array $notifications = []; - foreach($payload['entry'] as $entry) { - - if(!is_array($entry['changes'])) { + foreach ($payload['entry'] as $entry) { + if (!is_array($entry['changes'])) { continue; } - foreach($entry['changes'] as $change) { - + foreach ($entry['changes'] as $change) { $message = $change['value']['messages'][0] ?? []; $status = $change['value']['statuses'][0] ?? []; $contact = $change['value']['contacts'][0] ?? [];