-
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 #8 from Amele9/master
New tests and some refactoring
- Loading branch information
Showing
17 changed files
with
233 additions
and
122 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
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 |
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 |
---|---|---|
@@ -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() |
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 |
---|---|---|
|
@@ -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__": | ||
|
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 |
---|---|---|
|
@@ -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__": | ||
|
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 |
---|---|---|
|
@@ -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__": | ||
|
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1 +1 @@ | ||
requests | ||
requests==2.28.1 |
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 |
---|---|---|
|
@@ -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]", | ||
|
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 |
---|---|---|
@@ -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() |
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 |
---|---|---|
@@ -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"] |
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 |
---|---|---|
@@ -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" | ||
] |
Oops, something went wrong.