Skip to content

Commit 5905f76

Browse files
committed
Add PollHandler type and on_poll decorator for handling Poll updates
1 parent 5c638e7 commit 5905f76

File tree

6 files changed

+119
-5
lines changed

6 files changed

+119
-5
lines changed

pyrogram/client/ext/dispatcher.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from pyrogram.api import types
2727
from ..handlers import (
2828
CallbackQueryHandler, MessageHandler, DeletedMessagesHandler,
29-
UserStatusHandler, RawUpdateHandler, InlineQueryHandler
29+
UserStatusHandler, RawUpdateHandler, InlineQueryHandler, PollHandler
3030
)
3131

3232
log = logging.getLogger(__name__)
@@ -79,7 +79,10 @@ def __init__(self, client, workers: int):
7979
),
8080

8181
(types.UpdateBotInlineQuery,):
82-
lambda upd, usr, cht: (pyrogram.InlineQuery._parse(self.client, upd, usr), InlineQueryHandler)
82+
lambda upd, usr, cht: (pyrogram.InlineQuery._parse(self.client, upd, usr), InlineQueryHandler),
83+
84+
(types.UpdateMessagePoll,):
85+
lambda upd, usr, cht: (pyrogram.Poll._parse(self.client, upd), PollHandler)
8386
}
8487

8588
self.update_parsers = {key: value for key_tuple, value in self.update_parsers.items() for key in key_tuple}

pyrogram/client/handlers/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121
from .disconnect_handler import DisconnectHandler
2222
from .inline_query_handler import InlineQueryHandler
2323
from .message_handler import MessageHandler
24+
from .poll_handler import PollHandler
2425
from .raw_update_handler import RawUpdateHandler
2526
from .user_status_handler import UserStatusHandler
2627

2728
__all__ = [
2829
"MessageHandler", "DeletedMessagesHandler", "CallbackQueryHandler", "RawUpdateHandler", "DisconnectHandler",
29-
"UserStatusHandler", "InlineQueryHandler"
30+
"UserStatusHandler", "InlineQueryHandler", "PollHandler"
3031
]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2019 Dan Tès <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from .handler import Handler
20+
21+
22+
class PollHandler(Handler):
23+
"""The Poll handler class. Used to handle polls updates.
24+
25+
It is intended to be used with :meth:`add_handler() <pyrogram.Client.add_handler>`
26+
27+
For a nicer way to register this handler, have a look at the
28+
:meth:`on_poll() <pyrogram.Client.on_poll>` decorator.
29+
30+
Args:
31+
callback (``callable``):
32+
Pass a function that will be called when a new poll update arrives. It takes *(client, poll)*
33+
as positional arguments (look at the section below for a detailed description).
34+
35+
filters (:obj:`Filters <pyrogram.Filters>`):
36+
Pass one or more filters to allow only a subset of polls to be passed
37+
in your callback function.
38+
39+
Other parameters:
40+
client (:obj:`Client <pyrogram.Client>`):
41+
The Client itself, useful when you want to call other API methods inside the poll handler.
42+
43+
poll (:obj:`Poll <pyrogram.Poll>`):
44+
The received poll.
45+
"""
46+
47+
def __init__(self, callback: callable, filters=None):
48+
super().__init__(callback, filters)

pyrogram/client/methods/decorators/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from .on_disconnect import OnDisconnect
2222
from .on_inline_query import OnInlineQuery
2323
from .on_message import OnMessage
24+
from .on_poll import OnPoll
2425
from .on_raw_update import OnRawUpdate
2526
from .on_user_status import OnUserStatus
2627

@@ -32,6 +33,7 @@ class Decorators(
3233
OnRawUpdate,
3334
OnDisconnect,
3435
OnUserStatus,
35-
OnInlineQuery
36+
OnInlineQuery,
37+
OnPoll
3638
):
3739
pass
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2019 Dan Tès <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from typing import Tuple
20+
21+
import pyrogram
22+
from pyrogram.client.filters.filter import Filter
23+
from pyrogram.client.handlers.handler import Handler
24+
from ...ext import BaseClient
25+
26+
27+
class OnPoll(BaseClient):
28+
def on_poll(
29+
self=None,
30+
filters=None,
31+
group: int = 0
32+
) -> callable:
33+
"""Use this decorator to automatically register a function for handling poll updates.
34+
This does the same thing as :meth:`add_handler` using the :class:`PollHandler`.
35+
36+
Args:
37+
filters (:obj:`Filters <pyrogram.Filters>`):
38+
Pass one or more filters to allow only a subset of polls to be passed
39+
in your function.
40+
41+
group (``int``, *optional*):
42+
The group identifier, defaults to 0.
43+
"""
44+
45+
def decorator(func: callable) -> Tuple[Handler, int]:
46+
if isinstance(func, tuple):
47+
func = func[0].callback
48+
49+
handler = pyrogram.PollHandler(func, filters)
50+
51+
if isinstance(self, Filter):
52+
return pyrogram.PollHandler(func, self), group if filters is None else filters
53+
54+
if self is not None:
55+
self.add_handler(handler, group)
56+
57+
return handler, group
58+
59+
return decorator

pyrogram/client/types/messages_and_media/poll.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
from pyrogram.api import types
2323
from .poll_option import PollOption
2424
from ..pyrogram_type import PyrogramType
25+
from ..update import Update
2526

2627

27-
class Poll(PyrogramType):
28+
class Poll(PyrogramType, Update):
2829
"""This object represents a Poll.
2930
3031
Args:

0 commit comments

Comments
 (0)