Skip to content

Commit 6acc195

Browse files
feat: added partner methods
1 parent 823953e commit 6acc195

4 files changed

Lines changed: 101 additions & 4 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ This method will be called when an incoming message is received. Next, process m
187187
| `statuses.getIncomingStatuses` | The method returns the incoming status messages of the instance | [GetIncomingStatuses](https://green-api.com/en/docs/api/statuses/GetIncomingStatuses/) |
188188
| `statuses.getOutgoingStatuses` | The method returns the outgoing statuses of the account | [GetOutgoingStatuses](https://green-api.com/en/docs/api/statuses/GetOutgoingStatuses/) |
189189
| `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/) |
190+
| `partner.getInstances` | The method is designed to get the instances of the account | [GetInstances](https://green-api.com/en/docs/partners/getInstances/) |
191+
| `partner.createInstance` | The method is designed to create the instance in the account | [CreateInstance](https://green-api.com/en/docs/partners/createInstance/) |
192+
| `partner.deleteInstanceAccount` | The method is designed to remove the instance in the account | [DeleteInstanceAccount](https://green-api.com/en/docs/partners/deleteInstanceAccount/) |
190193

191194
## Service methods documentation
192195

README_RUS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ body | тело сообщения (json)
185185
| `statuses.getIncomingStatuses` | Метод возвращает крайние входящие статусы аккаунта | [GetIncomingStatuses](https://green-api.com/docs/api/statuses/GetIncomingStatuses/) |
186186
| `statuses.getOutgoingStatuses` | Метод возвращает крайние отправленные статусы аккаунта | [GetOutgoingStatuses](https://green-api.com/docs/api/statuses/GetOutgoingStatuses/) |
187187
| `statuses.getStatusStatistic` | Метод возвращает массив получателей со статусами, отмеченных как отправлено/доставлено/прочитано, для данного статуса | [GetStatusStatistic](https://green-api.com/docs/api/statuses/GetStatusStatistic/) |
188+
| `partner.getInstances` | Метод предназначен для получения инстансов аккаунта | [GetInstances](https://green-api.com/en/docs/partners/getInstances/) |
189+
| `partner.createInstance` | Метод предназначен для создания инстанса в аккаунте | [CreateInstance](https://green-api.com/en/docs/partners/createInstance/) |
190+
| `partner.deleteInstanceAccount` | Метод предназначен для удаления инстанса в аккаунте | [DeleteInstanceAccount](https://green-api.com/en/docs/partners/deleteInstanceAccount/) |
188191

189192
## Документация по методам сервиса
190193

src/GreenApiClient.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@
2020
use GreenApi\RestApi\tools\ServiceMethods;
2121
use GreenApi\RestApi\tools\Webhooks;
2222
use GreenApi\RestApi\tools\Statuses;
23+
use GreenApi\RestApi\tools\Partner;
2324
use stdClass;
2425

2526
class GreenApiClient {
2627
private $host;
2728
private $media;
28-
private $idInstance;
29-
private $apiTokenInstance;
29+
private $idInstance; = null;
30+
private $apiTokenInstance; = null;
3031

3132
/**
3233
* @var Account
@@ -72,14 +73,24 @@ class GreenApiClient {
7273
* @var Statuses
7374
*/
7475
public $statuses;
76+
/**
77+
* @var Partner|null
78+
*/
79+
public $partner;
7580

76-
public function __construct( $idInstance, $apiTokenInstance, $host = "https://api.green-api.com", $media = "https://media.green-api.com" ) {
81+
public function __construct( $idInstance = null, $apiTokenInstance = null, $partnerToken = null, $host = "https://api.green-api.com", $media = "https://media.green-api.com" ) {
7782

7883
$this->idInstance = $idInstance;
7984
$this->apiTokenInstance = $apiTokenInstance;
85+
$this->partnerToken = $partnerToken;
86+
8087
$this->host = $host;
8188
$this->media = $media;
8289

90+
if ($this->partnerToken) {
91+
$this->partner = new Partner( $this );
92+
}
93+
8394
$this->account = new Account( $this );
8495
$this->contacts = new Contacts( $this );
8596
$this->groups = new Groups( $this );
@@ -110,7 +121,7 @@ public function request( string $method, string $url, array $payload = null, boo
110121
$url = str_replace( '{{media}}', $this->media, $url );
111122
$url = str_replace( '{{idInstance}}', $this->idInstance, $url );
112123
$url = str_replace( '{{apiTokenInstance}}', $this->apiTokenInstance, $url );
113-
124+
$url = str_replace( '{{partnerToken}}', $this->partnerToken, $url );
114125
$method = strtoupper( $method );
115126
$curl = curl_init();
116127

src/tools/Partner.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace GreenApi\RestApi\tools;
4+
5+
use GreenApi\RestApi\GreenApiClient;
6+
use stdClass;
7+
8+
class Partner {
9+
10+
/**
11+
* @param GreenApiClient $greenApi
12+
*/
13+
private $greenApi;
14+
15+
/**
16+
* @param GreenApiClient $greenApi
17+
*/
18+
public function __construct(GreenApiClient $greenApi) {
19+
$this->greenApi = $greenApi;
20+
}
21+
22+
/**
23+
* The method is used to getting all the account instances created by the partner.
24+
*
25+
* @return stdClass
26+
* @link https://green-api.com/en/docs/partners/getInstances/
27+
*/
28+
public function getInstances(): stdClass {
29+
return $this->greenApi->request('GET',
30+
'{{host}}/partner/getInstances/{{partnerToken}}');
31+
}
32+
33+
/**
34+
* The method is used to creating a messenger account instance on the partner's part
35+
*
36+
* @param $payload
37+
* @return stdClass
38+
* @link https://green-api.com/en/docs/partners/createInstance/
39+
*/
40+
public function createInstance($payload): stdClass {
41+
$defaultParameters = [
42+
'name' => null,
43+
'webhookUrl' => null,
44+
'webhookUrlToken' => null,
45+
'delaySendMessagesMilliseconds' => null,
46+
'markIncomingMessagesReaded' => null,
47+
'markIncomingMessagesReadedOnReply' => null,
48+
'outgoingWebhook' => null,
49+
'outgoingMessageWebhook' => null,
50+
'outgoingAPIMessageWebhook' => null,
51+
'stateWebhook' => null,
52+
'incomingWebhook' => 'yes',
53+
'deviceWebhook' => null,
54+
'keepOnlineStatus' => null,
55+
'pollMessageWebhook' => null,
56+
'incomingBlockWebhook' => null,
57+
'incomingCallWebhook' => null,
58+
'editedMessageWebhook' => null,
59+
'deletedMessageWebhook' => null,
60+
];
61+
62+
$requestParameters = array_filter(array_merge($defaultParameters, $payload));
63+
64+
return $this->greenApi->request('POST',
65+
'{{host}}/partner/createInstance/{{partnerToken}}', $requestParameters);
66+
}
67+
68+
/**
69+
*
70+
* The method is used to deleting an instance of the partner's account.
71+
*
72+
* @param $idInstance
73+
* @return stdClass
74+
* @link https://green-api.com/en/docs/partners/deleteInstanceAccount/
75+
*/
76+
public function deleteInstanceAccount($idInstance): stdClass {
77+
return $this->greenApi->request('POST',
78+
'{{host}}/partner/deleteInstanceAccount/{{partnerToken}}', ['idInstance' => $idInstance]);
79+
}
80+
}

0 commit comments

Comments
 (0)