Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Interactive reply buttons media header #212

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,13 @@ $whatsapp_cloud_api->sendCatalog(
```

### Send a button reply message

Button reply with Text Header
```php
<?php

use Netflie\WhatsAppCloudApi\WhatsAppCloudApi;
use Netflie\WhatsAppCloudApi\Message\ButtonReply\Button;
use Netflie\WhatsAppCloudApi\Message\ButtonReply\TextHeader;
use Netflie\WhatsAppCloudApi\Message\ButtonReply\ButtonAction;

$whatsapp_cloud_api = new WhatsAppCloudApi([
Expand All @@ -288,15 +289,61 @@ $rows = [
];
$action = new ButtonAction($rows);

$header = new TextHeader('RATE US');

$whatsapp_cloud_api->sendButton(
'<destination-phone-number>',
'Would you like to rate us on Trustpilot?',
$action,
'RATE US', // Optional: Specify a header (type "text")
$header, // Optional, can be text, image, video, or document
'Please choose an option' // Optional: Specify a footer
);
```

Button Reply with Image Header
```php
use Netflie\WhatsAppCloudApi\Message\ButtonReply\ImageHeader;
use Netflie\WhatsAppCloudApi\Message\Media\LinkID;

$link_id = new LinkID('http(s)://image-url');
$header = new ImageHeader($link_id);

//or

$media_id = new MediaObjectID('<image-object-id>');
$header = new ImageHeader($media_id);
```

Button Reply with Video Header
```php
use Netflie\WhatsAppCloudApi\Message\ButtonReply\VideoHeader;
use Netflie\WhatsAppCloudApi\Message\Media\LinkID;

$link_id = new LinkID('http(s)://video-url');
$header = new VideoHeader($link_id);

//or

$media_id = new MediaObjectID('<video-object-id>');
$header = new VideoHeader($media_id);
```

Button Reply with Document Header
```php
use Netflie\WhatsAppCloudApi\Message\ButtonReply\DocumentHeader;
use Netflie\WhatsAppCloudApi\Message\Media\LinkID;

$filename = 'whatsapp-cloud-api-from-url.pdf';
$link_id = new LinkID('http(s)://document-url');
$header = new DocumentHeader($link_id);

//or

$filename = 'whatsapp-cloud-api-from-id.pdf';
$media_id = new MediaObjectID('<document-object-id>');
$header = new DocumentoHeader($media_id,);
```

### Send Multi Product Message
```php
<?php
Expand Down
29 changes: 29 additions & 0 deletions src/Message/ButtonReply/DocumentHeader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Netflie\WhatsAppCloudApi\Message\ButtonReply;

use Netflie\WhatsAppCloudApi\Message\Media\MediaID;

final class DocumentHeader extends Header
{
protected string $type = 'document';
private MediaID $id;
private string $filename;

public function __construct(MediaID $id, string $filename)
{
$this->id = $id;
$this->filename = $filename;
}

public function getBody(): array
{
return [
'type' => $this->type,
'document' => [
$this->id->type() => $this->id->value(),
'filename' => $this->filename,
],
];
}
}
10 changes: 10 additions & 0 deletions src/Message/ButtonReply/Header.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Netflie\WhatsAppCloudApi\Message\ButtonReply;

abstract class Header
{
protected string $type;

abstract public function getBody(): array;
}
26 changes: 26 additions & 0 deletions src/Message/ButtonReply/ImageHeader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Netflie\WhatsAppCloudApi\Message\ButtonReply;

use Netflie\WhatsAppCloudApi\Message\Media\MediaID;

final class ImageHeader extends Header
{
protected string $type = 'image';
private MediaID $id;

public function __construct(MediaID $id)
{
$this->id = $id;
}

public function getBody(): array
{
return [
'type' => $this->type,
'image' => [
$this->id->type() => $this->id->value(),
],
];
}
}
22 changes: 22 additions & 0 deletions src/Message/ButtonReply/TextHeader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Netflie\WhatsAppCloudApi\Message\ButtonReply;

final class TextHeader extends Header
{
protected string $type = 'text';
private string $text;

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

public function getBody(): array
{
return [
'type' => $this->type,
'text' => $this->text,
];
}
}
26 changes: 26 additions & 0 deletions src/Message/ButtonReply/VideoHeader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Netflie\WhatsAppCloudApi\Message\ButtonReply;

use Netflie\WhatsAppCloudApi\Message\Media\MediaID;

final class VideoHeader extends Header
{
protected string $type = 'video';
private MediaID $id;

public function __construct(MediaID $id)
{
$this->id = $id;
}

public function getBody(): array
{
return [
'type' => $this->type,
'video' => [
$this->id->type() => $this->id->value(),
],
];
}
}
11 changes: 6 additions & 5 deletions src/Message/ButtonReplyMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@
namespace Netflie\WhatsAppCloudApi\Message;

use Netflie\WhatsAppCloudApi\Message\ButtonReply\ButtonAction;
use Netflie\WhatsAppCloudApi\Message\ButtonReply\Header;

class ButtonReplyMessage extends Message
final class ButtonReplyMessage extends Message
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The class ButtonReplyMEssage yet exists in previous versions so you cannot convert it to final class because this is a breaking change (if anyone extended this class in our code you'll be breaking it)

{
protected string $type = 'button';

private ?string $header;
private ?Header $header;

private string $body;

private ?string $footer;

private ButtonAction $action;

public function __construct(string $to, string $body, ButtonAction $action, ?string $header = null, ?string $footer = null, ?string $reply_to = null)
public function __construct(string $to, string $body, ButtonAction $action, ?Header $header = null, ?string $footer = null, ?string $reply_to = null)
{
$this->body = $body;
$this->action = $action;
Expand All @@ -26,9 +27,9 @@ public function __construct(string $to, string $body, ButtonAction $action, ?str
parent::__construct($to, $reply_to);
}

public function header(): ?string
public function header(): ?array
{
return $this->header;
return $this->header ? $this->header->getBody() : null;
}

public function body(): string
Expand Down
7 changes: 2 additions & 5 deletions src/Request/MessageRequest/RequestButtonReplyMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Netflie\WhatsAppCloudApi\Request\MessageRequest;

class RequestButtonReplyMessage extends MessageRequest
final class RequestButtonReplyMessage extends MessageRequest
{
public function body(): array
{
Expand All @@ -21,10 +21,7 @@ public function body(): array
];

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

if ($this->message->footer()) {
Expand Down
14 changes: 12 additions & 2 deletions src/WhatsAppCloudApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
namespace Netflie\WhatsAppCloudApi;

use Netflie\WhatsAppCloudApi\Message\ButtonReply\ButtonAction;
use Netflie\WhatsAppCloudApi\Message\ButtonReply\Header as ButtonHeader;
use Netflie\WhatsAppCloudApi\Message\ButtonReply\TextHeader;
use Netflie\WhatsAppCloudApi\Message\Contact\ContactName;
use Netflie\WhatsAppCloudApi\Message\Contact\Phone;
use Netflie\WhatsAppCloudApi\Message\CtaUrl\Header;
use Netflie\WhatsAppCloudApi\Message\Error\InvalidMessage;
use Netflie\WhatsAppCloudApi\Message\Media\MediaID;
use Netflie\WhatsAppCloudApi\Message\MultiProduct\Action as MultiProductAction;
use Netflie\WhatsAppCloudApi\Message\OptionsList\Action;
Expand Down Expand Up @@ -235,7 +238,7 @@ public function sendSticker(string $to, MediaID $sticker_id): Response
* @param float $longitude Longitude position.
* @param float $latitude Latitude position.
* @param string $name Name of location sent.
* @param address $address Address of location sent.
* @param string $address Address of location sent.
*
* @return Response
*
Expand Down Expand Up @@ -354,8 +357,15 @@ public function sendCtaUrl(string $to, string $displayText, string $url, ?Header
return $this->client->sendMessage($request);
}

public function sendButton(string $to, string $body, ButtonAction $action, ?string $header = null, ?string $footer = null): Response
public function sendButton(string $to, string $body, ButtonAction $action, $header = null, ?string $footer = null): Response
{
if (is_string($header)) {
$header = new TextHeader($header);
}

if (!$header instanceof ButtonHeader && $header !== null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this if is not necessary, should you use type hinting in the function:

 public function sendButton(string $to, string $body, ButtonAction $action, ?string | ?ButtonHeader $header = null, ?string $footer = null): Response

throw new InvalidMessage('Header must be instance of Netflie\WhatsAppCloudApi\Message\ButtonReply\Header if not left null');
}
$message = new Message\ButtonReplyMessage(
$to,
$body,
Expand Down
Loading