From e2d5bf20651af6fff796c7c282de26bef7f83f81 Mon Sep 17 00:00:00 2001 From: Daniel Reed Date: Mon, 1 Jan 2018 10:13:15 -0800 Subject: [PATCH 1/2] Reformat check_update's filter logic to group related checks by line. --- telegram/ext/conversationhandler.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/telegram/ext/conversationhandler.py b/telegram/ext/conversationhandler.py index a144420898b..cb63f32a4a5 100644 --- a/telegram/ext/conversationhandler.py +++ b/telegram/ext/conversationhandler.py @@ -206,10 +206,11 @@ def check_update(self, update): """ # Ignore messages in channels - if (not isinstance(update, Update) or update.channel_post or self.per_chat - and (update.inline_query or update.chosen_inline_result) or self.per_message - and not update.callback_query or update.callback_query and self.per_chat - and not update.callback_query.message): + if (not isinstance(update, Update) or + update.channel_post or + self.per_chat and (update.inline_query or update.chosen_inline_result) or + self.per_message and not update.callback_query or + update.callback_query and self.per_chat and not update.callback_query.message): return False key = self._get_key(update) From 026e009dc25ebd674c41f7cb29b6d82c428044c5 Mon Sep 17 00:00:00 2001 From: Daniel Reed Date: Mon, 1 Jan 2018 10:50:23 -0800 Subject: [PATCH 2/2] I think this part of check_update was just trying to exclude updates without an effective_chat, so just go ahead and check for that explicitly. This should be pretty future proof. (I'm not sure what `update.callback_query and self.per_chat and not update.callback_query.message` is doing, though. _get_key only directly touches update.callback_query if self.per_message (not self.per_chat) is set, and only requires update.callback_query.message if not update.callback_query.inline_message_id. I'm not sure what the use case is, so just leaving it alone for now.) Add a new test_all_update_types which just runs through a quick list of update types. This is not future proof. Maybe add a list of all possible update types as Update.TYPES in a followup? --- telegram/ext/conversationhandler.py | 2 +- tests/test_conversationhandler.py | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/telegram/ext/conversationhandler.py b/telegram/ext/conversationhandler.py index cb63f32a4a5..c8589f190dc 100644 --- a/telegram/ext/conversationhandler.py +++ b/telegram/ext/conversationhandler.py @@ -208,7 +208,7 @@ def check_update(self, update): # Ignore messages in channels if (not isinstance(update, Update) or update.channel_post or - self.per_chat and (update.inline_query or update.chosen_inline_result) or + self.per_chat and not update.effective_chat or self.per_message and not update.callback_query or update.callback_query and self.per_chat and not update.callback_query.message): return False diff --git a/tests/test_conversationhandler.py b/tests/test_conversationhandler.py index d72b1eaf263..d0e29de4b94 100644 --- a/tests/test_conversationhandler.py +++ b/tests/test_conversationhandler.py @@ -20,7 +20,8 @@ import pytest -from telegram import Update, Message, User, Chat, CallbackQuery +from telegram import (CallbackQuery, Chat, ChosenInlineResult, InlineQuery, Message, + PreCheckoutQuery, ShippingQuery, Update, User) from telegram.ext import (ConversationHandler, CommandHandler, CallbackQueryHandler) @@ -277,3 +278,19 @@ def test_channel_message_without_chat(self, bot): message = Message(0, None, None, Chat(0, Chat.CHANNEL, 'Misses Test'), bot=bot) update = Update(0, message=message) assert not handler.check_update(update) + + def test_all_update_types(self, dp, bot, user1): + handler = ConversationHandler(entry_points=[CommandHandler('start', self.start_end)], + states={}, fallbacks=[]) + message = Message(0, user1, None, self.group, text='ignore', bot=bot) + callback_query = CallbackQuery(0, user1, None, message=message, data='data', bot=bot) + chosen_inline_result = ChosenInlineResult(0, user1, 'query', bot=bot) + inline_query = InlineQuery(0, user1, 'query', 0, bot=bot) + pre_checkout_query = PreCheckoutQuery(0, user1, 'USD', 100, [], bot=bot) + shipping_query = ShippingQuery(0, user1, [], None, bot=bot) + assert not handler.check_update(Update(0, callback_query=callback_query)) + assert not handler.check_update(Update(0, chosen_inline_result=chosen_inline_result)) + assert not handler.check_update(Update(0, inline_query=inline_query)) + assert not handler.check_update(Update(0, message=message)) + assert not handler.check_update(Update(0, pre_checkout_query=pre_checkout_query)) + assert not handler.check_update(Update(0, shipping_query=shipping_query)) 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