pyrofork: Add filters support for raw handler

Signed-off-by: Yasir Aris M <git@yasirdev.my.id>
This commit is contained in:
KurimuzonAkuma 2024-12-12 14:02:02 +03:00 committed by Yasir
parent ae3bc516fb
commit 252882851e
3 changed files with 26 additions and 11 deletions

View file

@ -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

View file

@ -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)

View file

@ -17,15 +17,17 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrofork. If not, see <http://www.gnu.org/licenses/>.
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