Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/) |
Expand Down
3 changes: 3 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/) |
Expand Down
18 changes: 18 additions & 0 deletions examples/async/contactsMethods.py
Original file line number Diff line number Diff line change
@@ -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())
21 changes: 21 additions & 0 deletions examples/sync/contactsMethods.py
Original file line number Diff line number Diff line change
@@ -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()
6 changes: 2 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand All @@ -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",
Expand All @@ -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"
)
8 changes: 8 additions & 0 deletions tests/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
2 changes: 2 additions & 0 deletions whatsapp_api_client_python/API.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .response import Response as GreenAPIResponse
from .tools import (
account,
contacts,
device,
groups,
journals,
Expand Down Expand Up @@ -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)
Expand Down
126 changes: 126 additions & 0 deletions whatsapp_api_client_python/tools/contacts.py
Original file line number Diff line number Diff line change
@@ -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
Loading