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

Add react to message #190

Merged
merged 2 commits into from
Jul 10, 2024
Merged
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
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,27 @@ $whatsapp_cloud_api
);
```

### React to a Message

You can react to a message from your conversations if you know the messageid

```php
<?php

$whatsapp_cloud_api->sendReaction(
'<destination-phone-number>',
'<message-id-to-react-to>',
'👍', // the emoji
);

// Unreact to a message
$whatsapp_cloud_api->sendReaction(
'<destination-phone-number>',
'<message-id-to-unreact-to>'
);

```

## 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 @@ -413,6 +434,7 @@ Fields list: https://developers.facebook.com/docs/whatsapp/cloud-api/reference/b
- Upload media resources to WhatsApp servers
- Download media resources from WhatsApp servers
- Mark messages as read
- React to a Message
- Get/Update Business Profile
- Webhook verification
- Webhook notifications
Expand Down
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 $message_id, string $emoji)
{
$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;
}
}
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;
}
}
24 changes: 24 additions & 0 deletions src/WhatsAppCloudApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,30 @@ public function markMessageAsRead(string $message_id): Response
return $this->client->sendMessage($request);
}

/**
* Sends a reaction to a provided message id.
*
* @param string $to WhatsApp ID or phone number for the person you want to send a message to.
* @param string $message_id The ID of the message to react to.
* @param string $emoji The emoji to use as a reaction.
* @return Response
*
* @throws Response\ResponseException
*/
public function sendReaction(string $to, string $message_id, string $emoji = ''): Response
{
$message = new Message\ReactionMessage($to, $message_id, $emoji);

$request = new Request\MessageRequest\RequestReactionMessage(
$message,
$this->app->accessToken(),
$this->app->fromPhoneNumberId(),
$this->timeout
);

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

/**
* Get Business Profile
*
Expand Down
47 changes: 47 additions & 0 deletions tests/Integration/WhatsAppCloudApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,53 @@ public function test_send_reply_buttons()
$this->assertEquals(false, $response->isError());
}

public function test_send_reaction_message()
{
$textMessage = $this->whatsapp_app_cloud_api->sendTextMessage(
WhatsAppCloudApiTestConfiguration::$to_phone_number_id,
'This text will receive a reaction',
true
);

$messageId = $textMessage->decodedBody()['messages'][0]['id'];

$response = $this->whatsapp_app_cloud_api->sendReaction(
WhatsAppCloudApiTestConfiguration::$to_phone_number_id,
$messageId,
'👍'
);

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

public function test_send_remove_reaction_message()
{
$textMessage = $this->whatsapp_app_cloud_api->sendTextMessage(
WhatsAppCloudApiTestConfiguration::$to_phone_number_id,
'This text will receive a reaction and then the reaction will be removed',
true
);

$messageId = $textMessage->decodedBody()['messages'][0]['id'];

$reactToMessage = $this->whatsapp_app_cloud_api->sendReaction(
WhatsAppCloudApiTestConfiguration::$to_phone_number_id,
$messageId,
'👍'
);

// sleep(3); // can delay next request to see reaction

$response = $this->whatsapp_app_cloud_api->sendReaction(
WhatsAppCloudApiTestConfiguration::$to_phone_number_id,
$messageId
);

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

public function test_upload_media()
{
$response = $this->whatsapp_app_cloud_api->uploadMedia('tests/Support/netflie.png');
Expand Down
75 changes: 75 additions & 0 deletions tests/Unit/WhatsAppCloudApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,81 @@ public function test_mark_a_message_as_read()
$this->assertEquals(false, $response->isError());
}

public function test_send_reaction_message()
{
$to = $this->faker->phoneNumber;
$url = $this->buildMessageRequestUri();
$emoji = $this->faker->emoji;
$message_id = $this->faker->uuid;

$body = [
'messaging_product' => 'whatsapp',
'recipient_type' => 'individual',
'to' => $to,
'type' => 'reaction',
'reaction' => [
'message_id' => $message_id,
'emoji' => $emoji,
],
];
$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->sendReaction(
$to,
$message_id,
$emoji
);

$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_send_remove_reaction_message()
{
$to = $this->faker->phoneNumber;
$url = $this->buildMessageRequestUri();
$emoji = '';
$message_id = $this->faker->uuid;

$body = [
'messaging_product' => 'whatsapp',
'recipient_type' => 'individual',
'to' => $to,
'type' => 'reaction',
'reaction' => [
'message_id' => $message_id,
'emoji' => $emoji,
],
];
$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->sendReaction(
$to,
$message_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());
}

private function buildBaseUri(): string
{
return Client::BASE_GRAPH_URL . '/' . static::TEST_GRAPH_VERSION . '/';
Expand Down