Skip to content

Commit

Permalink
Merge pull request #6 from green-api/dev
Browse files Browse the repository at this point in the history
Added settings update function and fixed filters
  • Loading branch information
Amele9 authored Jun 21, 2023
2 parents e167342 + d5b9194 commit ec8560e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 16 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="whatsapp-chatbot-python",
version="0.3.0",
version="0.4.0",
description=(
"This library helps you easily create"
" a Python chatbot with WhatsApp API."
Expand Down
13 changes: 8 additions & 5 deletions tests/test_manager.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest
from unittest.mock import patch

from whatsapp_chatbot_python import GreenAPIBot, Notification

Expand All @@ -15,7 +16,7 @@

class ManagerTestCase(unittest.TestCase):
def test_router(self):
bot = self.bot
bot = self.create_bot()

@bot.router.message()
def message_handler(notification: Notification):
Expand All @@ -24,7 +25,7 @@ def message_handler(notification: Notification):
bot.router.route_event(event_example)

def test_filters(self):
bot = self.bot
bot = self.create_bot()

@bot.router.message(command="help")
def command_handler(_):
Expand All @@ -33,7 +34,7 @@ def command_handler(_):
bot.router.route_event(event_example)

def test_observers(self):
bot = self.bot
bot = self.create_bot()

@bot.router.message()
def handler(_):
Expand All @@ -43,8 +44,10 @@ def handler(_):

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

@property
def bot(self):
@patch("whatsapp_chatbot_python.bot.Bot._update_settings")
def create_bot(self, mock__update_settings):
mock__update_settings.return_value = None

return GreenAPIBot("", "")


Expand Down
23 changes: 23 additions & 0 deletions whatsapp_chatbot_python/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class Bot:
def __init__(self, id_instance: str, api_token_instance: str):
self.api = GreenAPI(id_instance, api_token_instance)

self._update_settings()

self.router = Router(self.api)

def run_forever(self):
Expand All @@ -26,6 +28,27 @@ def run_forever(self):
except KeyboardInterrupt:
break

def _update_settings(self):
settings = self.api.account.getSettings()
if settings.error:
raise GreenAPIError(settings.error)

response = settings.data

incoming_webhook = response["incomingWebhook"]
outgoing_message_webhook = response["outgoingMessageWebhook"]
outgoing_api_message_webhook = response["outgoingAPIMessageWebhook"]
if (
incoming_webhook == "no"
and outgoing_message_webhook == "no"
and outgoing_api_message_webhook == "no"
):
self.api.account.setSettings({
"incomingWebhook": "yes",
"outgoingMessageWebhook": "yes",
"outgoingAPIMessageWebhook": "yes"
})


class GreenAPIBot(Bot):
pass
Expand Down
23 changes: 13 additions & 10 deletions whatsapp_chatbot_python/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,53 +16,55 @@ def check_event(self, event: dict) -> bool:
class ChatIDFilter(AbstractFilter):
def __init__(self, chat: Union[str, List[str]]):
self.chat = chat
if isinstance(chat, str):
self.chat = [chat]

def check_event(self, event: dict) -> bool:
chat = event["senderData"]["chatId"]

if chat == self.chat or chat in self.chat:
if chat in self.chat:
return True
return False


class SenderFilter(AbstractFilter):
def __init__(self, sender: Union[str, List[str]]):
self.sender = sender
if isinstance(sender, str):
self.sender = [sender]

def check_event(self, event: dict) -> bool:
sender = event["senderData"]["sender"]

if sender == self.sender or sender in self.sender:
if sender in self.sender:
return True
return False


class TypeMessageFilter(AbstractFilter):
def __init__(self, type_message: Union[str, List[str]]):
self.type_message = type_message
if isinstance(type_message, str):
self.type_message = [type_message]

def check_event(self, event: dict) -> bool:
type_message = event["messageData"]["typeMessage"]

if (
type_message == self.type_message
or type_message in self.type_message
):
if type_message in self.type_message:
return True
return False


class TextMessageFilter(AbstractFilter):
def __init__(self, text_message: Union[str, List[str]]):
self.text_message = text_message
if isinstance(text_message, str):
self.text_message = [text_message]

def check_event(self, event: dict) -> bool:
text_message = self.get_text_message(event)

if (
text_message == self.text_message
or text_message in self.text_message
):
if text_message in self.text_message:
return True
return False

Expand All @@ -83,6 +85,7 @@ def __init__(self, pattern: str):

def check_event(self, event: dict) -> bool:
text_message = TextMessageFilter.get_text_message(event)

if fullmatch(self.pattern, text_message):
return True
return False
Expand Down

0 comments on commit ec8560e

Please sign in to comment.