From 5905f761fa214a459aa66f63630c72b2cd408273 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 14 Apr 2019 20:50:13 +0200 Subject: [PATCH] Add PollHandler type and on_poll decorator for handling Poll updates --- pyrogram/client/ext/dispatcher.py | 7 ++- pyrogram/client/handlers/__init__.py | 3 +- pyrogram/client/handlers/poll_handler.py | 48 +++++++++++++++ .../client/methods/decorators/__init__.py | 4 +- pyrogram/client/methods/decorators/on_poll.py | 59 +++++++++++++++++++ .../client/types/messages_and_media/poll.py | 3 +- 6 files changed, 119 insertions(+), 5 deletions(-) create mode 100644 pyrogram/client/handlers/poll_handler.py create mode 100644 pyrogram/client/methods/decorators/on_poll.py diff --git a/pyrogram/client/ext/dispatcher.py b/pyrogram/client/ext/dispatcher.py index 7552b034..46be47f7 100644 --- a/pyrogram/client/ext/dispatcher.py +++ b/pyrogram/client/ext/dispatcher.py @@ -26,7 +26,7 @@ import pyrogram from pyrogram.api import types from ..handlers import ( CallbackQueryHandler, MessageHandler, DeletedMessagesHandler, - UserStatusHandler, RawUpdateHandler, InlineQueryHandler + UserStatusHandler, RawUpdateHandler, InlineQueryHandler, PollHandler ) log = logging.getLogger(__name__) @@ -79,7 +79,10 @@ class Dispatcher: ), (types.UpdateBotInlineQuery,): - lambda upd, usr, cht: (pyrogram.InlineQuery._parse(self.client, upd, usr), InlineQueryHandler) + lambda upd, usr, cht: (pyrogram.InlineQuery._parse(self.client, upd, usr), InlineQueryHandler), + + (types.UpdateMessagePoll,): + lambda upd, usr, cht: (pyrogram.Poll._parse(self.client, upd), PollHandler) } self.update_parsers = {key: value for key_tuple, value in self.update_parsers.items() for key in key_tuple} diff --git a/pyrogram/client/handlers/__init__.py b/pyrogram/client/handlers/__init__.py index 5e392949..c88c12fe 100644 --- a/pyrogram/client/handlers/__init__.py +++ b/pyrogram/client/handlers/__init__.py @@ -21,10 +21,11 @@ from .deleted_messages_handler import DeletedMessagesHandler from .disconnect_handler import DisconnectHandler from .inline_query_handler import InlineQueryHandler from .message_handler import MessageHandler +from .poll_handler import PollHandler from .raw_update_handler import RawUpdateHandler from .user_status_handler import UserStatusHandler __all__ = [ "MessageHandler", "DeletedMessagesHandler", "CallbackQueryHandler", "RawUpdateHandler", "DisconnectHandler", - "UserStatusHandler", "InlineQueryHandler" + "UserStatusHandler", "InlineQueryHandler", "PollHandler" ] diff --git a/pyrogram/client/handlers/poll_handler.py b/pyrogram/client/handlers/poll_handler.py new file mode 100644 index 00000000..567fcec0 --- /dev/null +++ b/pyrogram/client/handlers/poll_handler.py @@ -0,0 +1,48 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-2019 Dan Tès +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +from .handler import Handler + + +class PollHandler(Handler): + """The Poll handler class. Used to handle polls updates. + + It is intended to be used with :meth:`add_handler() ` + + For a nicer way to register this handler, have a look at the + :meth:`on_poll() ` decorator. + + Args: + callback (``callable``): + Pass a function that will be called when a new poll update arrives. It takes *(client, poll)* + as positional arguments (look at the section below for a detailed description). + + filters (:obj:`Filters `): + Pass one or more filters to allow only a subset of polls to be passed + in your callback function. + + Other parameters: + client (:obj:`Client `): + The Client itself, useful when you want to call other API methods inside the poll handler. + + poll (:obj:`Poll `): + The received poll. + """ + + def __init__(self, callback: callable, filters=None): + super().__init__(callback, filters) diff --git a/pyrogram/client/methods/decorators/__init__.py b/pyrogram/client/methods/decorators/__init__.py index 33f55a3d..2a2861ae 100644 --- a/pyrogram/client/methods/decorators/__init__.py +++ b/pyrogram/client/methods/decorators/__init__.py @@ -21,6 +21,7 @@ from .on_deleted_messages import OnDeletedMessages from .on_disconnect import OnDisconnect from .on_inline_query import OnInlineQuery from .on_message import OnMessage +from .on_poll import OnPoll from .on_raw_update import OnRawUpdate from .on_user_status import OnUserStatus @@ -32,6 +33,7 @@ class Decorators( OnRawUpdate, OnDisconnect, OnUserStatus, - OnInlineQuery + OnInlineQuery, + OnPoll ): pass diff --git a/pyrogram/client/methods/decorators/on_poll.py b/pyrogram/client/methods/decorators/on_poll.py new file mode 100644 index 00000000..56dcd757 --- /dev/null +++ b/pyrogram/client/methods/decorators/on_poll.py @@ -0,0 +1,59 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-2019 Dan Tès +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +from typing import Tuple + +import pyrogram +from pyrogram.client.filters.filter import Filter +from pyrogram.client.handlers.handler import Handler +from ...ext import BaseClient + + +class OnPoll(BaseClient): + def on_poll( + self=None, + filters=None, + group: int = 0 + ) -> callable: + """Use this decorator to automatically register a function for handling poll updates. + This does the same thing as :meth:`add_handler` using the :class:`PollHandler`. + + Args: + filters (:obj:`Filters `): + Pass one or more filters to allow only a subset of polls to be passed + in your function. + + group (``int``, *optional*): + The group identifier, defaults to 0. + """ + + def decorator(func: callable) -> Tuple[Handler, int]: + if isinstance(func, tuple): + func = func[0].callback + + handler = pyrogram.PollHandler(func, filters) + + if isinstance(self, Filter): + return pyrogram.PollHandler(func, self), group if filters is None else filters + + if self is not None: + self.add_handler(handler, group) + + return handler, group + + return decorator diff --git a/pyrogram/client/types/messages_and_media/poll.py b/pyrogram/client/types/messages_and_media/poll.py index e0461bba..acaf8697 100644 --- a/pyrogram/client/types/messages_and_media/poll.py +++ b/pyrogram/client/types/messages_and_media/poll.py @@ -22,9 +22,10 @@ import pyrogram from pyrogram.api import types from .poll_option import PollOption from ..pyrogram_type import PyrogramType +from ..update import Update -class Poll(PyrogramType): +class Poll(PyrogramType, Update): """This object represents a Poll. Args: