diff --git a/AUTHORS.rst b/AUTHORS.rst index 6a6825a6384..61de41be9ae 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -21,6 +21,7 @@ The following wonderful people contributed directly or indirectly to this projec - `Jacob Bom `_ - `JASON0916 `_ - `jh0ker `_ +- `jossalgon `_ - `JRoot3D `_ - `jlmadurga `_ - `Kjwon15 `_ diff --git a/telegram/ext/commandhandler.py b/telegram/ext/commandhandler.py index b9e7c4b6a91..e7a1f66bf4e 100644 --- a/telegram/ext/commandhandler.py +++ b/telegram/ext/commandhandler.py @@ -17,6 +17,7 @@ # You should have received a copy of the GNU Lesser Public License # along with this program. If not, see [http://www.gnu.org/licenses/]. """ This module contains the CommandHandler class """ +import warnings from .handler import Handler from telegram import Update @@ -34,6 +35,10 @@ class CommandHandler(Handler): callback (function): A function that takes ``bot, update`` as positional arguments. It will be called when the ``check_update`` has determined that an update should be processed by this handler. + filters (telegram.ext.BaseFilter): A filter inheriting from + :class:`telegram.ext.filters.BaseFilter`. Standard filters can be found in + :class:`telegram.ext.filters.Filters`. Filters can be combined using bitwise + operators (& for and, | for or). allow_edited (Optional[bool]): If the handler should also accept edited messages. Default is ``False`` pass_args (optional[bool]): If the handler should be passed the @@ -62,6 +67,7 @@ class CommandHandler(Handler): def __init__(self, command, callback, + filters=None, allow_edited=False, pass_args=False, pass_update_queue=False, @@ -75,9 +81,17 @@ def __init__(self, pass_user_data=pass_user_data, pass_chat_data=pass_chat_data) self.command = command + self.filters = filters self.allow_edited = allow_edited self.pass_args = pass_args + # We put this up here instead of with the rest of checking code + # in check_update since we don't wanna spam a ton + if isinstance(self.filters, list): + warnings.warn('Using a list of filters in MessageHandler is getting ' + 'deprecated, please use bitwise operators (& and |) ' + 'instead. More info: https://git.io/vPTbc.') + def check_update(self, update): if (isinstance(update, Update) and (update.message or update.edited_message and self.allow_edited)): @@ -87,8 +101,16 @@ def check_update(self, update): command = message.text[1:].split(' ')[0].split('@') command.append( update.message.bot.username) # in case the command was send without a username - return (message.text.startswith('/') and command[0] == self.command - and command[1] == update.message.bot.username) + + if self.filters is None: + res = True + elif isinstance(self.filters, list): + res = any(func(message) for func in self.filters) + else: + res = self.filters(message) + + return res and (message.text.startswith('/') and command[0] == self.command + and command[1] == update.message.bot.username) else: return False diff --git a/tests/test_updater.py b/tests/test_updater.py index e271e4dfe3d..541ac2297de 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -268,6 +268,34 @@ def test_addRemoveTelegramCommandHandler(self): sleep(.1) self.assertTrue(None is self.received_message) + def test_filterPassTelegramCommandHandler(self): + self._setup_updater('', messages=0) + d = self.updater.dispatcher + handler = CommandHandler('test', self.telegramHandlerTest, lambda msg: True) + self.updater.dispatcher.add_handler(handler) + user = User(first_name="singelton", id=404) + bot = self.updater.bot + queue = self.updater.start_polling(0.01) + + message = Message(0, user, None, None, text="/test", bot=bot) + queue.put(Update(update_id=0, message=message)) + sleep(.1) + self.assertEqual(self.received_message, '/test') + + def test_filterNotPassTelegramCommandHandler(self): + self._setup_updater('', messages=0) + d = self.updater.dispatcher + handler = CommandHandler('test', self.telegramHandlerTest, lambda msg: False) + self.updater.dispatcher.add_handler(handler) + user = User(first_name="singelton", id=404) + bot = self.updater.bot + queue = self.updater.start_polling(0.01) + + message = Message(0, user, None, None, text="/test", bot=bot) + queue.put(Update(update_id=0, message=message)) + sleep(.1) + self.assertTrue(None is self.received_message) + def test_addRemoveStringRegexHandler(self): self._setup_updater('', messages=0) d = self.updater.dispatcher pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy