Skip to content

Commit

Permalink
Only one warning for multiple CallbackqueryHandler's on ConversationH…
Browse files Browse the repository at this point in the history
…andler (#1319)
  • Loading branch information
Ambro17 authored and Eldinnie committed Feb 13, 2019
1 parent e54a318 commit d0936f7
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
3 changes: 3 additions & 0 deletions telegram/ext/conversationhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,17 +189,20 @@ def __init__(self,
logging.warning("If 'per_message=True', all entry points and state handlers"
" must be 'CallbackQueryHandler', since no other handlers "
"have a message context.")
break
else:
for handler in all_handlers:
if isinstance(handler, CallbackQueryHandler):
logging.warning("If 'per_message=False', 'CallbackQueryHandler' will not be "
"tracked for every message.")
break

if self.per_chat:
for handler in all_handlers:
if isinstance(handler, (InlineQueryHandler, ChosenInlineResultHandler)):
logging.warning("If 'per_chat=True', 'InlineQueryHandler' can not be used, "
"since inline queries have no chat context.")
break

def _get_key(self, update):
chat = update.effective_chat
Expand Down
59 changes: 58 additions & 1 deletion tests/test_conversationhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@

from telegram import (CallbackQuery, Chat, ChosenInlineResult, InlineQuery, Message,
PreCheckoutQuery, ShippingQuery, Update, User)
from telegram.ext import (ConversationHandler, CommandHandler, CallbackQueryHandler)
from telegram.ext import (ConversationHandler, CommandHandler, CallbackQueryHandler,
InlineQueryHandler)


@pytest.fixture(scope='class')
Expand Down Expand Up @@ -405,3 +406,59 @@ def test_conversation_timeout_two_users(self, dp, bot, user1, user2):
dp.job_queue.tick()
assert handler.conversations.get((self.group.id, user1.id)) is None
assert handler.conversations.get((self.group.id, user2.id)) is None

def test_per_message_warning_is_only_shown_once(self, caplog):
ConversationHandler(
entry_points=self.entry_points,
states={
self.THIRSTY: [CommandHandler('pourCoffee', self.drink)],
self.BREWING: [CommandHandler('startCoding', self.code)]
},
fallbacks=self.fallbacks,
per_message=True
)
assert len(caplog.messages) == 1
assert caplog.messages[0] == (
"If 'per_message=True', all entry points and state handlers"
" must be 'CallbackQueryHandler', since no other handlers"
" have a message context."
)

def test_per_message_false_warning_is_only_shown_once(self, caplog):
ConversationHandler(
entry_points=self.entry_points,
states={
self.THIRSTY: [CallbackQueryHandler(self.drink)],
self.BREWING: [CallbackQueryHandler(self.code)],
},
fallbacks=self.fallbacks,
per_message=False
)
assert len(caplog.messages) == 1
assert caplog.messages[0] == (
"If 'per_message=False', 'CallbackQueryHandler' will not be "
"tracked for every message."
)

def test_warnings_per_chat_is_only_shown_once(self, caplog):
def hello(bot, update):
return self.BREWING

def bye(bot, update):
return ConversationHandler.END

ConversationHandler(
entry_points=self.entry_points,
states={
self.THIRSTY: [InlineQueryHandler(hello)],
self.BREWING: [InlineQueryHandler(bye)]
},
fallbacks=self.fallbacks,
per_chat=True
)

assert len(caplog.messages) == 1
assert caplog.messages[0] == (
"If 'per_chat=True', 'InlineQueryHandler' can not be used,"
" since inline queries have no chat context."
)

0 comments on commit d0936f7

Please sign in to comment.