Skip to content

Commit

Permalink
Merge pull request #29 from green-api/SW-2939
Browse files Browse the repository at this point in the history
Sw 2939
  • Loading branch information
tkulzhan authored Jan 31, 2024
2 parents 5c5a379 + efbb7de commit f2b07b1
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
35 changes: 35 additions & 0 deletions examples/polls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from whatsapp_chatbot_python import GreenAPIBot, Notification

bot = GreenAPIBot(
"1101000001", "d75b3a66374942c5b3c019c698abc2067e151558acbd412345"
)

@bot.router.message(text_message="message")
def send_poll(notification: Notification) -> None:
notification.api.sending.sendPoll(
chatId=notification.chat,
message='A or B?',
options=[
{"optionName": "A"},
{"optionName": "B"},
],
multipleAnswers=False
)

@bot.router.polls()
def polls_handler(notification: Notification) -> None:
chatId = notification.chat
vote_data = notification.event["messageData"]["pollMessageData"]["votes"]
sender_vote = None

for vote in vote_data:
if notification.event["senderData"]["sender"] in vote["optionVoters"]:
sender_vote = vote["optionName"]
break
if sender_vote == "A":
notification.api.sending.sendMessage(chatId, "You selected A")
if sender_vote == "B":
notification.api.sending.sendMessage(chatId, "You selected B")


bot.run_forever()
2 changes: 1 addition & 1 deletion whatsapp_chatbot_python/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def run_forever(self) -> Optional[NoReturn]:
while True:
try:
response = self.api.receiving.receiveNotification()

if not response.data:
continue
response = response.data
Expand Down
14 changes: 13 additions & 1 deletion whatsapp_chatbot_python/manager/observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,17 @@ def add_handler(self, handler: HandlerType, **filters: Any) -> None:

self.router.message.add_handler(handler, **filters)

class PollObserver(Observer):
def add_handler(self, handler: HandlerType, **filters: Any) -> None:
message_types = [
"pollUpdateMessage"
]

message_type = filters.get("type_message")
if message_type not in message_types:
filters["type_message"] = message_types

self.router.message.add_handler(handler, **filters)


__all__ = ["AbstractObserver", "ButtonObserver", "Observer"]
__all__ = ["AbstractObserver", "ButtonObserver", "PollObserver", "Observer"]
5 changes: 3 additions & 2 deletions whatsapp_chatbot_python/manager/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
from typing import Dict, TYPE_CHECKING

from .observer import AbstractObserver, ButtonObserver, Observer
from .observer import AbstractObserver, ButtonObserver, PollObserver, Observer

if TYPE_CHECKING:
from ..bot import GreenAPI
Expand All @@ -20,6 +20,8 @@ def __init__(self, api: "GreenAPI", logger: logging.Logger):

self.buttons: AbstractObserver = ButtonObserver(self)

self.polls: AbstractObserver = PollObserver(self)

self.observers: Dict[str, AbstractObserver] = {
"incomingMessageReceived": self.message,
"outgoingMessageReceived": self.outgoing_message,
Expand All @@ -42,5 +44,4 @@ def route_event(self, event: dict) -> None:

observer.update_event(event)


__all__ = ["Router"]

0 comments on commit f2b07b1

Please sign in to comment.