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 Location Request #192

Merged
merged 1 commit into from
Jul 2, 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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@ $whatsapp_cloud_api->sendSticker('<destination-phone-number>', $media_id);
$whatsapp_cloud_api->sendLocation('<destination-phone-number>', $longitude, $latitude, $name, $address);
```

### Send a location request message

```php
<?php

$body = 'Let\'s start with your pickup. You can either manually *enter an address* or *share your current location*.';
$whatsapp_cloud_api->sendLocationRequest('<destination-phone-number>', $body);
```

### Send a contact message

```php
Expand Down Expand Up @@ -407,6 +416,7 @@ Fields list: https://developers.facebook.com/docs/whatsapp/cloud-api/reference/b
- Send Videos
- Send Stickers
- Send Locations
- Send Location Request
- Send Contacts
- Send Lists
- Send Buttons
Expand Down
28 changes: 28 additions & 0 deletions src/Message/LocationRequestMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Netflie\WhatsAppCloudApi\Message;

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

private string $body;

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

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

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

namespace Netflie\WhatsAppCloudApi\Request\MessageRequest;

use Netflie\WhatsAppCloudApi\Request\MessageRequest;

final class RequestLocationRequestMessage 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(),
'body' => ['text' => $this->message->body()],
'action' => [
'name' => 'send_location',
],
],
];

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

return $body;
}
}
23 changes: 23 additions & 0 deletions src/WhatsAppCloudApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,29 @@ public function sendLocation(string $to, float $longitude, float $latitude, stri
return $this->client->sendMessage($request);
}

/**
* Sends a location request message.
*
* @param string $to The WhatsApp ID or phone number for the person you want to send the message to.
* @param string $body The body of the location request message.
*
* @return Response The response object containing the result of the API request.
*
* @throws Response\ResponseException If there's an error with the API request.
*/
public function sendLocationRequest(string $to, string $body)
{
$message = new Message\LocationRequestMessage($to, $body, $this->reply_to);
$request = new Request\MessageRequest\RequestLocationRequestMessage(
$message,
$this->app->accessToken(),
$this->app->fromPhoneNumberId(),
$this->timeout
);

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

/**
* Sends a contact
*
Expand Down
12 changes: 12 additions & 0 deletions tests/Integration/WhatsAppCloudApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,18 @@ public function test_send_location()
$this->assertEquals(false, $response->isError());
}

public function test_send_location_request()
{
$body = 'Let\'s start with your pickup. You can either manually *enter an address* or *share your current location*.';
$response = $this->whatsapp_app_cloud_api->sendLocationRequest(
WhatsAppCloudApiTestConfiguration::$to_phone_number_id,
$body
);

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

public function test_send_contact()
{
$contact_name = new ContactName('Adams', 'Smith');
Expand Down
39 changes: 39 additions & 0 deletions tests/Unit/WhatsAppCloudApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,45 @@ public function test_send_location()
$this->assertEquals(false, $response->isError());
}

public function test_send_location_request()
{
$to = $this->faker->phoneNumber;
$url = $this->buildMessageRequestUri();
$message = $this->faker->text(1024);

$body = [
'messaging_product' => 'whatsapp',
'recipient_type' => 'individual',
'to' => $to,
'type' => 'interactive',
'interactive' => [
'type' => 'location_request_message',
'body' => ['text' => $message],
'action' => [
'name' => 'send_location',
],
],
];
$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->sendLocationRequest(
$to,
$message
);

$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_contact()
{
$to = $this->faker->phoneNumber;
Expand Down