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

React to Message #176

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
22b0230
Bump actions/checkout from 3 to 4
dependabot[bot] Oct 1, 2023
049a2c1
Merge pull request #146 from netflie/add-reply-to-feature
aalbarca Nov 19, 2023
6a32d7c
Update README.md
arneee Sep 8, 2023
8ec015e
Allow multiple entries and changes within one webhook call
arneee Aug 22, 2023
6e205be
Code style
arneee Aug 22, 2023
270868c
Merge pull request #148 from netflie/multiple-entries-webhook
aalbarca Nov 19, 2023
2aa75dd
Merge pull request #140 from netflie/dependabot/github_actions/action…
aalbarca Nov 19, 2023
0eb45d5
Fix optional header for interactive list messages
horatiua Aug 22, 2023
4262e84
Merge pull request #149 from netflie/fix-optional-interactive-message…
aalbarca Nov 19, 2023
9ebe853
Add button reply message section
derrickobedgiu1 Aug 12, 2023
35ec5f6
add sendButton method
derrickobedgiu1 Aug 12, 2023
758f2da
Add sendButton
derrickobedgiu1 Aug 12, 2023
e30562d
Footer fix, linting and tests
ianrothmann Aug 21, 2023
d4964da
StyleCI issues for Reply Buttons
ianrothmann Sep 11, 2023
5b65958
button-reply: add "reply to" feature for buttons
aalbarca Nov 19, 2023
67e3043
Merge pull request #150 from netflie/button-reply
aalbarca Nov 19, 2023
08e5fea
fix-emoji-removed-notification: fix a notification error when an emoj…
aalbarca Nov 19, 2023
1ece11a
fix-non-nullable-superclass-vars: set to empty string non nullable a…
aalbarca Nov 19, 2023
f7ff93a
Merge pull request #151 from netflie/fix-emoji-removed-notification
aalbarca Nov 19, 2023
2f9013e
Merge pull request #152 from netflie/fix-non-nullable-superclass-vars
aalbarca Nov 19, 2023
f3b9626
update-default-graph-version-to-18: update default Graph version to v18
aalbarca Nov 19, 2023
cca16a9
Merge pull request #153 from netflie/update-default-graph-version-to-18
aalbarca Nov 19, 2023
7c7f89c
Update CHANGELOG
aalbarca Nov 19, 2023
b8d6cbf
add replying message sample code
aalbarca Nov 19, 2023
3d14b4e
Merge pull request #154 from netflie/update-readme
aalbarca Nov 19, 2023
c8c5971
Bump actions/dependency-review-action from 3 to 4
dependabot[bot] Feb 1, 2024
2a8fafc
Merge pull request #169 from netflie/dependabot/github_actions/action…
aalbarca Feb 12, 2024
b162fbd
Added react to message feature
derrickobedgiu1 Mar 11, 2024
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
4 changes: 2 additions & 2 deletions .github/workflows/dependency-review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: 'Checkout Repository'
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: 'Dependency Review'
uses: actions/dependency-review-action@v3
uses: actions/dependency-review-action@v4
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Changelog

## 2.2.0 - 2023-11-19

### What's Changed

- Updated default Graph version to v18
- Retrieve and update business profile. Thanks @winkelco
- Allow to reply messages. Thanks @johnflash4real
- Add Interactive buttons. Thanks @derrickobedgiu1 and @ianrothmann
- Retrieve a batch of notifications from Webhook. Thanks @arneee
- Optional headers in interactive list messages. Thanks @horatiua
- Fix non nullable variables when empty ENV variables are defined

## 2.1.0 - 2023-08-12

### What's Changed
Expand Down
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,51 @@ $whatsapp_cloud_api->sendList(
);
```

### Send a button reply message

```php
<?php

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

$whatsapp_cloud_api = new WhatsAppCloudApi([
'from_phone_number_id' => 'your-configured-from-phone-number-id',
'access_token' => 'your-facebook-whatsapp-application-token'
]);

$rows = [
new Button('button-1', 'Yes'),
new Button('button-2', 'No'),
new Button('button-3', 'Not Now'),
];
$action = new ButtonAction($rows);

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

### Replying messages

You can reply a previous sent message:

```php
<?php

$whatsapp_cloud_api
->replyTo('<whatsapp-message-id-to-reply>')
->sendTextMessage(
'34676104574',
'Hey there! I\'m using WhatsApp Cloud API. Visit https://www.netflie.es'
);
```

## Media messages
### Upload media resources
Media messages accept as identifiers an Internet URL pointing to a public resource (image, video, audio, etc.). When you try to send a media message from a URL you must instantiate the `LinkID` object.
Expand Down Expand Up @@ -294,7 +339,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.
Expand Down Expand Up @@ -341,6 +390,7 @@ Fields list: https://developers.facebook.com/docs/whatsapp/cloud-api/reference/b
- Send Locations
- Send Contacts
- Send Lists
- Send Buttons
- Upload media resources to WhatsApp servers
- Download media resources from WhatsApp servers
- Mark messages as read
Expand Down
25 changes: 25 additions & 0 deletions src/Message/ButtonReply/Button.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Netflie\WhatsAppCloudApi\Message\ButtonReply;

class Button
{
private $id;
private $title;

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

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

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

namespace Netflie\WhatsAppCloudApi\Message\ButtonReply;

class ButtonAction
{
private $buttons;

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

public function buttons(): array
{
$buttonActions = [];

foreach ($this->buttons as $button) {
$buttonActions[] = [
"type" => "reply",
"reply" => [
"id" => $button->id(),
"title" => $button->title(),
],
];
}

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

namespace Netflie\WhatsAppCloudApi\Message;

use Netflie\WhatsAppCloudApi\Message\ButtonReply\ButtonAction;

class ButtonReplyMessage extends Message
{
protected string $type = 'button';

private ?string $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)
{
$this->body = $body;
$this->action = $action;
$this->header = $header;
$this->footer = $footer;

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

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

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

public function action(): ButtonAction
{
return $this->action;
}

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

namespace Netflie\WhatsAppCloudApi\Message;

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

private $emoji;

private $message_id;

/**
* {@inheritdoc}
*/
public function __construct(string $to, string $emoji, string $message_id)
{
$this->emoji = $emoji;
$this->message_id = $message_id;

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

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

public function message_id(): string
{
return $this->message_id;
}
}
42 changes: 42 additions & 0 deletions src/Request/MessageRequest/RequestButtonReplyMessage.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;

class RequestButtonReplyMessage extends MessageRequest
{
public function body(): array
{
$body = [
'messaging_product' => $this->message->messagingProduct(),
'recipient_type' => $this->message->recipientType(),
'to' => $this->message->to(),
'type' => 'interactive',
'interactive' => [
'type' => 'button',
'body' => ['text' => $this->message->body()],
'action' => ['buttons' => $this->message->action()->buttons()],
],
];

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

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

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

return $body;
}
}
11 changes: 7 additions & 4 deletions src/Request/MessageRequest/RequestOptionsListMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@ public function body(): array
'type' => 'interactive',
'interactive' => [
'type' => $this->message->type(),
'header' => [
'type' => 'text',
'text' => $this->message->header(),
],
'body' => ['text' => $this->message->body()],
'action' => $this->message->action(),
],
];

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

if ($this->message->footer()) {
$body['interactive']['footer'] = ['text' => $this->message->footer()];
}
Expand Down
27 changes: 27 additions & 0 deletions src/Request/MessageRequest/RequestReactionMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Netflie\WhatsAppCloudApi\Request\MessageRequest;

use Netflie\WhatsAppCloudApi\Request\MessageRequest;

final class RequestReactionMessage extends MessageRequest
{
/**
* {@inheritdoc}
*/
public function body(): array
{
$body = [
'messaging_product' => $this->message->messagingProduct(),
'recipient_type' => $this->message->recipientType(),
'to' => $this->message->to(),
'type' => $this->message->type(),
$this->message->type() => [
'message_id' => $this->message->message_id(),
'emoji' => $this->message->emoji(),
],
];

return $body;
}
}
12 changes: 12 additions & 0 deletions src/WebHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/WebHook/Notification/MessageNotificationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private function buildMessageNotification(array $metadata, array $message): Mess
$message['id'],
new Support\Business($metadata['phone_number_id'], $metadata['display_phone_number']),
$message['reaction']['message_id'],
$message['reaction']['emoji'],
$message['reaction']['emoji'] ?? '',
$message['timestamp']
);
case 'sticker':
Expand Down
Loading