From 27d5caf40e41fc3576518c4dc9469b5896fbe5fb Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sat, 16 May 2020 00:35:05 +0200 Subject: [PATCH] Give Filters.regex superpowers Basically make it work on Message, CallbackQuery and InlineQuery updates --- pyrogram/client/filters/filters.py | 36 +++++++++++++------ .../bots_and_keyboards/callback_query.py | 9 +++-- .../client/types/inline_mode/inline_query.py | 10 ++++-- 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/pyrogram/client/filters/filters.py b/pyrogram/client/filters/filters.py index 24637640..50543f6e 100644 --- a/pyrogram/client/filters/filters.py +++ b/pyrogram/client/filters/filters.py @@ -20,6 +20,7 @@ import re from typing import Callable from .filter import Filter +from ..types import Message, CallbackQuery, InlineQuery from ..types.bots_and_keyboards import InlineKeyboardMarkup, ReplyKeyboardMarkup CUSTOM_FILTER_NAME = "CustomFilter" @@ -288,26 +289,39 @@ class Filters: ) @staticmethod - def regex(pattern, flags: int = 0): - """Filter message texts or captions that match a given regular expression pattern. + def regex(pattern: str, flags: int = 0): + """Filter updates that match a given regular expression pattern. + + Can be applied to handlers that receive one of the following updates: + + - :obj:`Message`: The filter will match ``text`` or ``caption``. + - :obj:`CallbackQuery`: The filter will match ``data``. + - :obj:`InlineQuery`: The filter will match ``query``. + + When a pattern matches, all the `Match Objects `_ are + stored in the ``matches`` field of the update object itself. Parameters: pattern (``str``): - The RegEx pattern as string, it will be applied to the text or the caption of a message. When a pattern - matches, all the `Match Objects `_ are stored - in the *matches* field of the :obj:`Message` itself. + The regex pattern as string. flags (``int``, *optional*): - RegEx flags. + Regex flags. """ - def func(flt, message): - text = message.text or message.caption + def func(flt, update): + if isinstance(update, Message): + value = update.text or update.caption + elif isinstance(update, CallbackQuery): + value = update.data + elif isinstance(update, InlineQuery): + value = update.query + else: + raise ValueError("Regex filter doesn't work with {}".format(type(update))) - if text: - message.matches = list(flt.p.finditer(text)) or None + update.matches = list(flt.p.finditer(value)) or None - return bool(message.matches) + return bool(update.matches) return create(func, "RegexFilter", p=re.compile(pattern, flags)) diff --git a/pyrogram/client/types/bots_and_keyboards/callback_query.py b/pyrogram/client/types/bots_and_keyboards/callback_query.py index 34b8b52c..9820e560 100644 --- a/pyrogram/client/types/bots_and_keyboards/callback_query.py +++ b/pyrogram/client/types/bots_and_keyboards/callback_query.py @@ -18,7 +18,7 @@ from base64 import b64encode from struct import pack -from typing import Union +from typing import Union, List, Match import pyrogram from pyrogram.api import types @@ -59,6 +59,9 @@ class CallbackQuery(Object, Update): game_short_name (``str``, *optional*): Short name of a Game to be returned, serves as the unique identifier for the game. + matches (List of regex Matches, *optional*): + A list containing all `Match Objects `_ that match + the data of this callback query. Only applicable when using :obj:`Filters.regex `. """ def __init__( @@ -71,7 +74,8 @@ class CallbackQuery(Object, Update): message: "pyrogram.Message" = None, inline_message_id: str = None, data: Union[str, bytes] = None, - game_short_name: str = None + game_short_name: str = None, + matches: List[Match] = None ): super().__init__(client) @@ -82,6 +86,7 @@ class CallbackQuery(Object, Update): self.inline_message_id = inline_message_id self.data = data self.game_short_name = game_short_name + self.matches = matches @staticmethod def _parse(client, callback_query, users) -> "CallbackQuery": diff --git a/pyrogram/client/types/inline_mode/inline_query.py b/pyrogram/client/types/inline_mode/inline_query.py index 44fea75d..eadc539c 100644 --- a/pyrogram/client/types/inline_mode/inline_query.py +++ b/pyrogram/client/types/inline_mode/inline_query.py @@ -16,7 +16,7 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from typing import List +from typing import List, Match import pyrogram from pyrogram.api import types @@ -47,6 +47,10 @@ class InlineQuery(Object, Update): location (:obj:`Location`. *optional*): Sender location, only for bots that request user location. + + matches (List of regex Matches, *optional*): + A list containing all `Match Objects `_ that match + the query of this inline query. Only applicable when using :obj:`Filters.regex `. """ def __init__( @@ -57,7 +61,8 @@ class InlineQuery(Object, Update): from_user: User, query: str, offset: str, - location: Location = None + location: Location = None, + matches: List[Match] = None ): super().__init__(client) @@ -66,6 +71,7 @@ class InlineQuery(Object, Update): self.query = query self.offset = offset self.location = location + self.matches = matches @staticmethod def _parse(client, inline_query: types.UpdateBotInlineQuery, users: dict) -> "InlineQuery":