diff --git a/pyrogram/dispatcher.py b/pyrogram/dispatcher.py index b6c47658..a0ee6cb9 100644 --- a/pyrogram/dispatcher.py +++ b/pyrogram/dispatcher.py @@ -353,7 +353,12 @@ class Dispatcher: continue elif isinstance(handler, RawUpdateHandler): - args = (update, users, chats) + try: + if await handler.check(self.client, update): + args = (update, users, chats) + except Exception as e: + log.exception(e) + continue if args is None: continue diff --git a/pyrogram/handlers/raw_update_handler.py b/pyrogram/handlers/raw_update_handler.py index 3ea2bfc3..bb278032 100644 --- a/pyrogram/handlers/raw_update_handler.py +++ b/pyrogram/handlers/raw_update_handler.py @@ -35,6 +35,10 @@ class RawUpdateHandler(Handler): *(client, update, users, chats)* 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 callback queries to be passed + in your callback function. + Other Parameters: client (:obj:`~pyrogram.Client`): The Client itself, useful when you want to call other API methods inside the update handler. @@ -64,5 +68,5 @@ class RawUpdateHandler(Handler): - :obj:`~pyrogram.raw.types.ChannelForbidden` """ - def __init__(self, callback: Callable): - super().__init__(callback) + def __init__(self, callback: Callable, filters=None): + super().__init__(callback, filters) diff --git a/pyrogram/methods/decorators/on_raw_update.py b/pyrogram/methods/decorators/on_raw_update.py index ff1a8c95..88e842ab 100644 --- a/pyrogram/methods/decorators/on_raw_update.py +++ b/pyrogram/methods/decorators/on_raw_update.py @@ -17,15 +17,17 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrofork. If not, see . -from typing import Callable +from typing import Callable, Optional import pyrogram +from pyrogram.filters import Filter class OnRawUpdate: def on_raw_update( - self=None, - group: int = 0 + self: Optional["OnRawUpdate"] = None, + filters=None, + group: int = 0, ) -> Callable: """Decorator for handling raw updates. @@ -33,24 +35,28 @@ class OnRawUpdate: :obj:`~pyrogram.handlers.RawUpdateHandler`. Parameters: + filters (:obj:`~pyrogram.filters`, *optional*): + Pass one or more filters to allow only a subset of callback queries to be passed + in your function. + group (``int``, *optional*): The group identifier, defaults to 0. """ def decorator(func: Callable) -> Callable: if isinstance(self, pyrogram.Client): - self.add_handler(pyrogram.handlers.RawUpdateHandler(func), group) - else: + self.add_handler(pyrogram.handlers.RawUpdateHandler(func, filters), group) + elif isinstance(self, Filter) or self is None: if not hasattr(func, "handlers"): func.handlers = [] func.handlers.append( ( - pyrogram.handlers.RawUpdateHandler(func), - group + pyrogram.handlers.RawUpdateHandler(func, self), + group if filters is None else filters ) ) return func - return decorator + return decorator \ No newline at end of file