Allow filters to be passed without list.

Also deprecates actually using a list.
This commit is contained in:
Jacob Bom 2016-09-29 19:10:22 +02:00
parent 79e065a730
commit 79bdfe4c5d

View file

@ -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