From 3afa850515ba4c6f9b5fb9d414c56745a082b7f4 Mon Sep 17 00:00:00 2001 From: yuri-kovalev Date: Thu, 8 Jan 2026 04:20:52 +0700 Subject: [PATCH 1/3] Added support of partner API --- README.md | 5 ++- README_RUS.md | 5 ++- src/GreenApiClient.php | 69 +++++++++++++++++++++--------------- src/tools/Partner.php | 79 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 128 insertions(+), 30 deletions(-) create mode 100644 src/tools/Partner.php diff --git a/README.md b/README.md index 00c90eb..05d0f8c 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ php -S localhost:8080 ### How to initialize an object ``` -$greenApi = new GreenApiClient( ID_INSTANCE, API_TOKEN_INSTANCE ); +$greenApi = new GreenApiClient( idInstance: ID_INSTANCE, apiTokenInstance: API_TOKEN_INSTANCE ); ``` ### Sending a text message to a WhatsApp number @@ -183,6 +183,9 @@ This method will be called when an incoming message is received. Next, process m | `statuses.getIncomingStatuses` | The method returns the incoming status messages of the instance | [GetIncomingStatuses](https://green-api.com/en/docs/api/statuses/GetIncomingStatuses/) | | `statuses.getOutgoingStatuses` | The method returns the outgoing statuses of the account | [GetOutgoingStatuses](https://green-api.com/en/docs/api/statuses/GetOutgoingStatuses/) | | `statuses.getStatusStatistic` | The method returns an array of recipients marked sent/delivered/read for a given status | [GetStatusStatistic](https://green-api.com/en/docs/api/statuses/GetStatusStatistic/) | +| `partner.getInstances` | The method is designed to get the instances of the account | [GetInstances](https://green-api.com/en/docs/partners/getInstances/) | +| `partner.createInstance` | The method is designed to create the instance in the account | [CreateInstance](https://green-api.com/en/docs/partners/createInstance/) | +| `partner.deleteInstanceAccount` | The method is designed to remove the instance in the account | [GetStatusStatistic](https://green-api.com/en/docs/partners/deleteInstanceAccount/) | ## Service methods documentation diff --git a/README_RUS.md b/README_RUS.md index 5d787d2..787e36e 100644 --- a/README_RUS.md +++ b/README_RUS.md @@ -36,7 +36,7 @@ php -S localhost:8080 ### Как инициализировать объект ``` -$greenApi = new GreenApiClient( ID_INSTANCE, API_TOKEN_INSTANCE ); +$greenApi = new GreenApiClient( idInstance: ID_INSTANCE, apiTokenInstance: API_TOKEN_INSTANCE ); ``` ### Отправка текстового сообщения на номер WhatsApp @@ -181,6 +181,9 @@ body | тело сообщения (json) | `statuses.getIncomingStatuses` | Метод возвращает крайние входящие статусы аккаунта | [GetIncomingStatuses](https://green-api.com/docs/api/statuses/GetIncomingStatuses/) | | `statuses.getOutgoingStatuses` | Метод возвращает крайние отправленные статусы аккаунта | [GetOutgoingStatuses](https://green-api.com/docs/api/statuses/GetOutgoingStatuses/) | | `statuses.getStatusStatistic` | Метод возвращает массив получателей со статусами, отмеченных как отправлено/доставлено/прочитано, для данного статуса | [GetStatusStatistic](https://green-api.com/docs/api/statuses/GetStatusStatistic/) | +| `partner.getInstances` | Метод предназначен для получения инстансов аккаунта | [GetInstances](https://green-api.com/en/docs/partners/getInstances/) | +| `partner.createInstance` | Метод предназначен для создания инстанса в аккаунте | [CreateInstance](https://green-api.com/en/docs/partners/createInstance/) | +| `partner.deleteInstanceAccount` | Метод предназначен для удаления инстанса в аккаунте | [GetStatusStatistic](https://green-api.com/en/docs/partners/deleteInstanceAccount/) | ## Документация по методам сервиса diff --git a/src/GreenApiClient.php b/src/GreenApiClient.php index 11f41f9..6dfff1c 100644 --- a/src/GreenApiClient.php +++ b/src/GreenApiClient.php @@ -19,72 +19,85 @@ use GreenApi\RestApi\tools\ServiceMethods; use GreenApi\RestApi\tools\Webhooks; use GreenApi\RestApi\tools\Statuses; +use GreenApi\RestApi\tools\Partner; use stdClass; class GreenApiClient { private $host; private $media; - private $idInstance; - private $apiTokenInstance; + private $idInstance = null; + private $apiTokenInstance = null; + private $partnerToken = null; /** - * @var Account + * @var Account|null */ public $account; /** - * @var Sending + * @var Sending|null */ public $sending; /** - * @var Groups + * @var Groups|null */ public $groups; /** - * @var Journals + * @var Journals|null */ public $journals; /** - * @var Marking + * @var Marking|null */ public $marking; /** - * @var Queues + * @var Queues|null */ public $queues; /** - * @var Receiving + * @var Receiving|null */ public $receiving; /** - * @var ServiceMethods + * @var ServiceMethods|null */ public $serviceMethods; /** - * @var Webhooks + * @var Webhooks|null */ public $webhooks; /** - * @var Statuses + * @var Statuses|null */ public $statuses; + /** + * @var Partner|null + */ + public $partner; - public function __construct( $idInstance, $apiTokenInstance, $host = "https://api.green-api.com", $media = "https://media.green-api.com" ) { + public function __construct( $idInstance = null, $apiTokenInstance = null, $partnerToken = null, $host = "https://api.green-api.com", $media = "https://media.green-api.com" ) { - $this->idInstance = $idInstance; - $this->apiTokenInstance = $apiTokenInstance; - $this->host = $host; + $this->idInstance = $idInstance; + $this->apiTokenInstance = $apiTokenInstance; + $this->host = $host; $this->media = $media; - - $this->account = new Account( $this ); - $this->groups = new Groups( $this ); - $this->journals = new Journals( $this ); - $this->marking = new Marking( $this ); - $this->queues = new Queues( $this ); - $this->receiving = new Receiving( $this ); - $this->sending = new Sending( $this ); - $this->serviceMethods = new ServiceMethods( $this ); - $this->webhooks = new Webhooks( $this ); - $this->statuses = new Statuses( $this ); + $this->partnerToken = $partnerToken; + + if ($this->idInstance && $this->apiTokenInstance) { + $this->account = new Account( $this ); + $this->groups = new Groups( $this ); + $this->journals = new Journals( $this ); + $this->marking = new Marking( $this ); + $this->queues = new Queues( $this ); + $this->receiving = new Receiving( $this ); + $this->sending = new Sending( $this ); + $this->serviceMethods = new ServiceMethods( $this ); + $this->webhooks = new Webhooks( $this ); + $this->statuses = new Statuses( $this ); + } + + if ($this->partnerToken) { + $this->partner = new Partner( $this ); + } } /** @@ -144,7 +157,7 @@ public function request( string $method, string $url, array $payload = null, boo break; case 'POST_BINARY': $mime_type = mime_content_type( $path ); - $headers = array( + $headers = array( 'Content-Type: ' . $mime_type , 'GA-Filename: ' . basename($path) ); diff --git a/src/tools/Partner.php b/src/tools/Partner.php new file mode 100644 index 0000000..21f1f44 --- /dev/null +++ b/src/tools/Partner.php @@ -0,0 +1,79 @@ +greenApi = $greenApi; + } + + /** + * The method is used to getting all the account instances created by the partner. + * + * @return stdClass + * @link https://green-api.com/en/docs/partners/getInstances/ + */ + public function getInstances(): stdClass { + return $this->greenApi->request('GET', + '{{host}}/partner/getInstances/{{partnerToken}}'); + } + + /** + * The method is used to creating a messenger account instance on the partner's part + * + * @param $payload + * @return stdClass + * @link https://green-api.com/en/docs/partners/createInstance/ + */ + public function createInstance($payload): stdClass { + $defaultParameters = [ + 'name' => null, + 'webhookUrl' => 'https://app.itkovrochist.ru/api/webhook/green-api/', + 'webhookUrlToken' => null, + 'delaySendMessagesMilliseconds' => null, + 'markIncomingMessagesReaded' => 'yes', + 'markIncomingMessagesReadedOnReply' => null, + 'outgoingWebhook' => null, + 'outgoingMessageWebhook' => null, + 'outgoingAPIMessageWebhook' => null, + 'stateWebhook' => null, + 'incomingWebhook' => 'yes', + 'deviceWebhook' => null, + 'keepOnlineStatus' => null, + 'pollMessageWebhook' => null, + 'incomingBlockWebhook' => null, + 'incomingCallWebhook' => null, + 'editedMessageWebhook' => null, + 'deletedMessageWebhook' => null, + ]; + + $requestParameters = array_filter(array_merge($defaultParameters, $payload)); + + return $this->greenApi->request('GET', + '{{host}}/partner/createInstance/{{partnerToken}}', $requestParameters); + } + + /** + * + * The method is used to deleting an instance of the partner's account. + * + * @return stdClass + * @link https://green-api.com/en/docs/partners/deleteInstanceAccount/ + */ + public function deleteInstanceAccount(): stdClass { + return $this->greenApi->request('GET', + '{{host}}/partner/deleteInstanceAccount/{{partnerToken}}'); + } +} From 2dcb62ec0c95acb2a1dec7040255ab0c0fd2ac1e Mon Sep 17 00:00:00 2001 From: yuri-kovalev Date: Thu, 8 Jan 2026 04:38:04 +0700 Subject: [PATCH 2/3] updated package name for publish fork version as composer package --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 2b50a3a..d6e5522 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "green-api/whatsapp-api-client-php", + "name": "yuri-kovalev/whatsapp-api-client-php", "description": "Green-api.com REST API Client", "keywords": [ "green-api.com", From 6d707bf3e0c931686984e1c857f18819a695b62e Mon Sep 17 00:00:00 2001 From: yuri-kovalev Date: Thu, 8 Jan 2026 14:55:21 +0700 Subject: [PATCH 3/3] cleanup --- src/tools/Partner.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tools/Partner.php b/src/tools/Partner.php index 21f1f44..9511830 100644 --- a/src/tools/Partner.php +++ b/src/tools/Partner.php @@ -40,10 +40,10 @@ public function getInstances(): stdClass { public function createInstance($payload): stdClass { $defaultParameters = [ 'name' => null, - 'webhookUrl' => 'https://app.itkovrochist.ru/api/webhook/green-api/', + 'webhookUrl' => null, 'webhookUrlToken' => null, 'delaySendMessagesMilliseconds' => null, - 'markIncomingMessagesReaded' => 'yes', + 'markIncomingMessagesReaded' => null, 'markIncomingMessagesReadedOnReply' => null, 'outgoingWebhook' => null, 'outgoingMessageWebhook' => null,