Skip to content

Commit 823953e

Browse files
feat: add contacts methods
2 parents 7a8cbc6 + 11912d1 commit 823953e

5 files changed

Lines changed: 126 additions & 0 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ This method will be called when an incoming message is received. Next, process m
124124
| Example of sending a picture by uploading from the disk | [sendPictureByUpload.php](https://github.com/green-api/whatsapp-api-client-php/blob/master/examples/sendPictureByUpload.php) |
125125
| Example of a group creation and sending a message to the group | [createGroupAndSendMessage.php](https://github.com/green-api/whatsapp-api-client-php/blob/master/examples/createGroupAndSendMessage.php) |
126126
| Example of incoming webhooks receiving | [receiveNotification.php](https://github.com/green-api/whatsapp-api-client-php/blob/master/examples/receiveNotification.php) |
127+
| Example if working with contacts | [contactsMethods.php](https://github.com/green-api/whatsapp-api-client-php/blob/master/examples/contactsMethods.php) |
127128

128129
## The full list of the library methods
129130

@@ -138,6 +139,9 @@ This method will be called when an incoming message is received. Next, process m
138139
| `account.qr` | The method is designed to get a QR code | [QR](https://green-api.com/en/docs/api/account/QR/) |
139140
| `account.getAuthorizationCode` | The method is designed to authorize an instance by phone number | [GetAuthorizationCode](https://green-api.com/en/docs/api/account/GetAuthorizationCode/) |
140141
| `account.setProfilePicture` | The method is designed to set the avatar of the account | [SetProfilePicture](https://green-api.com/en/docs/api/account/SetProfilePicture/) |
142+
| `contacts.addContact()` | The method is aimed for adding a number to contacts | [AddContact](https://green-api.com/en/docs/api/contacts/AddContact) |
143+
| `contacts.editContact()` | The method is used to edit a number in contacts | [EditContact](https://green-api.com/en/docs/api/contacts/EditContact) |
144+
| `contacts.deleteContact()` | The method is used to remove a number from contacts | [DeleteContact](https://green-api.com/en/docs/api/contacts/DeleteContact) |
141145
| `groups.createGroup` | The method is designed to create a group chat | [CreateGroup](https://green-api.com/en/docs/api/groups/CreateGroup/) |
142146
| `groups.updateGroupName` | The method changes the name of the group chat | [UpdateGroupName](https://green-api.com/en/docs/api/groups/UpdateGroupName/) |
143147
| `groups.getGroupData` | The method gets group chat data | [GetGroupData](https://green-api.com/en/docs/api/groups/GetGroupData/) |

README_RUS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ body | тело сообщения (json)
122122
| Пример отправки картинки загрузкой с диска | [sendPictureByUpload.php](https://github.com/green-api/whatsapp-api-client-php/blob/master/examples/sendPictureByUpload.php) |
123123
| Пример создание группы и отправка сообщения в группу | [createGroupAndSendMessage.php](https://github.com/green-api/whatsapp-api-client-php/blob/master/examples/createGroupAndSendMessage.php) |
124124
| Пример получения входящих уведомлений | [receiveNotification.php](https://github.com/green-api/whatsapp-api-client-php/blob/master/examples/receiveNotification.php) |
125+
| Как работать с контактами | [contactsMethods.php](https://github.com/green-api/whatsapp-api-client-php/blob/master/examples/contactsMethods.php) |
125126

126127
## Полный список методов библиотеки
127128

@@ -136,6 +137,9 @@ body | тело сообщения (json)
136137
| `account.qr` | Метод предназначен для получения QR-кода | [QR](https://green-api.com/docs/api/account/QR/) |
137138
| `account.setProfilePicture` | Метод предназначен для установки аватара аккаунта | [SetProfilePicture](https://green-api.com/docs/api/account/SetProfilePicture/) |
138139
| `account.getAuthorizationCode` | Метод предназначен для авторизации инстанса по номеру телефона | [GetAuthorizationCode](https://green-api.com/docs/api/account/GetAuthorizationCode/) |
140+
| `contacts.addContact()` | Метод предназначен для добавления контакта | [AddContact](https://green-api.com/docs/api/contacts/AddContact) |
141+
| `contacts.editContact()` | Метод предназначен для изменения контакта | [EditContact](https://green-api.com/docs/api/contacts/EditContact) |
142+
| `contacts.deleteContact()` | Метод предназначен для удаления контакта | [DeleteContact](https://green-api.com/docs/api/contacts/DeleteContact) |
139143
| `groups.createGroup` | Метод предназначен для создания группового чата | [CreateGroup](https://green-api.com/docs/api/groups/CreateGroup/) |
140144
| `groups.updateGroupName` | Метод изменяет наименование группового чата | [UpdateGroupName](https://green-api.com/docs/api/groups/UpdateGroupName/) |
141145
| `groups.getGroupData` | Метод получает данные группового чата | [GetGroupData](https://green-api.com/docs/api/groups/GetGroupData/) |

examples/contactsMethods.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
require './vendor/autoload.php';
3+
4+
use GreenApi\RestApi\GreenApiClient;
5+
6+
define( "ID_INSTANCE", "1101712345" );
7+
define( "API_TOKEN_INSTANCE", "d75b3a66374942c5b3c019c698abc2067e151558acbd412345" );
8+
9+
$greenApi = new GreenApiClient( ID_INSTANCE, API_TOKEN_INSTANCE );
10+
11+
$resultAdd = $greenApi->contacts->addContact('79876543210@c.us', 'John', 'Doe', true);
12+
13+
print_r( $resultAdd->data );
14+
15+
$resultEdit = $greenApi->contacts->editContact('79876543210@c.us', 'John', 'Smith', true);
16+
17+
print_r( $resultEdit->data );
18+
19+
$resultDelete = $greenApi->contacts->deleteContact('79876543210@c.us');
20+
21+
print_r( $resultDelete->data );

src/GreenApiClient.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace GreenApi\RestApi;
1111

1212
use GreenApi\RestApi\tools\Account;
13+
use GreenApi\RestApi\tools\Contacts;
1314
use GreenApi\RestApi\tools\Groups;
1415
use GreenApi\RestApi\tools\Journals;
1516
use GreenApi\RestApi\tools\Marking;
@@ -31,6 +32,10 @@ class GreenApiClient {
3132
* @var Account
3233
*/
3334
public $account;
35+
/**
36+
* @var Contacts
37+
*/
38+
public $contacts;
3439
/**
3540
* @var Sending
3641
*/
@@ -76,6 +81,7 @@ public function __construct( $idInstance, $apiTokenInstance, $host = "https://ap
7681
$this->media = $media;
7782

7883
$this->account = new Account( $this );
84+
$this->contacts = new Contacts( $this );
7985
$this->groups = new Groups( $this );
8086
$this->journals = new Journals( $this );
8187
$this->marking = new Marking( $this );

src/tools/Contacts.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace GreenApi\RestApi\tools;
4+
5+
use GreenApi\RestApi\GreenApiClient;
6+
use stdClass;
7+
8+
class Contacts
9+
{
10+
/**
11+
* @param GreenApiClient $greenApi
12+
*/
13+
private $greenApi;
14+
15+
public function __construct(GreenApiClient $greenApi)
16+
{
17+
$this->greenApi = $greenApi;
18+
}
19+
20+
/**
21+
* The method adds a contact to the user's contact list.
22+
*
23+
* @param string $chatId
24+
* @param string $firstName
25+
* @param string|null $lastName
26+
* @param bool $saveInAddressbook
27+
*
28+
* @return stdClass
29+
*
30+
* @link https://green-api.com/en/docs/api/contacts/AddContact/
31+
*/
32+
public function addContact(string $chatId, string $firstName, string $lastName = null, bool $saveInAddressbook = true): stdClass
33+
{
34+
$requestBody = [
35+
'chatId' => $chatId,
36+
'firstName' => $firstName,
37+
'lastName' => $lastName,
38+
'saveInAddressbook' => $saveInAddressbook,
39+
];
40+
41+
return $this->greenApi->request(
42+
'POST', '{{host}}/waInstance{{idInstance}}/addContact/{{apiTokenInstance}}', $requestBody
43+
);
44+
}
45+
46+
/**
47+
* The method edits the contact in the user's contact list.
48+
*
49+
* @param string $chatId
50+
* @param string $firstName
51+
* @param string|null $lastName
52+
* @param bool $saveInAddressbook
53+
*
54+
* @return stdClass
55+
*
56+
* @link https://green-api.com/en/docs/api/contacts/EditContact/
57+
*/
58+
public function editContact(string $chatId, string $firstName, string $lastName = null, bool $saveInAddressbook = true): stdClass
59+
{
60+
$requestBody = [
61+
'chatId' => $chatId,
62+
'firstName' => $firstName,
63+
'lastName' => $lastName,
64+
'saveInAddressbook' => $saveInAddressbook,
65+
];
66+
67+
return $this->greenApi->request(
68+
'POST', '{{host}}/waInstance{{idInstance}}/editContact/{{apiTokenInstance}}', $requestBody
69+
);
70+
}
71+
72+
/**
73+
* The method deletes the contact from the user's contact list.
74+
*
75+
* @param string $chatId
76+
*
77+
* @return stdClass
78+
*
79+
* @link https://green-api.com/en/docs/api/contacts/DeleteContact/
80+
*/
81+
public function deleteContact(string $chatId): stdClass
82+
{
83+
$requestBody = [
84+
'chatId' => $chatId,
85+
];
86+
87+
return $this->greenApi->request(
88+
'POST', '{{host}}/waInstance{{idInstance}}/deleteContact/{{apiTokenInstance}}', $requestBody
89+
);
90+
}
91+
}

0 commit comments

Comments
 (0)