Skip to content

Commit 3e8aceb

Browse files
feat: add contacts methods
1 parent 1aab5af commit 3e8aceb

8 files changed

Lines changed: 183 additions & 4 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,9 @@ asyncio.run(main())
313313
| `account.qr` | The method is designed to get a QR code | [QR](https://green-api.com/en/docs/api/account/QR/) |
314314
| `account.setProfilePicture` | The method is designed to set the avatar of the account | [SetProfilePicture](https://green-api.com/en/docs/api/account/SetProfilePicture/) |
315315
| `account.getAuthorizationCode` | The method is designed to authorize an instance by phone number | [GetAuthorizationCode](https://green-api.com/en/docs/api/account/GetAuthorizationCode/) |
316+
| `contacts.addContact` | The method is used to add a number to contacts | [addContact](https://green-api.com/en/docs/api/contacts/AddContact/) |
317+
| `contacts.editContact` | The method is used to edit a number in contacts | [editContact](https://green-api.com/en/docs/api/contacts/EditContact/) |
318+
| `contacts.deleteContact` | The method is used to remove a number from contacts | [deleteContact](https://green-api.com/en/docs/api/contacts/DeleteContact/) |
316319
| `device.getDeviceInfo` | The method is designed to get information about the device (phone) on which the WhatsApp Business application is running | [GetDeviceInfo](https://green-api.com/en/docs/api/phone/GetDeviceInfo/) |
317320
| `groups.createGroup` | The method is designed to create a group chat | [CreateGroup](https://green-api.com/en/docs/api/groups/CreateGroup/) |
318321
| `groups.updateGroupName` | The method changes the name of the group chat | [UpdateGroupName](https://green-api.com/en/docs/api/groups/UpdateGroupName/) |

docs/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,9 @@ asyncio.run(main())
314314
| `account.qr` | Метод предназначен для получения QR-кода | [QR](https://green-api.com/docs/api/account/QR/) |
315315
| `account.setProfilePicture` | Метод предназначен для установки аватара аккаунта | [SetProfilePicture](https://green-api.com/docs/api/account/SetProfilePicture/) |
316316
| `account.getAuthorizationCode` | Метод предназначен для авторизации инстанса по номеру телефона | [GetAuthorizationCode](https://green-api.com/docs/api/account/GetAuthorizationCode/) |
317+
| `contacts.addContact` | Метод предназначен для добавления номера в контакты | [addContact](https://green-api.com/docs/api/contacts/AddContact/) |
318+
| `contacts.editContact` | Метод предназначен для редактирования номера в контактах | [editContact](https://green-api.com/docs/api/contacts/EditContact/) |
319+
| `contacts.deleteContact` | Метод предназначен для удаления номера из контактов | [deleteContact](https://green-api.com/docs/api/contacts/DeleteContact/) |
317320
| `device.getDeviceInfo` | Метод предназначен для получения информации об устройстве (телефоне), на котором запущено приложение WhatsApp Business | [GetDeviceInfo](https://green-api.com/docs/api/phone/GetDeviceInfo/) |
318321
| `groups.createGroup` | Метод предназначен для создания группового чата | [CreateGroup](https://green-api.com/docs/api/groups/CreateGroup/) |
319322
| `groups.updateGroupName` | Метод изменяет наименование группового чата | [UpdateGroupName](https://green-api.com/docs/api/groups/UpdateGroupName/) |

examples/async/contactsMethods.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import asyncio
2+
from whatsapp_api_client_python import API
3+
4+
greenAPI = API.GreenAPI(
5+
"1101000001", "d75b3a66374942c5b3c019c698abc2067e151558acbd412345"
6+
)
7+
8+
async def main():
9+
tasks = [
10+
greenAPI.contacts.addContactAsync("79876543210@c.us", "Bruce", "Wayne", True),
11+
greenAPI.contacts.editContactAsync("79876543210@c.us", "Batman", "", True),
12+
greenAPI.contacts.deleteContactAsync("79876543210@c.us")
13+
]
14+
responses = await asyncio.gather(*tasks, return_exceptions=True)
15+
[print(response.data) for response in responses if response.code == 200]
16+
17+
if __name__ == '__main__':
18+
asyncio.run(main())

examples/sync/contactsMethods.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from whatsapp_api_client_python import API
2+
3+
greenAPI = API.GreenAPI(
4+
"1101000001", "d75b3a66374942c5b3c019c698abc2067e151558acbd412345"
5+
)
6+
7+
8+
def main():
9+
# Add contact
10+
addContactResponse = greenAPI.contacts.addContact("79876543210@c.us", "Bruce", "Wayne", True)
11+
print(addContactResponse.data)
12+
# Edit contact
13+
editContactResponse = greenAPI.contacts.editContact("79876543210@c.us", "Batman", "", True)
14+
print(editContactResponse.data)
15+
16+
# Delete contact
17+
deleteContactResponse = greenAPI.contacts.deleteContact("79876543210@c.us")
18+
print(deleteContactResponse.data)
19+
20+
if __name__ == '__main__':
21+
main()

setup.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name="whatsapp-api-client-python",
8-
version="0.0.53",
8+
version="0.0.54",
99
description=(
1010
"This library helps you easily create"
1111
" a Python application with WhatsApp API."
@@ -27,8 +27,6 @@
2727
"Programming Language :: Python",
2828
"Programming Language :: Python :: 3",
2929
"Programming Language :: Python :: 3 :: Only",
30-
"Programming Language :: Python :: 3.7",
31-
"Programming Language :: Python :: 3.8",
3230
"Programming Language :: Python :: 3.9",
3331
"Programming Language :: Python :: 3.10",
3432
"Programming Language :: Python :: 3.11",
@@ -44,5 +42,5 @@
4442
" (CC BY-ND 4.0)"
4543
),
4644
install_requires=["requests>=2.31.0", "aiofiles>=24.1.0", "aiogram>=3.21.0", "aiohttp>=3.9.0"],
47-
python_requires=">=3.7"
45+
python_requires=">=3.9"
4846
)

tests/test_methods.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ def account_methods(self) -> typing.List[Response]:
5050
api.account.setProfilePicture(path),
5151
api.account.getAuthorizationCode(0)
5252
]
53+
54+
@property
55+
def contacts_methods(self) -> typing.List[Response]:
56+
return [
57+
api.contacts.addContact("", "", "", False),
58+
api.contacts.editContact("", "", "", False),
59+
api.contacts.deleteContact("")
60+
]
5361

5462
@property
5563
def device_methods(self) -> typing.List[Response]:

whatsapp_api_client_python/API.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from .response import Response as GreenAPIResponse
1010
from .tools import (
1111
account,
12+
contacts,
1213
device,
1314
groups,
1415
journals,
@@ -56,6 +57,7 @@ def __init__(
5657
self.__prepare_session()
5758

5859
self.account = account.Account(self)
60+
self.contacts = contacts.Contacts(self)
5961
self.device = device.Device(self)
6062
self.groups = groups.Groups(self)
6163
self.journals = journals.Journals(self)
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
from typing import Optional, TYPE_CHECKING
2+
3+
from ..response import Response
4+
5+
if TYPE_CHECKING:
6+
from ..API import GreenApi
7+
8+
class Contacts:
9+
def __init__(self, api: "GreenApi"):
10+
self.api = api
11+
12+
def addContact(
13+
self,
14+
chatId: str,
15+
firstName: str,
16+
lastName: Optional[str] = None,
17+
saveInAddressbook: Optional[bool] = True
18+
) -> Response:
19+
"""
20+
The method adds a contact to the user's contact list.
21+
22+
https://green-api.com/en/docs/api/contacts/AddContact/
23+
"""
24+
25+
request_body = self.__handle_parameters(locals())
26+
27+
return self.api.request(
28+
"POST", (
29+
"{{host}}/waInstance{{idInstance}}/"
30+
"addContact/{{apiTokenInstance}}"
31+
), request_body
32+
)
33+
34+
async def addContactAsync(
35+
self,
36+
chatId: str,
37+
firstName: str,
38+
lastName: Optional[str] = None,
39+
saveInAddressbook: Optional[bool] = True
40+
) -> Response:
41+
request_body = self.__handle_parameters(locals())
42+
43+
return await self.api.requestAsync(
44+
"POST",
45+
"{{host}}/waInstance{{idInstance}}/addContact/{{apiTokenInstance}}",
46+
request_body
47+
)
48+
49+
def editContact(
50+
self,
51+
chatId: str,
52+
firstName: str,
53+
lastName: Optional[str] = None,
54+
saveInAddressbook: Optional[bool] = True
55+
) -> Response:
56+
"""
57+
The method edits a contact in the user's contact list.
58+
59+
https://green-api.com/en/docs/api/contacts/EditContact/
60+
"""
61+
62+
request_body = self.__handle_parameters(locals())
63+
64+
return self.api.request(
65+
"POST", (
66+
"{{host}}/waInstance{{idInstance}}/"
67+
"editContact/{{apiTokenInstance}}"
68+
), request_body
69+
)
70+
71+
async def editContactAsync(
72+
self,
73+
chatId: str,
74+
firstName: str,
75+
lastName: Optional[str] = None,
76+
saveInAddressbook: Optional[bool] = True
77+
) -> Response:
78+
request_body = self.__handle_parameters(locals())
79+
80+
return await self.api.requestAsync(
81+
"POST",
82+
"{{host}}/waInstance{{idInstance}}/editContact/{{apiTokenInstance}}",
83+
request_body
84+
)
85+
86+
def deleteContact(
87+
self,
88+
chatId: str
89+
) -> Response:
90+
"""
91+
The method deletes a contact from the user's contact list.
92+
93+
https://green-api.com/en/docs/api/contacts/DeleteContact/
94+
"""
95+
96+
request_body = self.__handle_parameters(locals())
97+
98+
return self.api.request(
99+
"POST", (
100+
"{{host}}/waInstance{{idInstance}}/"
101+
"deleteContact/{{apiTokenInstance}}"
102+
), request_body
103+
)
104+
105+
async def deleteContactAsync(
106+
self,
107+
chatId: str
108+
) -> Response:
109+
request_body = self.__handle_parameters(locals())
110+
111+
return await self.api.requestAsync(
112+
"POST",
113+
"{{host}}/waInstance{{idInstance}}/deleteContact/{{apiTokenInstance}}",
114+
request_body
115+
)
116+
117+
@classmethod
118+
def __handle_parameters(cls, parameters: dict) -> dict:
119+
handled_parameters = parameters.copy()
120+
handled_parameters.pop("self")
121+
122+
for key, value in parameters.items():
123+
if value is None:
124+
handled_parameters.pop(key)
125+
126+
return handled_parameters

0 commit comments

Comments
 (0)