diff --git a/README.md b/README.md index caaf591..cd41d8b 100644 --- a/README.md +++ b/README.md @@ -169,6 +169,20 @@ response = greenAPI.sending.sendPoll( print(response.data) ``` +### Sending a text status + +Link to example: [sendTextStatus.py](https://github.com/green-api/whatsapp-api-client-python/blob/master/examples/statusesMethods/sendTextStatus.py). + +``` +response = greenAPI.statuses.sendTextStatus( + "I sent this status using Green Api Python SDK!", + "#54c774", + "NORICAN_REGULAR" +) + +print(response.data) +``` + ## Examples list | Description | Module | @@ -179,6 +193,7 @@ print(response.data) | Example of a group creation and sending a message to the group | [createGroupAndSendMessage.py](https://github.com/green-api/whatsapp-api-client-python/blob/master/examples/createGroupAndSendMessage.py) | | Example of incoming webhooks receiving | [receiveNotification.py](https://github.com/green-api/whatsapp-api-client-python/blob/master/examples/receiveNotification.py) | | Example of sending a polling message | [sendPoll.py](https://github.com/green-api/whatsapp-api-client-python/blob/master/examples/sendPoll.py) | +| Example of sending a text status | [sendTextStatus.py](https://github.com/green-api/whatsapp-api-client-python/blob/master/examples/sendTextStatus.py) | ## The full list of the library methods @@ -204,6 +219,13 @@ print(response.data) | `groups.removeAdmin` | The method deprives the participant of group chat administration rights | [RemoveAdmin](https://green-api.com/en/docs/api/groups/RemoveAdmin/) | | `groups.setGroupPicture` | The method sets the avatar of the group | [SetGroupPicture](https://green-api.com/en/docs/api/groups/SetGroupPicture/) | | `groups.leaveGroup` | The method logs the user of the current account out of the group chat | [LeaveGroup](https://green-api.com/en/docs/api/groups/LeaveGroup/) | +| `statuses.sendTextStatus` | The method is aimed for sending a text status | [SendTextStatus](https://green-api.com/en/docs/api/statuses/SendTextStatus/) | +| `statuses.sendVoiceStatus` | The method is aimed for sending a voice status | [SendVoiceStatus](https://green-api.com/en/docs/api/statuses/SendVoiceStatus/) | +| `statuses.sendMediaStatus` | The method is aimed for sending a pictures or video status | [SendMediaStatus](https://green-api.com/en/docs/api/statuses/SendMediaStatus/) | +| `statuses.deleteStatus` | The method is aimed for deleting a certain status | [DeleteStatus](https://green-api.com/en/docs/api/statuses/DeleteStatus/) | +| `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/) | +| `statuses.getOutgoingStatuses` | The method returns the outgoing statuses of the account | [GetOutgoingStatuses](https://green-api.com/en/docs/api/statuses/GetOutgoingStatuses/) | +| `statuses.getIncomingStatuses` | The method returns the incoming statuses of the account | [GetIncomingStatuses](https://green-api.com/en/docs/api/statuses/GetIncomingStatuses/) | | `journals.getChatHistory` | The method returns the chat message history | [GetChatHistory](https://green-api.com/en/docs/api/journals/GetChatHistory/) | | `journals.getMessage` | The method returns a chat message | [GetMessage](https://green-api.com/en/docs/api/journals/GetMessage/) | | `journals.lastIncomingMessages` | The method returns the most recent incoming messages of the account | [LastIncomingMessages](https://green-api.com/en/docs/api/journals/LastIncomingMessages/) | diff --git a/docs/README.md b/docs/README.md index a8704ce..3c47657 100644 --- a/docs/README.md +++ b/docs/README.md @@ -153,6 +153,20 @@ response = greenAPI.sending.sendPoll( print(response.data) ``` +## Отправка текстового статуса + +Ссылка на пример: [sendPoll.py](https://github.com/green-api/whatsapp-api-client-python/blob/master/examples/statusesMethods/sendTextStatus.py). + +``` +response = greenAPI.statuses.sendTextStatus( + "I sent this status using Green Api Python SDK!", + "#54c774", + "NORICAN_REGULAR" +) + +print(response.data) +``` + ## Список примеров | Описание | Модуль | diff --git a/examples/partherMethods/DeleteInstanceAccount.py b/examples/partherMethods/DeleteInstanceAccount.py index a6b48e0..f48d013 100644 --- a/examples/partherMethods/DeleteInstanceAccount.py +++ b/examples/partherMethods/DeleteInstanceAccount.py @@ -9,6 +9,5 @@ def main(): response = greenAPI.partner.deleteInstanceAccount(1103123456) print(response.data) - if __name__ == '__main__': main() diff --git a/examples/statusesMethods/deleteStatus.py b/examples/statusesMethods/deleteStatus.py new file mode 100644 index 0000000..5290a76 --- /dev/null +++ b/examples/statusesMethods/deleteStatus.py @@ -0,0 +1,13 @@ +from whatsapp_api_client_python import API + +greenAPI = API.GreenAPI( + "1101000001", "d75b3a66374942c5b3c019c698abc2067e151558acbd412345" +) + + +def main(): + response = greenAPI.statuses.deleteStatus('BAE54F518532FCB1') + print(response.data) + +if __name__ == '__main__': + main() diff --git a/examples/statusesMethods/getStatuses.py b/examples/statusesMethods/getStatuses.py new file mode 100644 index 0000000..9e93c04 --- /dev/null +++ b/examples/statusesMethods/getStatuses.py @@ -0,0 +1,19 @@ +from whatsapp_api_client_python import API + +greenAPI = API.GreenAPI( + "1101000001", "d75b3a66374942c5b3c019c698abc2067e151558acbd412345" +) + + +def main(): + response = greenAPI.statuses.getIncomingStatuses(1400) # minutes argument is Optional + print(response.data) + + response = greenAPI.statuses.getOutgoingStatuses(1400) # minutes argument is Optional + print(response.data) + + response = greenAPI.statuses.getStatusStatistic('BAE54F518532FCB1') + print(response.data) + +if __name__ == '__main__': + main() diff --git a/examples/statusesMethods/sendMediaStatus.py b/examples/statusesMethods/sendMediaStatus.py new file mode 100644 index 0000000..f8e9ee5 --- /dev/null +++ b/examples/statusesMethods/sendMediaStatus.py @@ -0,0 +1,17 @@ +from whatsapp_api_client_python import API + +greenAPI = API.GreenAPI( + "1101000001", "d75b3a66374942c5b3c019c698abc2067e151558acbd412345" +) + + +def main(): + response = greenAPI.statuses.sendMediaStatus( + "https://example.com/file.mp4", + "test.mp4" + "#54c774") + + print(response.data) + +if __name__ == '__main__': + main() diff --git a/examples/statusesMethods/sendTextStatus.py b/examples/statusesMethods/sendTextStatus.py new file mode 100644 index 0000000..8fdaefc --- /dev/null +++ b/examples/statusesMethods/sendTextStatus.py @@ -0,0 +1,17 @@ +from whatsapp_api_client_python import API + +greenAPI = API.GreenAPI( + "1101000001", "d75b3a66374942c5b3c019c698abc2067e151558acbd412345" +) + + +def main(): + response = greenAPI.statuses.sendTextStatus( + "I sent this status using Green Api Python SDK!", + "#54c774", + "NORICAN_REGULAR") + + print(response.data) + +if __name__ == '__main__': + main() diff --git a/examples/statusesMethods/sendVoiceStatus.py b/examples/statusesMethods/sendVoiceStatus.py new file mode 100644 index 0000000..7a8abf5 --- /dev/null +++ b/examples/statusesMethods/sendVoiceStatus.py @@ -0,0 +1,17 @@ +from whatsapp_api_client_python import API + +greenAPI = API.GreenAPI( + "1101000001", "d75b3a66374942c5b3c019c698abc2067e151558acbd412345" +) + + +def main(): + response = greenAPI.statuses.sendVoiceStatus( + "https://example.com/file.mp3", + "test.mp3" + "#54c774") + + print(response.data) + +if __name__ == '__main__': + main() diff --git a/tests/test_methods.py b/tests/test_methods.py index 3865f06..5861b7b 100644 --- a/tests/test_methods.py +++ b/tests/test_methods.py @@ -21,6 +21,7 @@ def test_methods(self, mock_request): *self.account_methods, *self.device_methods, *self.group_methods, + *self.status_methods, *self.log_methods, *self.queue_methods, *self.read_mark_methods, @@ -68,6 +69,18 @@ def group_methods(self) -> typing.List[Response]: api.groups.leaveGroup("") ] + @property + def status_methods(self) -> typing.List[Response]: + return [ + api.statuses.sendTextStatus(""), + api.statuses.sendVoiceStatus("", ""), + api.statuses.sendMediaStatus("", ""), + api.statuses.deleteStatus(""), + api.statuses.getStatusStatistic(""), + api.statuses.getIncomingStatuses(), + api.statuses.getOutgoingStatuses() + ] + @property def log_methods(self) -> typing.List[Response]: return [ diff --git a/whatsapp_api_client_python/API.py b/whatsapp_api_client_python/API.py index fb900f0..7cfe050 100644 --- a/whatsapp_api_client_python/API.py +++ b/whatsapp_api_client_python/API.py @@ -17,7 +17,8 @@ sending, serviceMethods, webhooks, - partner + partner, + statuses ) @@ -63,6 +64,7 @@ def __init__( self.sending = sending.Sending(self) self.serviceMethods = serviceMethods.ServiceMethods(self) self.webhooks = webhooks.Webhooks(self) + self.statuses = statuses.Statuses(self) self.logger = logging.getLogger("whatsapp-api-client-python") self.__prepare_logger() @@ -176,7 +178,6 @@ def __prepare_session(self) -> None: self.session.mount("http://", HTTPAdapter(max_retries=retry)) self.session.mount("https://", HTTPAdapter(max_retries=retry)) - class GreenAPI(GreenApi): pass diff --git a/whatsapp_api_client_python/tools/partner.py b/whatsapp_api_client_python/tools/partner.py index 73d3435..a003213 100644 --- a/whatsapp_api_client_python/tools/partner.py +++ b/whatsapp_api_client_python/tools/partner.py @@ -44,7 +44,7 @@ def deleteInstanceAccount(self, idInstance: int) -> Response: https://green-api.com/en/docs/partners/deleteInstanceAccount/ """ - request_body = self.handle_parameters(locals()) + request_body = self.__handle_parameters(locals()) return self.api.request( "POST", ( @@ -52,10 +52,15 @@ def deleteInstanceAccount(self, idInstance: int) -> Response: "deleteInstanceAccount/{{partnerToken}}" ), 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) - def handle_parameters(self, parameters: dict) -> dict: - return { - key: value - for key, value in parameters.items() - if value is not None and key != "self" - } \ No newline at end of file + return handled_parameters \ No newline at end of file diff --git a/whatsapp_api_client_python/tools/statuses.py b/whatsapp_api_client_python/tools/statuses.py new file mode 100644 index 0000000..1c1c883 --- /dev/null +++ b/whatsapp_api_client_python/tools/statuses.py @@ -0,0 +1,164 @@ +from typing import List, Optional, TYPE_CHECKING + +from ..response import Response + +if TYPE_CHECKING: + from ..API import GreenApi + + +class Statuses: + def __init__(self, api: "GreenApi"): + self.api = api + + def sendTextStatus( + self, + message: str, + backgroundColor: Optional[str] = None, + font: Optional[str] = None, + participants: Optional[List[str]] = None + ) -> Response: + """ + The method is aimed for sending a text status. + + https://green-api.com/en/docs/api/statuses/SendTextStatus/ + """ + + request_body = self.__handle_parameters(locals()) + + return self.api.request( + "POST", ( + "{{host}}/waInstance{{idInstance}}/" + "sendTextStatus/{{apiTokenInstance}}" + ), request_body + ) + + def sendVoiceStatus( + self, + urlFile: str, + fileName: str, + backgroundColor: Optional[str] = None, + participants: Optional[List[str]] = None + ) -> Response: + """ + The method is aimed for sending a voice status. + + https://green-api.com/en/docs/api/statuses/SendVoiceStatus/ + """ + + request_body = self.__handle_parameters(locals()) + + return self.api.request( + "POST", ( + "{{host}}/waInstance{{idInstance}}/" + "sendVoiceStatus/{{apiTokenInstance}}" + ), request_body + ) + + def sendMediaStatus( + self, + urlFile: str, + fileName: str, + caption: Optional[str] = None, + participants: Optional[List[str]] = None + ) -> Response: + """ + The method is aimed for sending a pictures or video status. + + https://green-api.com/en/docs/api/statuses/SendMediaStatus/ + """ + + request_body = self.__handle_parameters(locals()) + + return self.api.request( + "POST", ( + "{{host}}/waInstance{{idInstance}}/" + "sendMediaStatus/{{apiTokenInstance}}" + ), request_body + ) + + def deleteStatus( + self, + idMessage: str + ) -> Response: + """ + The method is aimed for deleting a certain status. + + https://green-api.com/en/docs/api/statuses/DeleteStatus/ + """ + + request_body = self.__handle_parameters(locals()) + + return self.api.request( + "POST", ( + "{{host}}/waInstance{{idInstance}}/" + "deleteStatus/{{apiTokenInstance}}" + ), request_body + ) + + def getStatusStatistic( + self, + idMessage: str + ) -> Response: + """ + The method returns an array of recipients marked sent/delivered/read for a given status. + + https://green-api.com/en/docs/api/statuses/GetStatusStatistic/ + """ + url = ( + "{{host}}/waInstance{{idInstance}}/" + "getStatusStatistic/{{apiTokenInstance}}" + ) + + return self.api.request("GET", f"{url}?idMessage={idMessage}") + + def getIncomingStatuses( + self, + minutes: Optional[int] = None + ) -> Response: + """ + The method returns the incoming statuses of the account + If no argument passed, the incoming statuses for the past 24 hours are returned. + + https://green-api.com/en/docs/api/statuses/GetIncomingStatuses/ + """ + url = ( + "{{host}}/waInstance{{idInstance}}/" + "getIncomingStatuses/{{apiTokenInstance}}" + ) + + if minutes: + return self.api.request("GET", f"{url}?minutes={minutes}") + else: + return self.api.request("GET", f"{url}") + + def getOutgoingStatuses( + self, + minutes: Optional[int] = None + ) -> Response: + """ + The method returns the outgoing statuses of the account + If no argument passed, the outgoing statuses for the past 24 hours are returned. + + https://green-api.com/en/docs/api/statuses/GetOutgoingStatuses/ + """ + url = ( + "{{host}}/waInstance{{idInstance}}/" + "getOutgoingStatuses/{{apiTokenInstance}}" + ) + + if minutes: + return self.api.request("GET", f"{url}?minutes={minutes}") + else: + return self.api.request("GET", f"{url}") + + @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