Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SW-3435 #41

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [ "3.7", "3.8", "3.9", "3.10", "3.11", "3.12" ]
python-version: [ "3.8", "3.9", "3.10", "3.11", "3.12" ]

steps:
- uses: actions/checkout@v3
Expand Down
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,39 @@ def call_support_operator_handler(notification: Notification) -> None:
bot.run_forever()
```

### Webhook-mode notifications receiving

By default bot read notifications using long polling method. Receiving notifications is also possible via webhooks:

```python
from whatsapp_chatbot_python import GreenAPIBot, Notification

bot = GreenAPIBot(
"1101000001",
"d75b3a66374942c5b3c019c698abc2067e151558acbd412345",
# Set `webhook_mode` to True (default: False)
webhook_mode=True,
# Set your host for webhook server (default: "0.0.0.0")
webhook_host = "0.0.0.0",
# Set your port for webhook server (default: 8080)
webhook_port = 8080,
# Set your auth header value (:str) from API console
# If it is None, auth header will not affect on data receiving
webhook_auth_header = None,
)


@bot.router.outgoing_message()
def outgoint_message_handler(notification: Notification) -> None:
print("Outgoint message received")

if __name__ == "__main__":
bot.run_forever()

```
For this mode to work correctly, you must specify the correct Webhook Url in the instance settings. Based on [whatsapp-api-webhook-server-python-v2](https://github.com/green-api/whatsapp-api-webhook-server-python-v2) (`python >= 3.8` required)


## Service methods documentation

[Service methods documentation](https://green-api.com/en/docs/api/)
Expand Down
33 changes: 33 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,39 @@ def call_support_operator_handler(notification: Notification) -> None:
bot.run_forever()
```

### Получение уведомлений при помощи webhook

По умолчанию бот читает уведомления, используя long polling метод. Получение уведомлений также возможно с помощью webhook-сервера:

```python
from whatsapp_chatbot_python import GreenAPIBot, Notification

bot = GreenAPIBot(
"1101000001",
"d75b3a66374942c5b3c019c698abc2067e151558acbd412345",
# Укажите значение `webhook_mode` равное True (False по-умолчанию)
webhook_mode = True,
# Укажите хост вебхук-сервера ("0.0.0.0" по-умолчанию)
webhook_host = "0.0.0.0",
# Укажите порт вебхук-сервера (8080 по-умолчанию)
webhook_port = 8080,
# При необходимости, укажите заголовок авторизации (:str), который
# установлен в консоли инстанса. Если указать None, то
# заголовок авторизации не будет проверяться вебхук-сервером
webhook_auth_header = None,
)


@bot.router.outgoing_message()
def outgoint_message_handler(notification: Notification) -> None:
print("Outgoint message received")

if __name__ == "__main__":
bot.run_forever()

```
Для того, чтобы этот режим работал корректно, укажите корректный URL вебхук-севрера в настройках инстанса. В качестве вебхук-сервера используется [whatsapp-api-webhook-server-python-v2](https://github.com/green-api/whatsapp-api-webhook-server-python-v2) (`python >= 3.8`)

## Документация по методам сервиса

[Документация по методам сервиса](https://green-api.com/docs/api/)
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
whatsapp-api-client-python==0.0.45
whatsapp-api-webhook-server-python-v2==0.1.0
16 changes: 8 additions & 8 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
name="whatsapp-chatbot-python",
version="0.9.1",
description=(
"This library helps you easily create"
" a Python chatbot with WhatsApp API."
"This library helps you easily create a Python chatbot with WhatsApp API."
),
long_description=long_description,
long_description_content_type="text/markdown",
Expand All @@ -27,7 +26,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",
Expand All @@ -37,12 +35,14 @@
"Topic :: Communications :: Chat",
"Topic :: Software Development",
"Topic :: Software Development :: Libraries",
"Topic :: Software Development :: Libraries :: Application Frameworks"
"Topic :: Software Development :: Libraries :: Application Frameworks",
],
license=(
"Creative Commons Attribution-NoDerivatives 4.0 International"
" (CC BY-ND 4.0)"
"Creative Commons Attribution-NoDerivatives 4.0 International" " (CC BY-ND 4.0)"
),
install_requires=["whatsapp-api-client-python==0.0.45"],
python_requires=">=3.7"
install_requires=[
"whatsapp-api-client-python==0.0.45",
"whatsapp-api-webhook-server-python-v2==0.1.0",
],
python_requires=">=3.8",
)
14 changes: 7 additions & 7 deletions tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
"messageData": {
"typeMessage": "textMessage",
"textMessageData": {
"textMessage": "Hello"
}
}
"textMessage": "Hello",
},
},
}


Expand Down Expand Up @@ -44,12 +44,12 @@ def handler(_):

self.assertEqual(len(bot.router.message.handlers), 2)

@patch("whatsapp_chatbot_python.bot.Bot._update_settings")
def create_bot(self, mock__update_settings: MagicMock) -> GreenAPIBot:
mock__update_settings.return_value = None
@patch("whatsapp_chatbot_python.bot.Bot._Bot__init_instance_settings")
def create_bot(self, mock__init_instance_settings: MagicMock) -> GreenAPIBot:
mock__init_instance_settings.return_value = None

return GreenAPIBot("", "", delete_notifications_at_startup=False)


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
Loading
Loading