Make MessageHandler filter for Filters.update first (#2085)

* Tweak MessageHandlers filters

* Improve test
This commit is contained in:
Bibo-Joshi 2020-09-14 17:55:01 +02:00 committed by GitHub
parent bb34c79909
commit 49c0c9e4d1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 4 deletions

View file

@ -126,9 +126,8 @@ class MessageHandler(Handler):
if message_updates is False and channel_post_updates is False and edited_updates is False:
raise ValueError(
'message_updates, channel_post_updates and edited_updates are all False')
self.filters = filters
if self.filters is not None:
self.filters &= Filters.update
if filters is not None:
self.filters = Filters.update & filters
else:
self.filters = Filters.update
if message_updates is not None:

View file

@ -24,7 +24,7 @@ from telegram.utils.deprecate import TelegramDeprecationWarning
from telegram import (Message, Update, Chat, Bot, User, CallbackQuery, InlineQuery,
ChosenInlineResult, ShippingQuery, PreCheckoutQuery)
from telegram.ext import Filters, MessageHandler, CallbackContext, JobQueue
from telegram.ext import Filters, MessageHandler, CallbackContext, JobQueue, BaseFilter
message = Message(1, User(1, '', False), None, Chat(1, ''), text='Text')
@ -161,6 +161,24 @@ class TestMessageHandler:
message.chat.type = 'private'
assert not handler.check_update(Update(0, message))
def test_callback_query_with_filter(self, message):
class TestFilter(BaseFilter):
update_filter = True
flag = False
def filter(self, u):
self.flag = True
test_filter = TestFilter()
handler = MessageHandler(test_filter, self.callback_basic)
update = Update(1, callback_query=CallbackQuery(1, None, None, message=message))
assert update.effective_message
assert not handler.check_update(update)
assert not test_filter.flag
def test_specific_filters(self, message):
f = (~Filters.update.messages
& ~Filters.update.channel_post