Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
aalbarca authored Aug 28, 2024
2 parents 157e13c + 0c3acae commit 719c7cf
Show file tree
Hide file tree
Showing 18 changed files with 701 additions and 6 deletions.
65 changes: 64 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ $whatsapp_cloud_api->sendCtaUrl(

$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
$sku_thumbnail = '<product-sku-id>'; // product sku id to use as header thumbnail

$whatsapp_cloud_api->sendCatalog(
'<destination-phone-number>',
Expand Down Expand Up @@ -297,6 +297,67 @@ $whatsapp_cloud_api->sendButton(
);
```

### Send Multi Product Message
```php
<?php

use Netflie\WhatsAppCloudApi\WhatsAppCloudApi;
use Netflie\WhatsAppCloudApi\Message\MultiProduct\Row;
use Netflie\WhatsAppCloudApi\Message\MultiProduct\Section;
use Netflie\WhatsAppCloudApi\Message\MultiProduct\Action;

$rows_section_1 = [
new Row('<product-sku-id>'),
new Row('<product-sku-id>'),
// etc
];

$rows_section_2 = [
new Row('<product-sku-id>'),
new Row('<product-sku-id>'),
new Row('<product-sku-id>'),
// etc
];

$sections = [
new Section('Section 1', $rows_section_1),
new Section('Section 2', $rows_section_2),
];

$action = new Action($sections);
$catalog_id = '<catalog-id>';
$header = 'Grocery Collections';
$body = 'Hello! Thanks for your interest. Here\'s what we can offer you under our grocery collection. Thank you for shopping with us.';
$footer = 'Subject to T&C';

$whatsapp_cloud_api->sendMultiProduct(
'<destination-phone-number>',
$catalog_id,
$action,
$header,
$body,
$footer // optional
);
```

### Send Single Product Message
```php
<?php

$catalog_id = '<catalog-id>';
$sku_id = '<product-sku-id>';
$body = 'Hello! Here\'s your requested product. Thanks for shopping with us.';
$footer = 'Subject to T&C';

$whatsapp_cloud_api->sendSingleProduct(
'<destination-phone-number>',
$catalog_id,
$sku_id,
$body, // body: optional
$footer // footer: optional
);
```

### Replying messages

You can reply a previous sent message:
Expand Down Expand Up @@ -458,6 +519,8 @@ Fields list: https://developers.facebook.com/docs/whatsapp/cloud-api/reference/b
- Send Contacts
- Send Lists
- Send Buttons
- Send Multi Product Message
- Send Single Product
- Upload media resources to WhatsApp servers
- Download media resources from WhatsApp servers
- Mark messages as read
Expand Down
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
27 changes: 27 additions & 0 deletions src/Message/MultiProduct/Action.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Netflie\WhatsAppCloudApi\Message\MultiProduct;

final class Action
{
protected array $sections;

public function __construct(array $sections)
{
$this->sections = $sections;
}

public function sections(): array
{
$result = [];

foreach ($this->sections as $section) {
$result[] = [
'title' => $section->title(),
'product_items' => $section->rows(),
];
}

return $result;
}
}
18 changes: 18 additions & 0 deletions src/Message/MultiProduct/Row.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Netflie\WhatsAppCloudApi\Message\MultiProduct;

final class Row
{
protected string $product_retailer_id;

public function __construct(string $product_retailer_id)
{
$this->product_retailer_id = $product_retailer_id;
}

public function product_retailer_id(): string
{
return $this->product_retailer_id;
}
}
34 changes: 34 additions & 0 deletions src/Message/MultiProduct/Section.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Netflie\WhatsAppCloudApi\Message\MultiProduct;

final class Section
{
protected string $title;

protected array $rows;

public function __construct(string $title, array $rows)
{
$this->title = $title;
$this->rows = $rows;
}

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

public function rows(): array
{
$result = [];

foreach ($this->rows as $row) {
$result[] = [
'product_retailer_id' => $row->product_retailer_id(),
];
}

return $result;
}
}
65 changes: 65 additions & 0 deletions src/Message/MultiProductMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Netflie\WhatsAppCloudApi\Message;

use Netflie\WhatsAppCloudApi\Message\MultiProduct\Action;

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

private string $header;

private string $body;

private int $catalog_id;

private ?string $footer;

private Action $action;

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

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

public function header(): array
{
return [
'type' => 'text',
'text' => $this->header,
];
}

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

public function catalog_id(): int
{
return $this->catalog_id;
}

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

public function sections(): array
{
return $this->action->sections();
}
}
52 changes: 52 additions & 0 deletions src/Message/SingleProductMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Netflie\WhatsAppCloudApi\Message;

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

private int $catalog_id;

private string $product_retailer_id;

private ?string $body;

private ?string $footer;

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

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

public function catalog_id(): int
{
return $this->catalog_id;
}

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

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

public function footer(): ?string
{
return $this->footer;
}
}
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
40 changes: 40 additions & 0 deletions src/Request/MessageRequest/RequestMultiProductMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Netflie\WhatsAppCloudApi\Request\MessageRequest;

use Netflie\WhatsAppCloudApi\Request\MessageRequest;

final class RequestMultiProductMessage 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(),
'header' => $this->message->header(),
'body' => ['text' => $this->message->body()],
'action' => [
'catalog_id' => $this->message->catalog_id(),
'sections' => $this->message->sections(),
],
],
];

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

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

return $body;
}
}
42 changes: 42 additions & 0 deletions src/Request/MessageRequest/RequestSingleProductMessage.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 RequestSingleProductMessage 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(),
'action' => [
'catalog_id' => $this->message->catalog_id(),
'product_retailer_id' => $this->message->product_retailer_id(),
],
],
];

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

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

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

return $body;
}
}
Loading

0 comments on commit 719c7cf

Please sign in to comment.