Skip to content

Commit

Permalink
Merge pull request #197 from pravnyadv/main
Browse files Browse the repository at this point in the history
adding flow message support
  • Loading branch information
aalbarca authored Aug 6, 2024
2 parents 955728a + cdfe9b0 commit 8e98f7a
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 5 deletions.
2 changes: 0 additions & 2 deletions src/Message/CtaUrl/TitleHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Netflie\WhatsAppCloudApi\Message\CtaUrl;

use Netflie\WhatsAppCloudApi\Message\CtaUrl\Header;

final class TitleHeader extends Header
{
protected string $title;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function body(): array
{
return array_merge(
[
'messaging_product' => 'whatsapp'
'messaging_product' => 'whatsapp',
],
$this->information
);
Expand Down
42 changes: 42 additions & 0 deletions src/WebHook/Notification/Flow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Netflie\WhatsAppCloudApi\WebHook\Notification;

final class Flow extends MessageNotification
{
private string $name;

private string $body;

private string $response;

public function __construct(
string $id,
Support\Business $business,
string $name,
string $body,
string $response,
string $received_at_timestamp
) {
parent::__construct($id, $business, $received_at_timestamp);

$this->name = $name;
$this->body = $body;
$this->response = $response;
}

public function name(): string
{
return $this->name;
}

public function body(): string
{
return $this->body;
}

public function response(): string
{
return $this->response;
}
}
13 changes: 13 additions & 0 deletions src/WebHook/Notification/MessageNotificationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,19 @@ private function buildMessageNotification(array $metadata, array $message): Mess
$message['timestamp']
);
case 'interactive':
if (isset($message['interactive']['nfm_reply'])) {
$nfmReply = $message['interactive']['nfm_reply'];

return new Flow(
$message['id'],
new Support\Business($metadata['phone_number_id'], $metadata['display_phone_number']),
$nfmReply['name'],
$nfmReply['body'],
$nfmReply['response_json'] ?? '',
$message['timestamp'],
);
}

return new Interactive(
$message['id'],
new Support\Business($metadata['phone_number_id'], $metadata['display_phone_number']),
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function test_update_business_profile()
$request = new Request\BusinessProfileRequest\UpdateBusinessProfileRequest(
[
'about' => 'About text',
'email' => 'my-email@email.com'
'email' => 'my-email@email.com',
],
WhatsAppCloudApiTestConfiguration::$access_token,
WhatsAppCloudApiTestConfiguration::$from_phone_number_id
Expand Down
55 changes: 55 additions & 0 deletions tests/Unit/WebHook/NotificationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,61 @@ public function test_build_from_payload_can_build_a_button_reply_notification()
$this->assertEquals('', $notification->description());
}

public function test_build_from_payload_can_build_a_flow_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_ID"
}
],
"messages": [
{
"from": "PHONE_NUMBER_ID",
"id": "wamid.ID",
"timestamp": 1669233778,
"interactive": {
"type": "nfm_reply",
"nfm_reply": {
"response_json": "{\"screen_0_name_0\":\"Email\",\"screen_0_orderNumber_1\":\"ID\",\"flow_token\":\"unused\"}",
"body": "Sent",
"name": "flow"
}
},
"type": "interactive"
}
]
},
"field": "messages"
}
]
}
]
}', true);

$notification = $this->notification_factory->buildFromPayload($payload);

$this->assertInstanceOf(Notification\Flow::class, $notification);
$this->assertEquals('flow', $notification->name());
$this->assertEquals('Sent', $notification->body());
$this->assertEquals('{"screen_0_name_0":"Email","screen_0_orderNumber_1":"ID","flow_token":"unused"}', $notification->response());
}

public function test_build_from_payload_can_build_an_order_notification()
{
$payload = json_decode('{
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/WhatsAppCloudApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ public function test_send_cta_url()
'parameters' => [
'display_text' => $this->faker->text(24),
'url' => $this->faker->url,
]
],
];

$body = [
Expand Down

0 comments on commit 8e98f7a

Please sign in to comment.