Skip to content

Commit

Permalink
Add react to message
Browse files Browse the repository at this point in the history
  • Loading branch information
derrickobedgiu1 committed Apr 23, 2024
1 parent 52bf370 commit 0424115
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 0 deletions.
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
31 changes: 31 additions & 0 deletions tests/Integration/WhatsAppCloudApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,37 @@ public function test_send_reply_buttons()
$this->assertEquals(false, $response->isError());
}

public function test_send_reaction_message()
{
$this->markTestIncomplete(
'This test should use a real message ID.'
);

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

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

public function test_send_remove_reaction_message()
{
$this->markTestIncomplete(
'This test should use a real message ID.'
);

$response = $this->whatsapp_app_cloud_api->sendReaction(
WhatsAppCloudApiTestConfiguration::$to_phone_number_id,
'wamid.HBgMMjU2NzQyMDMwNDAzFQIAERgSMEU2MkE3Q0I3RTEyRDU5NzIwAA=='
);

$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

0 comments on commit 0424115

Please sign in to comment.