diff --git a/README.md b/README.md index 9361dd1..6abe99a 100644 --- a/README.md +++ b/README.md @@ -313,6 +313,9 @@ asyncio.run(main()) | `account.qr` | The method is designed to get a QR code | [QR](https://green-api.com/en/docs/api/account/QR/) | | `account.setProfilePicture` | The method is designed to set the avatar of the account | [SetProfilePicture](https://green-api.com/en/docs/api/account/SetProfilePicture/) | | `account.getAuthorizationCode` | The method is designed to authorize an instance by phone number | [GetAuthorizationCode](https://green-api.com/en/docs/api/account/GetAuthorizationCode/) | +| `contacts.addContact` | The method is used to add a number to contacts | [addContact](https://green-api.com/en/docs/api/contacts/AddContact/) | +| `contacts.editContact` | The method is used to edit a number in contacts | [editContact](https://green-api.com/en/docs/api/contacts/EditContact/) | +| `contacts.deleteContact` | The method is used to remove a number from contacts | [deleteContact](https://green-api.com/en/docs/api/contacts/DeleteContact/) | | `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/) | | `groups.createGroup` | The method is designed to create a group chat | [CreateGroup](https://green-api.com/en/docs/api/groups/CreateGroup/) | | `groups.updateGroupName` | The method changes the name of the group chat | [UpdateGroupName](https://green-api.com/en/docs/api/groups/UpdateGroupName/) | diff --git a/docs/README.md b/docs/README.md index 6e9d7b7..06f9330 100644 --- a/docs/README.md +++ b/docs/README.md @@ -314,6 +314,9 @@ asyncio.run(main()) | `account.qr` | Метод предназначен для получения QR-кода | [QR](https://green-api.com/docs/api/account/QR/) | | `account.setProfilePicture` | Метод предназначен для установки аватара аккаунта | [SetProfilePicture](https://green-api.com/docs/api/account/SetProfilePicture/) | | `account.getAuthorizationCode` | Метод предназначен для авторизации инстанса по номеру телефона | [GetAuthorizationCode](https://green-api.com/docs/api/account/GetAuthorizationCode/) | +| `contacts.addContact` | Метод предназначен для добавления номера в контакты | [addContact](https://green-api.com/docs/api/contacts/AddContact/) | +| `contacts.editContact` | Метод предназначен для редактирования номера в контактах | [editContact](https://green-api.com/docs/api/contacts/EditContact/) | +| `contacts.deleteContact` | Метод предназначен для удаления номера из контактов | [deleteContact](https://green-api.com/docs/api/contacts/DeleteContact/) | | `device.getDeviceInfo` | Метод предназначен для получения информации об устройстве (телефоне), на котором запущено приложение WhatsApp Business | [GetDeviceInfo](https://green-api.com/docs/api/phone/GetDeviceInfo/) | | `groups.createGroup` | Метод предназначен для создания группового чата | [CreateGroup](https://green-api.com/docs/api/groups/CreateGroup/) | | `groups.updateGroupName` | Метод изменяет наименование группового чата | [UpdateGroupName](https://green-api.com/docs/api/groups/UpdateGroupName/) | diff --git a/examples/async/contactsMethods.py b/examples/async/contactsMethods.py new file mode 100644 index 0000000..60b2c56 --- /dev/null +++ b/examples/async/contactsMethods.py @@ -0,0 +1,18 @@ +import asyncio +from whatsapp_api_client_python import API + +greenAPI = API.GreenAPI( + "1101000001", "d75b3a66374942c5b3c019c698abc2067e151558acbd412345" +) + +async def main(): + tasks = [ + greenAPI.contacts.addContactAsync("79876543210@c.us", "Bruce", "Wayne", True), + greenAPI.contacts.editContactAsync("79876543210@c.us", "Batman", "", True), + greenAPI.contacts.deleteContactAsync("79876543210@c.us") + ] + responses = await asyncio.gather(*tasks, return_exceptions=True) + [print(response.data) for response in responses if response.code == 200] + +if __name__ == '__main__': + asyncio.run(main()) diff --git a/examples/sync/contactsMethods.py b/examples/sync/contactsMethods.py new file mode 100644 index 0000000..df095e7 --- /dev/null +++ b/examples/sync/contactsMethods.py @@ -0,0 +1,21 @@ +from whatsapp_api_client_python import API + +greenAPI = API.GreenAPI( + "1101000001", "d75b3a66374942c5b3c019c698abc2067e151558acbd412345" +) + + +def main(): + # Add contact + addContactResponse = greenAPI.contacts.addContact("79876543210@c.us", "Bruce", "Wayne", True) + print(addContactResponse.data) + # Edit contact + editContactResponse = greenAPI.contacts.editContact("79876543210@c.us", "Batman", "", True) + print(editContactResponse.data) + + # Delete contact + deleteContactResponse = greenAPI.contacts.deleteContact("79876543210@c.us") + print(deleteContactResponse.data) + +if __name__ == '__main__': + main() diff --git a/setup.py b/setup.py index 7eb849d..04714a7 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setup( name="whatsapp-api-client-python", - version="0.0.53", + version="0.0.54", description=( "This library helps you easily create" " a Python application with WhatsApp API." @@ -27,8 +27,6 @@ "Programming Language :: Python", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3 :: Only", - "Programming Language :: Python :: 3.7", - "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", @@ -44,5 +42,5 @@ " (CC BY-ND 4.0)" ), install_requires=["requests>=2.31.0", "aiofiles>=24.1.0", "aiogram>=3.21.0", "aiohttp>=3.9.0"], - python_requires=">=3.7" + python_requires=">=3.9" ) diff --git a/tests/test_methods.py b/tests/test_methods.py index 0590bfb..47ea19e 100644 --- a/tests/test_methods.py +++ b/tests/test_methods.py @@ -50,6 +50,14 @@ def account_methods(self) -> typing.List[Response]: api.account.setProfilePicture(path), api.account.getAuthorizationCode(0) ] + + @property + def contacts_methods(self) -> typing.List[Response]: + return [ + api.contacts.addContact("", "", "", False), + api.contacts.editContact("", "", "", False), + api.contacts.deleteContact("") + ] @property def device_methods(self) -> typing.List[Response]: diff --git a/whatsapp_api_client_python/API.py b/whatsapp_api_client_python/API.py index f9f1322..78611f2 100644 --- a/whatsapp_api_client_python/API.py +++ b/whatsapp_api_client_python/API.py @@ -9,6 +9,7 @@ from .response import Response as GreenAPIResponse from .tools import ( account, + contacts, device, groups, journals, @@ -56,6 +57,7 @@ def __init__( self.__prepare_session() self.account = account.Account(self) + self.contacts = contacts.Contacts(self) self.device = device.Device(self) self.groups = groups.Groups(self) self.journals = journals.Journals(self) diff --git a/whatsapp_api_client_python/tools/contacts.py b/whatsapp_api_client_python/tools/contacts.py new file mode 100644 index 0000000..412fb1a --- /dev/null +++ b/whatsapp_api_client_python/tools/contacts.py @@ -0,0 +1,126 @@ +from typing import Optional, TYPE_CHECKING + +from ..response import Response + +if TYPE_CHECKING: + from ..API import GreenApi + +class Contacts: + def __init__(self, api: "GreenApi"): + self.api = api + + def addContact( + self, + chatId: str, + firstName: str, + lastName: Optional[str] = None, + saveInAddressbook: Optional[bool] = True + ) -> Response: + """ + The method adds a contact to the user's contact list. + + https://green-api.com/en/docs/api/contacts/AddContact/ + """ + + request_body = self.__handle_parameters(locals()) + + return self.api.request( + "POST", ( + "{{host}}/waInstance{{idInstance}}/" + "addContact/{{apiTokenInstance}}" + ), request_body + ) + + async def addContactAsync( + self, + chatId: str, + firstName: str, + lastName: Optional[str] = None, + saveInAddressbook: Optional[bool] = True + ) -> Response: + request_body = self.__handle_parameters(locals()) + + return await self.api.requestAsync( + "POST", + "{{host}}/waInstance{{idInstance}}/addContact/{{apiTokenInstance}}", + request_body + ) + + def editContact( + self, + chatId: str, + firstName: str, + lastName: Optional[str] = None, + saveInAddressbook: Optional[bool] = True + ) -> Response: + """ + The method edits a contact in the user's contact list. + + https://green-api.com/en/docs/api/contacts/EditContact/ + """ + + request_body = self.__handle_parameters(locals()) + + return self.api.request( + "POST", ( + "{{host}}/waInstance{{idInstance}}/" + "editContact/{{apiTokenInstance}}" + ), request_body + ) + + async def editContactAsync( + self, + chatId: str, + firstName: str, + lastName: Optional[str] = None, + saveInAddressbook: Optional[bool] = True + ) -> Response: + request_body = self.__handle_parameters(locals()) + + return await self.api.requestAsync( + "POST", + "{{host}}/waInstance{{idInstance}}/editContact/{{apiTokenInstance}}", + request_body + ) + + def deleteContact( + self, + chatId: str + ) -> Response: + """ + The method deletes a contact from the user's contact list. + + https://green-api.com/en/docs/api/contacts/DeleteContact/ + """ + + request_body = self.__handle_parameters(locals()) + + return self.api.request( + "POST", ( + "{{host}}/waInstance{{idInstance}}/" + "deleteContact/{{apiTokenInstance}}" + ), request_body + ) + + async def deleteContactAsync( + self, + chatId: str + ) -> Response: + request_body = self.__handle_parameters(locals()) + + return await self.api.requestAsync( + "POST", + "{{host}}/waInstance{{idInstance}}/deleteContact/{{apiTokenInstance}}", + request_body + ) + + @classmethod + def __handle_parameters(cls, parameters: dict) -> dict: + handled_parameters = parameters.copy() + handled_parameters.pop("self") + + for key, value in parameters.items(): + if value is None: + handled_parameters.pop(key) + + return handled_parameters \ No newline at end of file