Skip to content

Commit

Permalink
add conversational components
Browse files Browse the repository at this point in the history
  • Loading branch information
derrickobedgiu1 committed Aug 29, 2024
1 parent 820c2bb commit c29c65e
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 1 deletion.
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<?php

$whatsapp_cloud_api->conversationalComponents();

```

### Update Conversational Component Settings
```php
<?php

$whatsapp_cloud_api->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
Expand All @@ -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

Expand All @@ -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
```
Expand Down
49 changes: 49 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Netflie\WhatsAppCloudApi\Request\ConversationalComponentRequest;

use Netflie\WhatsAppCloudApi\Request;

final class ConversationalComponentRequest extends Request
{
private string $fields;

/**
* @var string WhatsApp Number Id from messages will sent.
*/
private string $from_phone_number_id;

public function __construct(string $fields, string $access_token, string $from_phone_number_id, ?int $timeout = null)
{
$this->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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Netflie\WhatsAppCloudApi\Request\ConversationalComponentRequest;

use Netflie\WhatsAppCloudApi\Request;

final class UpdateConversationalComponentRequest extends Request
{
private string $from_phone_number_id;
private array $components;

public function __construct(array $components, string $access_token, string $from_phone_number_id, ?int $timeout = null)
{
$this->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';
}
}
40 changes: 40 additions & 0 deletions src/WhatsAppCloudApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
43 changes: 43 additions & 0 deletions tests/Integration/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit c29c65e

Please sign in to comment.