mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2025-01-10 12:02:39 +01:00
Allow filters to be passed without list.
Also deprecates actually using a list.
This commit is contained in:
parent
79e065a730
commit
79bdfe4c5d
1 changed files with 16 additions and 7 deletions
|
@ -17,6 +17,7 @@
|
||||||
# You should have received a copy of the GNU Lesser Public License
|
# You should have received a copy of the GNU Lesser Public License
|
||||||
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
||||||
""" This module contains the MessageHandler class """
|
""" This module contains the MessageHandler class """
|
||||||
|
import warnings
|
||||||
|
|
||||||
from .handler import Handler
|
from .handler import Handler
|
||||||
from telegram import Update
|
from telegram import Update
|
||||||
|
@ -30,12 +31,10 @@ class MessageHandler(Handler):
|
||||||
updates.
|
updates.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
filters (list[function]): A list of filter functions. Standard filters
|
filters (telegram.ext.BaseFilter): A filter inheriting from
|
||||||
can be found in the Filters class above.
|
:class:`telegram.filters.BaseFilter`. Standard filters can be found in
|
||||||
| Each `function` takes ``Update`` as arg and returns ``bool``.
|
:class:`telegram.filters.Filters`. Filters can be combined using bitwise
|
||||||
| All messages that match at least one of those filters will be
|
operators (& for and, | for or).
|
||||||
accepted. If ``bool(filters)`` evaluates to ``False``, messages are
|
|
||||||
not filtered.
|
|
||||||
callback (function): A function that takes ``bot, update`` as
|
callback (function): A function that takes ``bot, update`` as
|
||||||
positional arguments. It will be called when the ``check_update``
|
positional arguments. It will be called when the ``check_update``
|
||||||
has determined that an update should be processed by this handler.
|
has determined that an update should be processed by this handler.
|
||||||
|
@ -57,6 +56,13 @@ class MessageHandler(Handler):
|
||||||
self.filters = filters
|
self.filters = filters
|
||||||
self.allow_edited = allow_edited
|
self.allow_edited = allow_edited
|
||||||
|
|
||||||
|
# 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):
|
def check_update(self, update):
|
||||||
if (isinstance(update, Update)
|
if (isinstance(update, Update)
|
||||||
and (update.message or update.edited_message and self.allow_edited)):
|
and (update.message or update.edited_message and self.allow_edited)):
|
||||||
|
@ -66,7 +72,10 @@ class MessageHandler(Handler):
|
||||||
|
|
||||||
else:
|
else:
|
||||||
message = update.message or update.edited_message
|
message = update.message or update.edited_message
|
||||||
res = any(func(message) for func in self.filters)
|
if isinstance(self.filters, list):
|
||||||
|
res = any(func(message) for func in self.filters)
|
||||||
|
else:
|
||||||
|
res = self.filters(message)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
res = False
|
res = False
|
||||||
|
|
Loading…
Reference in a new issue