mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-12-22 14:35:00 +01:00
Add filters to commandHandler (#536)
* Add filters to commandHandler * Add commandHandler tests with filters * Add myself to authors
This commit is contained in:
parent
cc73469dab
commit
e78d11a99b
3 changed files with 53 additions and 2 deletions
|
@ -21,6 +21,7 @@ The following wonderful people contributed directly or indirectly to this projec
|
|||
- `Jacob Bom <https://github.com/bomjacob>`_
|
||||
- `JASON0916 <https://github.com/JASON0916>`_
|
||||
- `jh0ker <https://github.com/jh0ker>`_
|
||||
- `jossalgon <https://github.com/jossalgon>`_
|
||||
- `JRoot3D <https://github.com/JRoot3D>`_
|
||||
- `jlmadurga <https://github.com/jlmadurga>`_
|
||||
- `Kjwon15 <https://github.com/kjwon15>`_
|
||||
|
|
|
@ -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 @@ class CommandHandler(Handler):
|
|||
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 @@ class CommandHandler(Handler):
|
|||
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
|
||||
|
||||
|
|
|
@ -268,6 +268,34 @@ class UpdaterTest(BaseTest, unittest.TestCase):
|
|||
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
|
||||
|
|
Loading…
Reference in a new issue