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 "Update Webhook Endpoint" #216

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 25 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,31 @@ public function updateBusinessProfile(Request\BusinessProfileRequest\UpdateBusin
return $return_response;
}

/**
* Update the webhook configuration (phone number override).
*
* @return Response Raw response from the server.
*
* @throws Netflie\WhatsAppCloudApi\Response\ResponseException
*/
public function updateWebhook(Request\WebhookRequest\UpdateWebhookRequest $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
63 changes: 63 additions & 0 deletions src/Request/WebhookRequest/UpdateWebhookRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Netflie\WhatsAppCloudApi\Request\WebhookRequest;

use Netflie\WhatsAppCloudApi\Request;
use Netflie\WhatsAppCloudApi\Request\RequestWithBody;

final class UpdateWebhookRequest extends Request implements RequestWithBody
{
/**
* @var string WhatsApp webhook uri.
*/
private string $uri;

/**
* @var ?string WhatsApp webhook verify token, if provided.
*/
private ?string $verify_token;

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

public function __construct(array $uri, ?string $verify_token, string $access_token, string $from_phone_number_id, ?int $timeout = null)
HighLiuk marked this conversation as resolved.
Show resolved Hide resolved
{
$this->uri = $uri;
$this->verify_token = $verify_token;
$this->from_phone_number_id = $from_phone_number_id;

parent::__construct($access_token, $timeout);
}

/**
* Returns the raw form of the request.
*
* @return array
*/
public function body(): array
{
$body = [
'webhook_configuration' => [
'override_callback_uri' => $this->uri,
],
];

if ($this->verify_token) {
$body['webhook_configuration']['verify_token'] = $this->verify_token;
}

return $body;
}

/**
* WhatsApp node path.
*
* @return string
*/
public function nodePath(): string
{
return $this->from_phone_number_id;
}
}
22 changes: 22 additions & 0 deletions src/WhatsAppCloudApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,28 @@ public function updateBusinessProfile(array $information): Response
return $this->client->updateBusinessProfile($request);
}

/**
* Update webhook configuration (phone number override)
*
* @param array $uri Whatsapp webhook uri.
*
* @return Response
*
* @throws Response\ResponseException
*/
public function updateWebhook(string $uri, ?string $verify_token = null): Response
HighLiuk marked this conversation as resolved.
Show resolved Hide resolved
{
$request = new Request\WebhookRequest\UpdateWebhookRequest(
$uri,
$verify_token,
$this->app->accessToken(),
$this->app->fromPhoneNumberId(),
$this->timeout
);

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

/**
* Returns the Facebook Whatsapp Access Token.
*
Expand Down