Skip to content

Commit

Permalink
Merge pull request #191 from derrickobedgiu1/catalog-message
Browse files Browse the repository at this point in the history
Add Catalog Message
  • Loading branch information
aalbarca authored Jul 10, 2024
2 parents 3f968a7 + a7ead6c commit 79632d0
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 1 deletion.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,23 @@ $whatsapp_cloud_api->sendCtaUrl(
);
```

### Send Catalog Message

```php
<?php

$body = 'Hello! Thanks for your interest. Ordering is easy. Just visit our catalog and add items you\'d like to purchase.';
$footer = 'Best grocery deals on WhatsApp!';
$sku_thumbnail = '<product-sku-id>'; // product sku id to use as header thumbnail

$whatsapp_cloud_api->sendCatalog(
'<destination-phone-number>',
$body,
$footer, // optional
$sku_thumbnail // optional
);
```

### Send a button reply message

```php
Expand Down
44 changes: 44 additions & 0 deletions src/Message/CatalogMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Netflie\WhatsAppCloudApi\Message;

final class CatalogMessage extends Message
{
/**
* {@inheritdoc}
*/
protected string $type = 'catalog_message';

private string $body;

private ?string $footer;

private ?string $thumbnail_product_retailer_id;

/**
* {@inheritdoc}
*/
public function __construct(string $to, string $body, ?string $footer, ?string $thumbnail_product_retailer_id, ?string $reply_to)
{
$this->body = $body;
$this->footer = $footer;
$this->thumbnail_product_retailer_id = $thumbnail_product_retailer_id;

parent::__construct($to, $reply_to);
}

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

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

public function thumbnail_product_retailer_id(): ?string
{
return $this->thumbnail_product_retailer_id;
}
}
42 changes: 42 additions & 0 deletions src/Request/MessageRequest/RequestCatalogMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Netflie\WhatsAppCloudApi\Request\MessageRequest;

use Netflie\WhatsAppCloudApi\Request\MessageRequest;

final class RequestCatalogMessage extends MessageRequest
{
/**
* {@inheritdoc}
*/
public function body(): array
{
$body = [
'messaging_product' => $this->message->messagingProduct(),
'recipient_type' => $this->message->recipientType(),
'to' => $this->message->to(),
'type' => 'interactive',
'interactive' => [
'type' => $this->message->type(),
'body' => ['text' => $this->message->body()],
'action' => [
'name' => 'catalog_message',
],
],
];

if ($this->message->thumbnail_product_retailer_id()) {
$body['interactive']['action']['parameters'] = ['thumbnail_product_retailer_id' => $this->message->thumbnail_product_retailer_id()];
}

if ($this->message->footer()) {
$body['interactive']['footer'] = ['text' => $this->message->footer()];
}

if ($this->message->replyTo()) {
$body['context']['message_id'] = $this->message->replyTo();
}

return $body;
}
}
24 changes: 24 additions & 0 deletions src/WhatsAppCloudApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,30 @@ public function downloadMedia(string $media_id): Response
return $this->client->downloadMedia($request);
}

/**
* Sends a catalog message.
*
* @param string $to WhatsApp ID or phone number for the person you want to send the message to.
* @param string $body The body of the catalog message.
* @param ?string $footer The footer of the catalog message.
* @param ?string $thumbnail_product_retailer_id The product retailer ID to use as thumbnail.
* @return Response
*
* @throws Response\ResponseException
*/
public function sendCatalog(string $to, string $body, ?string $footer = '', ?string $thumbnail_product_retailer_id = '')
{
$message = new Message\CatalogMessage($to, $body, $footer, $thumbnail_product_retailer_id, $this->reply_to);
$request = new Request\MessageRequest\RequestCatalogMessage(
$message,
$this->app->accessToken(),
$this->app->fromPhoneNumberId(),
$this->timeout
);

return $this->client->sendMessage($request);
}

/**
* Mark a message as read
*
Expand Down
16 changes: 15 additions & 1 deletion tests/Integration/WhatsAppCloudApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,20 @@ public function test_send_cta_url()
$this->assertEquals(false, $response->isError());
}

public function test_send_catalog_message()
{
$body = 'Hello! Thanks for your interest. Ordering is easy. Just visit our catalog and add items you\'d like to purchase.';
$footer = 'Best grocery deals on WhatsApp!';
$response = $this->whatsapp_app_cloud_api->sendCatalog(
WhatsAppCloudApiTestConfiguration::$to_phone_number_id,
$body,
$footer,
);

$this->assertEquals(200, $response->httpStatusCode());
$this->assertEquals(false, $response->isError());
}

public function test_send_reply_buttons()
{
$buttonRows = [
Expand Down Expand Up @@ -393,7 +407,7 @@ public function test_update_business_profile()
{
$response = $this->whatsapp_app_cloud_api->updateBusinessProfile([
'about' => 'About text',
'email' => 'my-email@email.com'
'email' => 'my-email@email.com',
]);

$this->assertEquals(200, $response->httpStatusCode());
Expand Down
45 changes: 45 additions & 0 deletions tests/Unit/WhatsAppCloudApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,51 @@ public function test_update_business_profile()
$this->assertEquals(false, $response->isError());
}

public function test_send_catalog_message()
{
$to = $this->faker->phoneNumber;
$url = $this->buildMessageRequestUri();
$message = $this->faker->text(1024);
$footer = $this->faker->text(60);
$product_retailer_id = $this->faker->text;

$body = [
'messaging_product' => 'whatsapp',
'recipient_type' => 'individual',
'to' => $to,
'type' => 'interactive',
'interactive' => [
'type' => 'catalog_message',
'body' => ['text' => $message],
'footer' => ['text' => $footer],
'action' => [
'name' => 'catalog_message',
'parameters' => ['thumbnail_product_retailer_id' => $product_retailer_id],
],
],
];
$headers = [
'Authorization' => 'Bearer ' . $this->access_token,
];

$this->client_handler
->postJsonData($url, $body, $headers, Argument::type('int'))
->shouldBeCalled()
->willReturn(new RawResponse($headers, $this->successfulMessageNodeResponse(), 200));

$response = $this->whatsapp_app_cloud_api->sendCatalog(
$to,
$message,
$footer,
$product_retailer_id
);

$this->assertEquals(200, $response->httpStatusCode());
$this->assertEquals(json_decode($this->successfulMessageNodeResponse(), true), $response->decodedBody());
$this->assertEquals($this->successfulMessageNodeResponse(), $response->body());
$this->assertEquals(false, $response->isError());
}

public function test_mark_a_message_as_read()
{
$url = $this->buildMessageRequestUri();
Expand Down

0 comments on commit 79632d0

Please sign in to comment.