Skip to content

Commit

Permalink
Merge pull request #33 from green-api/SW-2939-rc1
Browse files Browse the repository at this point in the history
Added poll support
  • Loading branch information
Amele9 committed Feb 6, 2024
2 parents d78a6d4 + df4101d commit 66aafcc
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 7 deletions.
52 changes: 52 additions & 0 deletions examples/poll.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from whatsapp_chatbot_python import GreenAPIBot, Notification

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


@bot.router.message(command="start")
def message_handler(notification: Notification) -> None:
sender_data = notification.event["senderData"]
sender_name = sender_data["senderName"]

response = notification.answer_with_poll(
f"Hello, {sender_name}. Here's what I can do:\n\n",
[
{"optionName": "1. Report a problem"},
{"optionName": "2. Show office address"},
{"optionName": "3. Show available rates"},
{"optionName": "4. Call a support operator"}
]
)

stanza = response.data["idMessage"]

bot.router.poll_update_message.add_handler_with_stanza(
start_poll_handler, stanza
)


def start_poll_handler(notification: Notification) -> None:
votes = notification.event["messageData"]["pollMessageData"]["votes"]
for vote_data in votes:
voters = vote_data["optionVoters"]
if voters:
option_name = vote_data["optionName"]
if option_name == "1. Report a problem":
link = "https://github.com/green-api/issues/issues/new"

notification.answer(link, link_preview=False)
elif option_name == "2. Show office address":
notification.api.sending.sendLocation(
notification.chat, 55.7522200, 37.6155600
)
elif option_name == "3. Show available rates":
notification.answer_with_file("data/rates.png")
elif option_name == "4. Call a support operator":
notification.answer(
"Good. A tech support operator will contact you soon."
)


bot.run_forever()
30 changes: 25 additions & 5 deletions whatsapp_chatbot_python/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
if TYPE_CHECKING:
from .manager.handler import Notification

TEXT_TYPES = ["textMessage", "extendedTextMessage", "quotedMessage"]


class AbstractFilter(ABC):
@abstractmethod
Expand Down Expand Up @@ -132,19 +134,36 @@ def check_event(self, notification: "Notification") -> bool:
return False


class StanzaFilter(AbstractFilter):
def __init__(self, stanza: Optional[str]):
self.stanza = stanza

def check_event(self, notification: "Notification") -> bool:
message_data = notification.event["messageData"]

type_message = message_data["typeMessage"]
if type_message == "pollUpdateMessage":
stanza = message_data["pollMessageData"]["stanzaId"]
if stanza == self.stanza:
return True

return False


filters: Dict[str, Type[AbstractFilter]] = {
"from_chat": ChatIDFilter,
"from_sender": SenderFilter,
"type_message": TypeMessageFilter,
"text_message": TextMessageFilter,
"regexp": RegExpFilter,
"command": CommandFilter,
"state": StateFilter
"state": StateFilter,
"stanza": StanzaFilter
}

TEXT_TYPES = ["textMessage", "extendedTextMessage", "quotedMessage"]

__all__ = [
"TEXT_TYPES",

"AbstractFilter",
"ChatIDFilter",
"SenderFilter",
Expand All @@ -153,6 +172,7 @@ def check_event(self, notification: "Notification") -> bool:
"RegExpFilter",
"CommandFilter",
"StateFilter",
"filters",
"TEXT_TYPES"
"StanzaFilter",

"filters"
]
13 changes: 13 additions & 0 deletions whatsapp_chatbot_python/manager/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,19 @@ def answer_with_file(
chat, file, file_name, caption, quoted_message_id
)

def answer_with_poll(
self,
message: str,
options: List[Dict[str, str]],
multiple_answers: Optional[bool] = None,
quoted_message_id: Optional[str] = None
) -> Optional[Response]:
chat = self.get_chat()
if chat:
return self.api.sending.sendPoll(
chat, message, options, multiple_answers, quoted_message_id
)


HandlerType = Callable[[Notification], Any]

Expand Down
32 changes: 31 additions & 1 deletion whatsapp_chatbot_python/manager/observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,34 @@ def add_handler(self, handler: HandlerType, **filters: Any) -> None:
self.router.message.add_handler(handler, **filters)


__all__ = ["AbstractObserver", "ButtonObserver", "Observer"]
class PollObserver(Observer):
def add_handler(self, handler: HandlerType, **filters: Any) -> None:
filters["type_message"] = "pollMessage"

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


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

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

def add_handler_with_stanza(
self, handler: HandlerType, stanza: str, **filters: Any
) -> None:
filters.update({
"stanza": stanza,
"type_message": "pollUpdateMessage"
})

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


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

from .observer import AbstractObserver, ButtonObserver, Observer
from .observer import (
AbstractObserver,
ButtonObserver,
Observer,
PollObserver,
PollUpdateObserver
)

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

self.buttons: AbstractObserver = ButtonObserver(self)

self.poll_message: PollObserver = PollObserver(self)
self.poll_update_message: PollUpdateObserver = PollUpdateObserver(self)

self.observers: Dict[str, AbstractObserver] = {
"incomingMessageReceived": self.message,
"outgoingMessageReceived": self.outgoing_message,
Expand Down

0 comments on commit 66aafcc

Please sign in to comment.