diff --git a/README.md b/README.md index d9a28d1..b71f29a 100644 --- a/README.md +++ b/README.md @@ -505,6 +505,35 @@ $whatsapp_cloud_api->updateBusinessProfile([ Fields list: https://developers.facebook.com/docs/whatsapp/cloud-api/reference/business-profiles +### Get Conversational Component Settings +```php +conversationalComponents(); + +``` + +### Update Conversational Component Settings +```php +updateConversationalComponents([ + 'enable_welcome_message' => false, // true/false + 'commands' => [ + [ + 'command_name' => 'tickets', + 'command_description' => 'Book flight tickets', + ], + [ + 'command_name' => 'hotel', + 'command_description' => 'Book hotel', + ], + ], + 'prompts' => ['Book a flight','plan a vacation'], +]); +``` +Note: All existing Conversational Component settings will be overwritten with the new update and excluded fields will be removed. + ## Features - Send Text Messages @@ -526,6 +555,7 @@ Fields list: https://developers.facebook.com/docs/whatsapp/cloud-api/reference/b - Mark messages as read - React to a Message - Get/Update Business Profile +- Get/Update Conversational Components - Webhook verification - Webhook notifications @@ -545,7 +575,7 @@ Please see [CHANGELOG](https://github.com/netflie/whatsapp-cloud-api/blob/main/C ```php composer unit-test ``` -You also can run tests making real calls to the WhastApp Clou API. Please put your testing credentials on **WhatsAppCloudApiTestConfiguration** file. +You also can run tests making real calls to the WhastApp Cloud API. Please put your testing credentials on **WhatsAppCloudApiTestConfiguration** file. ```php composer integration-test ``` diff --git a/src/Client.php b/src/Client.php index 05c2a96..d681570 100644 --- a/src/Client.php +++ b/src/Client.php @@ -177,6 +177,55 @@ public function updateBusinessProfile(Request\BusinessProfileRequest\UpdateBusin return $return_response; } + /** + * Get the Conversational Components. + * + * @return Response Raw response from the server. + * + * @throws Netflie\WhatsAppCloudApi\Response\ResponseException + */ + public function conversationalComponents(Request\ConversationalComponentRequest\ConversationalComponentRequest $request): Response + { + $raw_response = $this->handler->get( + $this->buildRequestUri($request->nodePath()), + $request->headers(), + $request->timeout() + ); + + $return_response = Response::fromClientResponse($request, $raw_response); + + if ($return_response->isError()) { + $return_response->throwException(); + } + + return $return_response; + } + + /** + * Update the Conversational Component settings. + * + * @return Response Raw response from the server. + * + * @throws Netflie\WhatsAppCloudApi\Response\ResponseException + */ + public function updateConversationalComponents(Request\ConversationalComponentRequest\UpdateConversationalComponentRequest $request): Response + { + $raw_response = $this->handler->postJsonData( + $this->buildRequestUri($request->nodePath()), + $request->body(), + $request->headers(), + $request->timeout() + ); + + $return_response = Response::fromClientResponse($request, $raw_response); + + if ($return_response->isError()) { + $return_response->throwException(); + } + + return $return_response; + } + private function defaultHandler(): ClientHandler { return new GuzzleClientHandler(); diff --git a/src/Request/ConversationalComponentRequest/ConversationalComponentRequest.php b/src/Request/ConversationalComponentRequest/ConversationalComponentRequest.php new file mode 100644 index 0000000..0c665e7 --- /dev/null +++ b/src/Request/ConversationalComponentRequest/ConversationalComponentRequest.php @@ -0,0 +1,43 @@ +fields = $fields; + $this->from_phone_number_id = $from_phone_number_id; + + parent::__construct($access_token, $timeout); + } + + /** + * Return WhatsApp Number Id for this request. + * + * @return string + */ + public function fromPhoneNumberId(): string + { + return $this->from_phone_number_id; + } + + /** + * WhatsApp node path. + * + * @return string + */ + public function nodePath(): string + { + return $this->from_phone_number_id . '?fields=' . $this->fields; + } +} diff --git a/src/Request/ConversationalComponentRequest/UpdateConversationalComponentRequest.php b/src/Request/ConversationalComponentRequest/UpdateConversationalComponentRequest.php new file mode 100644 index 0000000..f3a4ea7 --- /dev/null +++ b/src/Request/ConversationalComponentRequest/UpdateConversationalComponentRequest.php @@ -0,0 +1,33 @@ +from_phone_number_id = $from_phone_number_id; + $this->components = $components; + parent::__construct($access_token, $timeout); + } + + public function body(): array + { + return array_merge( + [ + 'messaging_product' => 'whatsapp', + ], + $this->components + ); + } + + public function nodePath(): string + { + return $this->from_phone_number_id . '/conversational_automation'; + } +} diff --git a/src/WhatsAppCloudApi.php b/src/WhatsAppCloudApi.php index 9ee9124..ae5bc67 100644 --- a/src/WhatsAppCloudApi.php +++ b/src/WhatsAppCloudApi.php @@ -580,6 +580,46 @@ public function updateBusinessProfile(array $information): Response return $this->client->updateBusinessProfile($request); } + /** + * Get Conversational Component settings. + * + * @return Response + * + * @throws Response\ResponseException + */ + public function conversationalComponents(): Response + { + $request = new Request\ConversationalComponentRequest\ConversationalComponentRequest( + 'conversational_automation', + $this->app->accessToken(), + $this->app->fromPhoneNumberId(), + $this->timeout + ); + + return $this->client->conversationalComponents($request); + } + + /** + * Update Conversational Component settings. + * + * @param array $components The conversational components to be updated. + * + * @return Response + * + * @throws Response\ResponseException + */ + public function updateConversationalComponents(array $components): Response + { + $request = new Request\ConversationalComponentRequest\UpdateConversationalComponentRequest( + $components, + $this->app->accessToken(), + $this->app->fromPhoneNumberId(), + $this->timeout + ); + + return $this->client->updateConversationalComponents($request); + } + /** * Returns the Facebook Whatsapp Access Token. * diff --git a/tests/Integration/ClientTest.php b/tests/Integration/ClientTest.php index 5b405e3..8dbcccd 100644 --- a/tests/Integration/ClientTest.php +++ b/tests/Integration/ClientTest.php @@ -108,4 +108,47 @@ public function test_update_business_profile() $this->assertEquals($request, $response->request()); $this->assertEquals(false, $response->isError()); } + + public function test_conversational_components() + { + $request = new Request\ConversationalComponentRequest\ConversationalComponentRequest( + 'conversational_automation', + WhatsAppCloudApiTestConfiguration::$access_token, + WhatsAppCloudApiTestConfiguration::$from_phone_number_id + ); + + $response = $this->client->conversationalComponents($request); + + $this->assertEquals(200, $response->httpStatusCode()); + $this->assertEquals($request, $response->request()); + $this->assertEquals(false, $response->isError()); + } + + public function test_update_conversational_components() + { + $request = new Request\ConversationalComponentRequest\UpdateConversationalComponentRequest( + [ + 'enable_welcome_message' => false, + 'commands' => [ + [ + 'command_name' => 'tickets', + 'command_description' => 'Book flight tickets', + ], + [ + 'command_name' => 'hotel', + 'command_description' => 'Book hotel', + ], + ], + 'prompts' => ['Book a flight','plan a vacation'], + ], + WhatsAppCloudApiTestConfiguration::$access_token, + WhatsAppCloudApiTestConfiguration::$from_phone_number_id + ); + + $response = $this->client->updateConversationalComponents($request); + + $this->assertEquals(200, $response->httpStatusCode()); + $this->assertEquals($request, $response->request()); + $this->assertEquals(false, $response->isError()); + } }