Skip to content

Commit

Permalink
Merge pull request #21 from green-api/SW-1731
Browse files Browse the repository at this point in the history
SW-1731
  • Loading branch information
Amele9 authored Jun 14, 2023
2 parents db21b99 + 9d02e05 commit f37c568
Show file tree
Hide file tree
Showing 12 changed files with 282 additions and 209 deletions.
37 changes: 34 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
Expand Down Expand Up @@ -50,6 +49,7 @@ coverage.xml
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
Expand All @@ -72,6 +72,7 @@ instance/
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
Expand All @@ -82,7 +83,9 @@ profile_default/
ipython_config.py

# pyenv
.python-version
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
Expand All @@ -91,7 +94,22 @@ ipython_config.py
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
Expand Down Expand Up @@ -127,3 +145,16 @@ dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea/
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,4 @@ other arrangements, understandings, or agreements concerning use of
licensed material. For the avoidance of doubt, this paragraph does not
form part of the public licenses.

Creative Commons may be contacted at creativecommons.org.
Creative Commons may be contacted at creativecommons.org.
170 changes: 95 additions & 75 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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).
Loading

0 comments on commit f37c568

Please sign in to comment.