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>
This commit is contained in:
parent
4df9357b48
commit
3bc96b4193
@ -17,7 +17,7 @@
|
||||
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
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):
|
||||
|
Loading…
Reference in New Issue
Block a user