Allow CallbackQueryHandler in ConversationHandler with per_mess… (#561)

* 🐛 Allow CallbackQueryHandler in ConversationHandler with per_message=False

but show a warning #556

*  warning logs instead of ValueErrors

 #556
This commit is contained in:
Jannes Höke 2017-04-29 14:15:17 +02:00 committed by Noam Meltzer
parent fe5ae8ed84
commit c7dbdce3dc
2 changed files with 11 additions and 26 deletions

View file

@ -122,6 +122,10 @@ class ConversationHandler(Handler):
if not any((self.per_user, self.per_chat, self.per_message)):
raise ValueError("'per_user', 'per_chat' and 'per_message' can't all be 'False'")
if self.per_message and not self.per_chat:
logging.warning("If 'per_message=True' is used, 'per_chat=True' should also be used, "
"since message IDs are not globally unique.")
all_handlers = list()
all_handlers.extend(entry_points)
all_handlers.extend(fallbacks)
@ -132,17 +136,20 @@ class ConversationHandler(Handler):
if self.per_message:
for handler in all_handlers:
if not isinstance(handler, CallbackQueryHandler):
raise ValueError("If 'per_message=True', all entry points and state handlers"
" must be 'CallbackQueryHandler'")
logging.warning("If 'per_message=True', all entry points and state handlers"
" must be 'CallbackQueryHandler', since no other handlers "
"have a message context.")
else:
for handler in all_handlers:
if isinstance(handler, CallbackQueryHandler):
raise ValueError("If 'per_message=False', 'CallbackQueryHandler' doesn't work")
logging.warning("If 'per_message=False', 'CallbackQueryHandler' will not be "
"tracked for every message.")
if self.per_chat:
for handler in all_handlers:
if isinstance(handler, (InlineQueryHandler, ChosenInlineResultHandler)):
raise ValueError("If 'per_chat=True', 'InlineQueryHandler' doesn't work")
logging.warning("If 'per_chat=True', 'InlineQueryHandler' can not be used, "
"since inline queries have no chat context.")
def _get_key(self, update):
chat = update.effective_chat

View file

@ -280,28 +280,6 @@ class ConversationHandlerTest(BaseTest, unittest.TestCase):
sleep(.1)
self.assertEquals(handler.conversations[(self.group.id, user.id, message.message_id)], 2)
def test_illegal_handlers(self):
with self.assertRaises(ValueError):
ConversationHandler(
entry_points=[CommandHandler('/test', lambda bot, update: None)],
states={},
fallbacks=[],
per_message=True)
with self.assertRaises(ValueError):
ConversationHandler(
entry_points=[CallbackQueryHandler(lambda bot, update: None)],
states={},
fallbacks=[],
per_message=False)
with self.assertRaises(ValueError):
ConversationHandler(
entry_points=[InlineQueryHandler(lambda bot, update: None)],
states={},
fallbacks=[],
per_chat=True)
def test_endOnFirstMessage(self):
self._setup_updater('', messages=0)
d = self.updater.dispatcher