Skip to content

Commit

Permalink
Add filters support for raw handler
Browse files Browse the repository at this point in the history
  • Loading branch information
KurimuzonAkuma committed Dec 12, 2024
1 parent 802284a commit a6dbd7f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
7 changes: 6 additions & 1 deletion pyrogram/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,12 @@ async def handler_worker(self, lock):
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
Expand Down
8 changes: 6 additions & 2 deletions pyrogram/handlers/raw_update_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,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.
Expand Down Expand Up @@ -63,5 +67,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)
14 changes: 10 additions & 4 deletions pyrogram/methods/decorators/on_raw_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
from typing import Callable, Optional

import pyrogram
from pyrogram.filters import Filter


class OnRawUpdate:
def on_raw_update(
self: Optional["OnRawUpdate"] = None,
filters=None,
group: int = 0,
) -> Callable:
"""Decorator for handling raw updates.
Expand All @@ -32,21 +34,25 @@ def on_raw_update(
: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
)
)

Expand Down

0 comments on commit a6dbd7f

Please sign in to comment.