|
| 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 |
0 commit comments