Skip to content

Commit 0c54bb0

Browse files
Add filters support for raw handler
Co-authored-by: KurimuzonAkuma <[email protected]>
1 parent 5b29cd9 commit 0c54bb0

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

pyrogram/dispatcher.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,12 @@ async def handler_worker(self, lock):
331331
continue
332332

333333
elif isinstance(handler, RawUpdateHandler):
334-
args = (update, users, chats)
334+
try:
335+
if await handler.check(self.client, update):
336+
args = (update, users, chats)
337+
except Exception as e:
338+
log.exception(e)
339+
continue
335340

336341
if args is None:
337342
continue

pyrogram/filters.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def create(func: Callable, name: str = None, **kwargs) -> Filter:
136136
The *filter* argument refers to the filter itself and can be used to access keyword arguments (read below).
137137
The *client* argument refers to the :obj:`~pyrogram.Client` that received the update.
138138
The *update* argument type will vary depending on which `Handler <handlers>`_ is coming from.
139-
For example, in a :obj:`~pyrogram.handlers.MessageHandler` the *update* argument will be a :obj:`~pyrogram.types.Message`; in a :obj:`~pyrogram.handlers.CallbackQueryHandler` the *update* will be a :obj:`~pyrogram.types.CallbackQuery`.
139+
For example, in a :obj:`~pyrogram.handlers.MessageHandler` the *update* argument will be a :obj:`~pyrogram.types.Message`; in a :obj:`~pyrogram.handlers.CallbackQueryHandler` the *update* will be a :obj:`~pyrogram.types.CallbackQuery`; in a :obj:`~pyrogram.handlers.RawUpdateHandler` the *update* will be a :obj:`~pyrogram.raw.base.Update`.
140140
Your function body can then access the incoming update attributes and decide whether to allow it or not.
141141
142142
name (``str``, *optional*):
@@ -146,6 +146,7 @@ def create(func: Callable, name: str = None, **kwargs) -> Filter:
146146
**kwargs (``any``, *optional*):
147147
Any keyword argument you would like to pass. Useful when creating parameterized custom filters, such as
148148
:meth:`~pyrogram.filters.command` or :meth:`~pyrogram.filters.regex`.
149+
149150
"""
150151
return type(
151152
name or func.__name__ or CUSTOM_FILTER_NAME,

pyrogram/handlers/raw_update_handler.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ class RawUpdateHandler(Handler):
3333
A function that will be called when a new update is received from the server. It takes
3434
*(client, update, users, chats)* as positional arguments (look at the section below for
3535
a detailed description).
36+
37+
filters (:obj:`Filters`):
38+
Pass one or more filters to allow only a subset of callback queries to be passed
39+
in your callback function.
3640
3741
Other Parameters:
3842
client (:obj:`~pyrogram.Client`):
@@ -63,5 +67,5 @@ class RawUpdateHandler(Handler):
6367
- :obj:`~pyrogram.raw.types.ChannelForbidden`
6468
"""
6569

66-
def __init__(self, callback: Callable):
67-
super().__init__(callback)
70+
def __init__(self, callback: Callable, filters=None):
71+
super().__init__(callback, filters)

pyrogram/methods/decorators/on_raw_update.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
from typing import Callable
2020

2121
import pyrogram
22+
from pyrogram.filters import Filter
2223

2324

2425
class OnRawUpdate:
2526
def on_raw_update(
2627
self=None,
28+
filters=None,
2729
group: int = 0
2830
) -> Callable:
2931
"""Decorator for handling raw updates.
@@ -32,21 +34,25 @@ def on_raw_update(
3234
:obj:`~pyrogram.handlers.RawUpdateHandler`.
3335
3436
Parameters:
37+
filters (:obj:`~pyrogram.filters`, *optional*):
38+
Pass one or more filters to allow only a subset of callback queries
39+
to be passed in your callback function.
40+
3541
group (``int``, *optional*):
3642
The group identifier, defaults to 0.
3743
"""
3844

3945
def decorator(func: Callable) -> Callable:
4046
if isinstance(self, pyrogram.Client):
41-
self.add_handler(pyrogram.handlers.RawUpdateHandler(func), group)
42-
else:
47+
self.add_handler(pyrogram.handlers.RawUpdateHandler(func, filters), group)
48+
elif isinstance(self, Filter) or self is None:
4349
if not hasattr(func, "handlers"):
4450
func.handlers = []
4551

4652
func.handlers.append(
4753
(
48-
pyrogram.handlers.RawUpdateHandler(func),
49-
group
54+
pyrogram.handlers.RawUpdateHandler(func, self),
55+
group if filters is None else filters
5056
)
5157
)
5258

0 commit comments

Comments
 (0)