-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from green-api/SW-1731
SW-1731
- Loading branch information
Showing
12 changed files
with
282 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,121 +6,146 @@ | |
![](https://img.shields.io/github/actions/workflow/status/green-api/whatsapp-api-client-python/python-package.yml) | ||
![](https://img.shields.io/pypi/dm/whatsapp-api-client-python) | ||
|
||
- [Документация на русском языке](README_RUS.md) | ||
- [Документация на русском языке](https://github.com/green-api/whatsapp-api-client-python/blob/master/README_RUS.md). | ||
|
||
Python library for intagration with WhatsAPP messanger via API of [green-api.com](https://green-api.com/en/) service. To use the library you have to get a registration token and an account id in the [personal area](https://console.green-api.com). There is a free developer account tariff plan. | ||
whatsapp-api-client-python is a library for integration with WhatsApp messenger using the API | ||
service [green-api.com](https://green-api.com/en/). You should get a registration token and an account ID in | ||
your [personal cabinet](https://console.green-api.com/) to use the library. There is a free developer account tariff. | ||
|
||
## API | ||
|
||
You can find REST API documentation by [url](https://green-api.com/en/docs/api/). The library is a wrapper for REST API, so the documentation at the above url applies to the library as well. | ||
The documentation for the REST API can be found at the [link](https://green-api.com/en/docs/). The library is a wrapper | ||
for the REST API, so the documentation at the link above also applies. | ||
|
||
## Authorization | ||
|
||
To send a message or perform other GREEN API methods, the WhatsApp account in the phone app must be authorized. To | ||
authorize the account, go to your [cabinet](https://console.green-api.com/) and scan the QR code using the WhatsApp app. | ||
|
||
## Installation | ||
|
||
```shell | ||
pip install whatsapp-api-client-python | ||
python -m pip install whatsapp-api-client-python | ||
``` | ||
|
||
## Import | ||
## Import | ||
|
||
``` | ||
from whatsapp_api_client_python import API | ||
``` | ||
## Authorization | ||
|
||
To send a message or to exacute some other Green-API method, you have to have the WhatsApp account in the phone application to be authorized. To authorize your account please go to the [personal area](https://console.green-api.com) and scan a QR-code using the WhatsApp application. | ||
|
||
## Examples | ||
|
||
### How to initialize an object | ||
|
||
```python | ||
greenAPI = API.GreenApi(ID_INSTANCE, API_TOKEN_INSTANCE) | ||
``` | ||
greenAPI = API.GreenApi( | ||
"1101000001", "d75b3a66374942c5b3c019c698abc2067e151558acbd412345" | ||
) | ||
``` | ||
|
||
### Sending a text message to a WhatsApp number | ||
|
||
```python | ||
result = greenAPI.sending.sendMessage('[email protected]', 'Message text') | ||
``` | ||
Link to example: [sendTextMessage.py]( | ||
https://github.com/green-api/whatsapp-api-client-python/blob/master/examples/sendTextMessage.py | ||
). | ||
|
||
Example url: [sendTextMessage.py](https://github.com/green-api/whatsapp-api-client-python/blob/master/examples/sendTextMessage.py) | ||
|
||
Please note that keys can be obtained from environment variables: | ||
```python | ||
from os import environ | ||
``` | ||
response = greenAPI.sending.sendMessage("[email protected]", "Message text") | ||
ID_INSTANCE = environ['ID_INSTANCE'] | ||
API_TOKEN_INSTANCE = environ['API_TOKEN_INSTANCE'] | ||
print(response.data) | ||
``` | ||
|
||
### Sending an image via URL | ||
|
||
```python | ||
result = greenAPI.sending.sendFileByUrl('[email protected]', | ||
'https://www.google.ru/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png', | ||
'googlelogo_color_272x92dp.png', 'Google logo') | ||
``` | ||
Link to example: [sendPictureByLink.py]( | ||
https://github.com/green-api/whatsapp-api-client-python/blob/master/examples/sendPictureByLink.py | ||
). | ||
|
||
Example url: [sendPictureByLink.py](https://github.com/green-api/whatsapp-api-client-python/blob/master/examples/sendPictureByLink.py) | ||
``` | ||
response = greenAPI.sending.sendFileByUrl( | ||
"[email protected]", | ||
"https://green-api.com/green-api-logo_2.png", | ||
"green-api-logo_2.png", | ||
"GREEN API logo" | ||
) | ||
print(response.data) | ||
``` | ||
|
||
### Sending an image by uploading from the disk | ||
|
||
```python | ||
result = greenAPI.sending.sendFileByUpload('[email protected]', | ||
'C:\Games\PicFromDisk.png', | ||
'PicFromDisk.png', 'Picture from disk') | ||
``` | ||
Link to example: [sendPictureByUpload.py]( | ||
https://github.com/green-api/whatsapp-api-client-python/blob/master/examples/sendPictureByUpload.py | ||
). | ||
|
||
Example url: [sendPictureByUpload.py](https://github.com/green-api/whatsapp-api-client-python/blob/master/examples/sendPictureByUpload.py) | ||
``` | ||
response = greenAPI.sending.sendFileByUpload( | ||
"[email protected]", | ||
"data/green-api-logo_2.png", | ||
"green-api-logo_2.png", | ||
"GREEN API logo" | ||
) | ||
print(response.data) | ||
``` | ||
|
||
### Group creation and sending a message to the group | ||
|
||
```python | ||
chatIds = [ | ||
"[email protected]" | ||
] | ||
resultCreate = greenAPI.groups.createGroup('GroupName', | ||
chatIds) | ||
|
||
if resultCreate.code == 200: | ||
resultSend = greenAPI.sending.sendMessage(resultCreate.data['chatId'], | ||
'Message text') | ||
``` | ||
**Attention**. If one tries to create a group with a non-existent number, WhatsApp may block the sender's number. The | ||
number in the example is non-existent. | ||
|
||
IMPORTANT: If one tries to create a group with a non-existent number, WhatsApp | ||
may block the sender's number. The number in the example is non-existent. | ||
Link to example: [createGroupAndSendMessage.py]( | ||
https://github.com/green-api/whatsapp-api-client-python/blob/master/examples/createGroupAndSendMessage.py | ||
). | ||
|
||
Example url: [createGroupAndSendMessage.py](https://github.com/green-api/whatsapp-api-client-python/blob/master/examples/createGroupAndSendMessage.py) | ||
``` | ||
create_group_response = greenAPI.groups.createGroup( | ||
"Group Name", ["[email protected]"] | ||
) | ||
if create_group_response.code == 200: | ||
send_message_response = greenAPI.sending.sendMessage( | ||
create_group_response.data["chatId"], "Message text" | ||
) | ||
``` | ||
|
||
### Receive incoming messages by HTTP API | ||
|
||
The general concept of receiving data in the Green API is described [here](https://green-api.com/en/docs/api/receiving/) | ||
To start receiving messages by the HTTP API you need to execute the library method: | ||
Link to example: [receiveNotification.py]( | ||
https://github.com/green-api/whatsapp-api-client-python/blob/master/examples/receiveNotification.py | ||
). | ||
|
||
The general concept of receiving data in the GREEN API is described [here]( | ||
https://green-api.com/en/docs/api/receiving/ | ||
). To start receiving notifications by the HTTP API you need to execute the library method: | ||
|
||
```python | ||
``` | ||
greenAPI.webhooks.startReceivingNotifications(onEvent) | ||
``` | ||
|
||
onEvent - your method which should contain parameters: | ||
Parameter | Description | ||
----- | ----- | ||
typewebhook | received message type (string) | ||
body | message body (json) | ||
onEvent - your function which should contain parameters: | ||
|
||
Message body types and formats [here](https://green-api.com/en/docs/api/receiving/notifications-format/) | ||
| Parameter | Description | | ||
|-------------|----------------------------------| | ||
| typeWebhook | received notification type (str) | | ||
| body | notification body (dict) | | ||
|
||
This method will be called when an incoming message is received. Next, process messages according to the business logic of your system. | ||
Notification body types and formats can be found [here]( | ||
https://green-api.com/en/docs/api/receiving/notifications-format/ | ||
). | ||
|
||
## Examples list | ||
This method will be called when an incoming notification is received. Next, process notifications according to the | ||
business logic of your system. | ||
|
||
Description | Module | ||
----- | ----- | ||
Example of sending text | [sendTextMessage.py](https://github.com/green-api/whatsapp-api-client-python/blob/master/examples/sendTextMessage.py) | ||
Example of sending a picture by URL | [sendPictureByLink.py](https://github.com/green-api/whatsapp-api-client-python/blob/master/examples/sendPictureByLink.py) | ||
Example of sending a picture by uploading from the disk | [sendPictureByUpload.py](https://github.com/green-api/whatsapp-api-client-python/blob/master/examples/sendPictureByUpload.py) | ||
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) | ||
## Examples list | ||
|
||
| Description | Module | | ||
|----------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------| | ||
| Example of sending text | [sendTextMessage.py](https://github.com/green-api/whatsapp-api-client-python/blob/master/examples/sendTextMessage.py) | | ||
| Example of sending a picture by URL | [sendPictureByLink.py](https://github.com/green-api/whatsapp-api-client-python/blob/master/examples/sendPictureByLink.py) | | ||
| Example of sending a picture by uploading from the disk | [sendPictureByUpload.py](https://github.com/green-api/whatsapp-api-client-python/blob/master/examples/sendPictureByUpload.py) | | ||
| 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) | | ||
|
||
## The full list of the library methods | ||
|
||
|
@@ -177,20 +202,15 @@ Example of incoming webhooks receiving | [receiveNotification.py](https://github | |
|
||
## Service methods documentation | ||
|
||
[https://green-api.com/en/docs/api/](https://green-api.com/en/docs/api/) | ||
[https://green-api.com/en/docs/api/](https://green-api.com/en/docs/api/). | ||
|
||
## External products | ||
|
||
* [requests](https://requests.readthedocs.io) - for http requests | ||
- [requests](https://requests.readthedocs.io/en/latest/) - for HTTP requests. | ||
|
||
## License | ||
|
||
[![CC BY-ND 4.0][cc-by-nd-shield]][cc-by-nd] | ||
|
||
This work is licensed under a | ||
[Creative Commons Attribution-NoDerivatives 4.0 International License][cc-by-nd]. | ||
|
||
[cc-by-nd]: https://creativecommons.org/licenses/by-nd/4.0/ | ||
[cc-by-nd-shield]: https://img.shields.io/badge/License-CC%20BY--ND%204.0-lightgrey.svg | ||
|
||
Please see file [LICENSE](LICENSE) | ||
Licensed under [ | ||
Creative Commons Attribution-NoDerivatives 4.0 International (CC BY-ND 4.0) | ||
](https://creativecommons.org/licenses/by-nd/4.0/) terms. | ||
Please see file [LICENSE](https://github.com/green-api/whatsapp-api-client-python/blob/master/LICENSE). |
Oops, something went wrong.