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 continue
elif isinstance(handler, RawUpdateHandler): 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: if args is None:
continue continue

View file

@ -35,6 +35,10 @@ class RawUpdateHandler(Handler):
*(client, update, users, chats)* as positional arguments (look at the section below for *(client, update, users, chats)* as positional arguments (look at the section below for
a detailed description). 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: Other Parameters:
client (:obj:`~pyrogram.Client`): client (:obj:`~pyrogram.Client`):
The Client itself, useful when you want to call other API methods inside the update handler. 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` - :obj:`~pyrogram.raw.types.ChannelForbidden`
""" """
def __init__(self, callback: Callable): def __init__(self, callback: Callable, filters=None):
super().__init__(callback) super().__init__(callback, filters)

View file

@ -17,15 +17,17 @@
# You should have received a copy of the GNU Lesser General Public License # You should have received a copy of the GNU Lesser General Public License
# along with Pyrofork. If not, see <http://www.gnu.org/licenses/>. # along with Pyrofork. If not, see <http://www.gnu.org/licenses/>.
from typing import Callable from typing import Callable, Optional
import pyrogram import pyrogram
from pyrogram.filters import Filter
class OnRawUpdate: class OnRawUpdate:
def on_raw_update( def on_raw_update(
self=None, self: Optional["OnRawUpdate"] = None,
group: int = 0 filters=None,
group: int = 0,
) -> Callable: ) -> Callable:
"""Decorator for handling raw updates. """Decorator for handling raw updates.
@ -33,21 +35,25 @@ class OnRawUpdate:
:obj:`~pyrogram.handlers.RawUpdateHandler`. :obj:`~pyrogram.handlers.RawUpdateHandler`.
Parameters: 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*): group (``int``, *optional*):
The group identifier, defaults to 0. The group identifier, defaults to 0.
""" """
def decorator(func: Callable) -> Callable: def decorator(func: Callable) -> Callable:
if isinstance(self, pyrogram.Client): if isinstance(self, pyrogram.Client):
self.add_handler(pyrogram.handlers.RawUpdateHandler(func), group) self.add_handler(pyrogram.handlers.RawUpdateHandler(func, filters), group)
else: elif isinstance(self, Filter) or self is None:
if not hasattr(func, "handlers"): if not hasattr(func, "handlers"):
func.handlers = [] func.handlers = []
func.handlers.append( func.handlers.append(
( (
pyrogram.handlers.RawUpdateHandler(func), pyrogram.handlers.RawUpdateHandler(func, self),
group group if filters is None else filters
) )
) )