From 3bc96b41932c3182a84b05b9fbba688149b0e08d Mon Sep 17 00:00:00 2001 From: CyanBook Date: Fri, 21 Aug 2020 07:21:53 +0200 Subject: [PATCH] Add support for compiled patterns in Filters.regex (#468) * Add support for compiled patterns in Filters.regex and remove extra whitespaces * Update filters.py Co-authored-by: Dan <14043624+delivrance@users.noreply.github.com> --- pyrogram/client/filters/filters.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) 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):