diff --git a/pyrogram/client/filters/filters.py b/pyrogram/client/filters/filters.py index fd4a947c..3a798874 100644 --- a/pyrogram/client/filters/filters.py +++ b/pyrogram/client/filters/filters.py @@ -17,7 +17,7 @@ # along with Pyrogram. If not, see . import re -from typing import Callable +from typing import Callable, Union from .filter import Filter from ..types import Message, CallbackQuery, InlineQuery @@ -67,7 +67,7 @@ class Filters: all = create(lambda _, m: True, "AllFilter") """Filter all messages.""" - + me = create(lambda _, m: bool(m.from_user and m.from_user.is_self), "MeFilter") """Filter messages generated by you yourself.""" @@ -296,7 +296,7 @@ class Filters: ) @staticmethod - def regex(pattern: str, flags: int = 0): + def regex(pattern: Union[str, re.Pattern], flags: int = 0): """Filter updates that match a given regular expression pattern. Can be applied to handlers that receive one of the following updates: @@ -309,8 +309,8 @@ class Filters: stored in the ``matches`` field of the update object itself. Parameters: - pattern (``str``): - The regex pattern as string. + pattern (``str`` | ``Pattern``): + The regex pattern as string or as pre-compiled pattern. flags (``int``, *optional*): Regex flags. @@ -331,7 +331,11 @@ class Filters: return bool(update.matches) - return create(func, "RegexFilter", p=re.compile(pattern, flags)) + return create( + func, + "RegexFilter", + p=pattern if isinstance(pattern, re.Pattern) else re.compile(pattern, flags) + ) # noinspection PyPep8Naming class user(Filter, set):