diff --git a/README.md b/README.md index 9617a5b..fa6cd03 100644 --- a/README.md +++ b/README.md @@ -179,6 +179,15 @@ $whatsapp_cloud_api->sendSticker('', $media_id); $whatsapp_cloud_api->sendLocation('', $longitude, $latitude, $name, $address); ``` +### Send a location request message + +```php +sendLocationRequest('', $body); +``` + ### Send a contact message ```php @@ -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 diff --git a/src/Message/LocationRequestMessage.php b/src/Message/LocationRequestMessage.php new file mode 100644 index 0000000..2e28058 --- /dev/null +++ b/src/Message/LocationRequestMessage.php @@ -0,0 +1,28 @@ +body = $body; + + parent::__construct($to, $reply_to); + } + + public function body(): string + { + return $this->body; + } +} diff --git a/src/Request/MessageRequest/RequestLocationRequestMessage.php b/src/Request/MessageRequest/RequestLocationRequestMessage.php new file mode 100644 index 0000000..4d4e2f5 --- /dev/null +++ b/src/Request/MessageRequest/RequestLocationRequestMessage.php @@ -0,0 +1,34 @@ + $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; + } +} diff --git a/src/WhatsAppCloudApi.php b/src/WhatsAppCloudApi.php index d9eed05..759bf08 100644 --- a/src/WhatsAppCloudApi.php +++ b/src/WhatsAppCloudApi.php @@ -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 * diff --git a/tests/Integration/WhatsAppCloudApiTest.php b/tests/Integration/WhatsAppCloudApiTest.php index e4c89cd..a7a2e23 100644 --- a/tests/Integration/WhatsAppCloudApiTest.php +++ b/tests/Integration/WhatsAppCloudApiTest.php @@ -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'); diff --git a/tests/Unit/WhatsAppCloudApiTest.php b/tests/Unit/WhatsAppCloudApiTest.php index b391dbb..a4a4c68 100644 --- a/tests/Unit/WhatsAppCloudApiTest.php +++ b/tests/Unit/WhatsAppCloudApiTest.php @@ -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;