Skip to content
Open
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
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());
}
}
69 changes: 68 additions & 1 deletion tests/Unit/WhatsAppCloudApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,7 @@ public function test_upload_media()

public function test_download_media()
{
$media_id = (string) $this->faker->randomNumber;
$media_id = (string)$this->faker->randomNumber;
$url = $this->buildBaseUri() . $media_id;
$headers = [
'Authorization' => 'Bearer ' . $this->access_token,
Expand Down Expand Up @@ -1456,6 +1456,68 @@ public function test_send_remove_reaction_message()
$this->assertEquals(false, $response->isError());
}

public function test_get_conversational_components()
{
$url = $this->buildConversationalComponentRequestUri() . '?fields=conversational_automation';
$headers = [
'Authorization' => 'Bearer ' . $this->access_token,
];

$this->client_handler
->get($url, $headers, Argument::type('int'))
->shouldBeCalled()
->willReturn(new RawResponse($headers, $this->successfulMessageNodeResponse(), 200));

$response = $this->whatsapp_app_cloud_api->conversationalComponents();

$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_update_conversational_components()
{
$url = $this->buildConversationalComponentRequestUri() . '/conversational_automation';
$components = [
'enable_welcome_message' => $this->faker->boolean,
'commands' => [
[
'command_name' => $this->faker->word,
'command_description' => $this->faker->sentence,
],
[
'command_name' => $this->faker->word,
'command_description' => $this->faker->sentence,
],
],
'prompts' => [$this->faker->sentence, $this->faker->sentence],
];

$body = array_merge(
[
'messaging_product' => 'whatsapp',
],
$components
);

$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->updateConversationalComponents($components);

$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 All @@ -1476,6 +1538,11 @@ private function buildBusinessProfileRequestUri(): string
return $this->buildBaseUri() . $this->from_phone_number_id . '/whatsapp_business_profile';
}

private function buildConversationalComponentRequestUri(): string
{
return $this->buildBaseUri() . $this->from_phone_number_id;
}

private function successfulMessageNodeResponse(): string
{
return '{"messaging_product": "whatsapp", "contacts": [{"input": "PHONE_NUMBER", "wa_id": "WHATSAPP_ID"}], "messages": [{"id": "wamid.ID"}]}';
Expand Down
Loading