Skip to content

Commit 66aafcc

Browse files
authored
Merge pull request #33 from green-api/SW-2939-rc1
Added poll support
2 parents d78a6d4 + df4101d commit 66aafcc

File tree

5 files changed

+131
-7
lines changed

5 files changed

+131
-7
lines changed

examples/poll.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from whatsapp_chatbot_python import GreenAPIBot, Notification
2+
3+
bot = GreenAPIBot(
4+
"1101000001", "d75b3a66374942c5b3c019c698abc2067e151558acbd412345"
5+
)
6+
7+
8+
@bot.router.message(command="start")
9+
def message_handler(notification: Notification) -> None:
10+
sender_data = notification.event["senderData"]
11+
sender_name = sender_data["senderName"]
12+
13+
response = notification.answer_with_poll(
14+
f"Hello, {sender_name}. Here's what I can do:\n\n",
15+
[
16+
{"optionName": "1. Report a problem"},
17+
{"optionName": "2. Show office address"},
18+
{"optionName": "3. Show available rates"},
19+
{"optionName": "4. Call a support operator"}
20+
]
21+
)
22+
23+
stanza = response.data["idMessage"]
24+
25+
bot.router.poll_update_message.add_handler_with_stanza(
26+
start_poll_handler, stanza
27+
)
28+
29+
30+
def start_poll_handler(notification: Notification) -> None:
31+
votes = notification.event["messageData"]["pollMessageData"]["votes"]
32+
for vote_data in votes:
33+
voters = vote_data["optionVoters"]
34+
if voters:
35+
option_name = vote_data["optionName"]
36+
if option_name == "1. Report a problem":
37+
link = "https://github.com/green-api/issues/issues/new"
38+
39+
notification.answer(link, link_preview=False)
40+
elif option_name == "2. Show office address":
41+
notification.api.sending.sendLocation(
42+
notification.chat, 55.7522200, 37.6155600
43+
)
44+
elif option_name == "3. Show available rates":
45+
notification.answer_with_file("data/rates.png")
46+
elif option_name == "4. Call a support operator":
47+
notification.answer(
48+
"Good. A tech support operator will contact you soon."
49+
)
50+
51+
52+
bot.run_forever()

whatsapp_chatbot_python/filters.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
if TYPE_CHECKING:
66
from .manager.handler import Notification
77

8+
TEXT_TYPES = ["textMessage", "extendedTextMessage", "quotedMessage"]
9+
810

911
class AbstractFilter(ABC):
1012
@abstractmethod
@@ -132,19 +134,36 @@ def check_event(self, notification: "Notification") -> bool:
132134
return False
133135

134136

137+
class StanzaFilter(AbstractFilter):
138+
def __init__(self, stanza: Optional[str]):
139+
self.stanza = stanza
140+
141+
def check_event(self, notification: "Notification") -> bool:
142+
message_data = notification.event["messageData"]
143+
144+
type_message = message_data["typeMessage"]
145+
if type_message == "pollUpdateMessage":
146+
stanza = message_data["pollMessageData"]["stanzaId"]
147+
if stanza == self.stanza:
148+
return True
149+
150+
return False
151+
152+
135153
filters: Dict[str, Type[AbstractFilter]] = {
136154
"from_chat": ChatIDFilter,
137155
"from_sender": SenderFilter,
138156
"type_message": TypeMessageFilter,
139157
"text_message": TextMessageFilter,
140158
"regexp": RegExpFilter,
141159
"command": CommandFilter,
142-
"state": StateFilter
160+
"state": StateFilter,
161+
"stanza": StanzaFilter
143162
}
144163

145-
TEXT_TYPES = ["textMessage", "extendedTextMessage", "quotedMessage"]
146-
147164
__all__ = [
165+
"TEXT_TYPES",
166+
148167
"AbstractFilter",
149168
"ChatIDFilter",
150169
"SenderFilter",
@@ -153,6 +172,7 @@ def check_event(self, notification: "Notification") -> bool:
153172
"RegExpFilter",
154173
"CommandFilter",
155174
"StateFilter",
156-
"filters",
157-
"TEXT_TYPES"
175+
"StanzaFilter",
176+
177+
"filters"
158178
]

whatsapp_chatbot_python/manager/handler.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,19 @@ def answer_with_file(
104104
chat, file, file_name, caption, quoted_message_id
105105
)
106106

107+
def answer_with_poll(
108+
self,
109+
message: str,
110+
options: List[Dict[str, str]],
111+
multiple_answers: Optional[bool] = None,
112+
quoted_message_id: Optional[str] = None
113+
) -> Optional[Response]:
114+
chat = self.get_chat()
115+
if chat:
116+
return self.api.sending.sendPoll(
117+
chat, message, options, multiple_answers, quoted_message_id
118+
)
119+
107120

108121
HandlerType = Callable[[Notification], Any]
109122

whatsapp_chatbot_python/manager/observer.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,34 @@ def add_handler(self, handler: HandlerType, **filters: Any) -> None:
8989
self.router.message.add_handler(handler, **filters)
9090

9191

92-
__all__ = ["AbstractObserver", "ButtonObserver", "Observer"]
92+
class PollObserver(Observer):
93+
def add_handler(self, handler: HandlerType, **filters: Any) -> None:
94+
filters["type_message"] = "pollMessage"
95+
96+
self.router.message.add_handler(handler, **filters)
97+
98+
99+
class PollUpdateObserver(Observer):
100+
def add_handler(self, handler: HandlerType, **filters: Any) -> None:
101+
filters["type_message"] = "pollUpdateMessage"
102+
103+
self.router.message.add_handler(handler, **filters)
104+
105+
def add_handler_with_stanza(
106+
self, handler: HandlerType, stanza: str, **filters: Any
107+
) -> None:
108+
filters.update({
109+
"stanza": stanza,
110+
"type_message": "pollUpdateMessage"
111+
})
112+
113+
self.router.message.add_handler(handler, **filters)
114+
115+
116+
__all__ = [
117+
"AbstractObserver",
118+
"ButtonObserver",
119+
"Observer",
120+
"PollObserver",
121+
"PollUpdateObserver"
122+
]

whatsapp_chatbot_python/manager/router.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
import logging
33
from typing import Dict, TYPE_CHECKING
44

5-
from .observer import AbstractObserver, ButtonObserver, Observer
5+
from .observer import (
6+
AbstractObserver,
7+
ButtonObserver,
8+
Observer,
9+
PollObserver,
10+
PollUpdateObserver
11+
)
612

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

2127
self.buttons: AbstractObserver = ButtonObserver(self)
2228

29+
self.poll_message: PollObserver = PollObserver(self)
30+
self.poll_update_message: PollUpdateObserver = PollUpdateObserver(self)
31+
2332
self.observers: Dict[str, AbstractObserver] = {
2433
"incomingMessageReceived": self.message,
2534
"outgoingMessageReceived": self.outgoing_message,

0 commit comments

Comments
 (0)