Skip to content

Commit

Permalink
Merge pull request #8 from Amele9/master
Browse files Browse the repository at this point in the history
New tests and some refactoring
  • Loading branch information
ripreal authored Dec 14, 2022
2 parents 2d7c255 + 0681760 commit b5ac6f4
Show file tree
Hide file tree
Showing 17 changed files with 233 additions and 122 deletions.
58 changes: 31 additions & 27 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
@@ -1,41 +1,45 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Python application

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
- pull_request
- push

permissions:
contents: read

jobs:
build:
name: ${{ matrix.python-version }}

runs-on: ubuntu-latest
env:
ID_INSTANCE: ${{ secrets.ID_INSTANCE }}
API_TOKEN_INSTANCE: ${{ secrets.API_TOKEN_INSTANCE }}

runs-on: ${{ matrix.os }}
strategy:
matrix:
os:
- ubuntu-latest
python-version:
- "3.7"
- "3.8"
- "3.9"
- "3.10"

steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |
python${{ matrix.python-version }} -m flake8 --extend-ignore E999 .
- name: Test with pytest
run: |
pytest
28 changes: 28 additions & 0 deletions examples/bot_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from os import environ

from whatsapp_api_client_python import GreenAPI, Bot

ID_INSTANCE = environ["ID_INSTANCE"]
API_TOKEN_INSTANCE = environ["API_TOKEN_INSTANCE"]

greenAPI = GreenAPI(ID_INSTANCE, API_TOKEN_INSTANCE)

bot = Bot(greenAPI)


@bot.handler(type_webhook="incomingMessageReceived")
def handler(body: dict) -> None:
greenAPI.read_mark.read_chat(
chatId=body["senderData"]["chatId"],
idMessage=body["idMessage"]
)


@bot.message(message_text="Hello")
def message_handler(body: dict) -> str:
sender_name = body["senderData"]["senderName"]

return f"Hello, {sender_name}."


bot.run_forever()
7 changes: 3 additions & 4 deletions examples/create_group_and_send_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ def main():
chatIds=["[email protected]"]
)

print(create_group_response.data)
print(create_group_response)

chatId = create_group_response.data["chatId"]
send_message_response = greenAPI.sending.send_message(
chatId=chatId,
chatId=create_group_response["chatId"],
message="Any message"
)

print(send_message_response.data)
print(send_message_response)


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions examples/send_file_by_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
def main():
response = greenAPI.sending.send_file_by_upload(
chatId="[email protected]",
path="C:\\Games\\PicFromDisk.png"
file="C:\\Games\\PicFromDisk.png"
)

print(response.data)
print(response)


if __name__ == "__main__":
Expand Down
7 changes: 5 additions & 2 deletions examples/send_file_by_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
def main():
response = greenAPI.sending.send_file_by_url(
chatId="[email protected]",
urlFile="https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png",
urlFile=(
"https://www.google.com/images/branding/"
"googlelogo/2x/googlelogo_color_272x92dp.png"
),
fileName="googlelogo_color_272x92dp.png"
)

print(response.data)
print(response)


if __name__ == "__main__":
Expand Down
8 changes: 3 additions & 5 deletions examples/send_message.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from os import environ

from whatsapp_api_client_python import GreenAPI

ID_INSTANCE = '1101000001'
API_TOKEN_INSTANCE = '3e03ea9ff3324e228ae3dfdf4d48e409bfa1b1ad0b0c46bf8c'
ID_INSTANCE = "1101000001"
API_TOKEN_INSTANCE = "3e03ea9ff3324e228ae3dfdf4d48e409bfa1b1ad0b0c46bf8c"

greenAPI = GreenAPI(ID_INSTANCE, API_TOKEN_INSTANCE)

Expand All @@ -14,7 +12,7 @@ def main():
message="Any message"
)

print(response.data)
print(response)


if __name__ == "__main__":
Expand Down
28 changes: 0 additions & 28 deletions examples/webhook_example.py

This file was deleted.

2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
requests
requests==2.28.1
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="whatsapp_api_client_python",
version="0.0.29",
version="0.0.30",
install_requires=["requests"],
author="Ivan Sadovy",
author_email="[email protected]",
Expand Down
12 changes: 6 additions & 6 deletions tests/test_library.py → tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@


class GreenAPITestCase(unittest.TestCase):
def test_getSettings(self):
response = greenAPI.account.get_settings()
def test_GetStateInstance(self):
response = greenAPI.account.get_state_instance()

self.assertEqual(response.status_code, 200, response.error)
self.assertTrue(response)

def test_getStateInstance(self):
response = greenAPI.account.get_state_instance()
def test_CheckWhatsapp(self):
response = greenAPI.service_methods.check_whatsapp(79001234567)

self.assertEqual(response.status_code, 200, response.error)
self.assertTrue(response)


if __name__ == '__main__':
Expand Down
69 changes: 69 additions & 0 deletions tests/test_bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import unittest
from os import environ

from whatsapp_api_client_python import GreenAPI, Bot
from whatsapp_api_client_python.bot import Handler, MessageHandler

ID_INSTANCE = environ["ID_INSTANCE"]
API_TOKEN_INSTANCE = environ["API_TOKEN_INSTANCE"]

greenAPI = GreenAPI(ID_INSTANCE, API_TOKEN_INSTANCE)

bot = Bot(greenAPI)


class BotTestCase(unittest.TestCase):
def test_handler(self):
type_webhook = "incomingMessageReceived"

@bot.handler(type_webhook=type_webhook)
def handler(body: dict) -> None:
self.assertEqual(body["typeWebhook"], type_webhook)

self.run_forever()

def test_message_handler(self):
message_text = "Hello"

@bot.message(message_text=message_text)
def message_handler(body: dict) -> str:
message_data = body["messageData"]
text_message_data = message_data["textMessageData"]
text_message = text_message_data["textMessage"]

self.assertEqual(text_message, message_text)

return "Response"

self.run_forever()

def run_forever(self):
response = {
"body": {
"typeWebhook": "incomingMessageReceived",
"messageData": {
"typeMessage": "textMessage",
"textMessageData": {
"textMessage": "Hello"
}
}
}
}

body = response["body"]
type_webhook = body["typeWebhook"]

for handler in bot.handlers:
if handler.type_webhook == type_webhook:
if isinstance(handler, Handler):
handler.function(body)
elif isinstance(handler, MessageHandler):
check_result = handler.check_message_text(body)
if check_result:
message = handler.function(body)
if isinstance(message, str):
self.assertEqual(message, "Response")


if __name__ == '__main__':
unittest.main()
3 changes: 1 addition & 2 deletions whatsapp_api_client_python/api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from .abc import AbstractAPI
from .api import GreenAPI
from .api import GreenAPIError
from .api import GreenAPI, GreenAPIError

__all__ = ["AbstractAPI", "GreenAPI", "GreenAPIError"]
5 changes: 4 additions & 1 deletion whatsapp_api_client_python/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ def __init__(self, status_code: int, error_message: str):
self.error_message = error_message

def __str__(self) -> str:
return f"{self.status_code=} | {self.error_message=}"
return (
f"status_code={self.status_code}, "
f"error_message={self.error_message}"
)


__all__ = ["GreenAPI", "GreenAPIError"]
11 changes: 9 additions & 2 deletions whatsapp_api_client_python/bot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
from .bot import Bot
from .bot import AbstractBot, Bot
from .handlers import AbstractHandler, Handler, MessageHandler

__all__ = ["Bot"]
__all__ = [
"AbstractBot",
"Bot",
"AbstractHandler",
"Handler",
"MessageHandler"
]
Loading

0 comments on commit b5ac6f4

Please sign in to comment.