Skip to content

Commit 063704c

Browse files
nmlorgtsnoam
authored andcommitted
Explicitly check update.effective_chat in ConversationHandler.check_update (python-telegram-bot#959)
Fixes python-telegram-bot#927
1 parent 62e76f1 commit 063704c

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

telegram/ext/conversationhandler.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,11 @@ def check_update(self, update):
206206
207207
"""
208208
# Ignore messages in channels
209-
if (not isinstance(update, Update) or update.channel_post or self.per_chat
210-
and (update.inline_query or update.chosen_inline_result) or self.per_message
211-
and not update.callback_query or update.callback_query and self.per_chat
212-
and not update.callback_query.message):
209+
if (not isinstance(update, Update) or
210+
update.channel_post or
211+
self.per_chat and not update.effective_chat or
212+
self.per_message and not update.callback_query or
213+
update.callback_query and self.per_chat and not update.callback_query.message):
213214
return False
214215

215216
key = self._get_key(update)

tests/test_conversationhandler.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020

2121
import pytest
2222

23-
from telegram import Update, Message, User, Chat, CallbackQuery
23+
from telegram import (CallbackQuery, Chat, ChosenInlineResult, InlineQuery, Message,
24+
PreCheckoutQuery, ShippingQuery, Update, User)
2425
from telegram.ext import (ConversationHandler, CommandHandler, CallbackQueryHandler)
2526

2627

@@ -277,3 +278,19 @@ def test_channel_message_without_chat(self, bot):
277278
message = Message(0, None, None, Chat(0, Chat.CHANNEL, 'Misses Test'), bot=bot)
278279
update = Update(0, message=message)
279280
assert not handler.check_update(update)
281+
282+
def test_all_update_types(self, dp, bot, user1):
283+
handler = ConversationHandler(entry_points=[CommandHandler('start', self.start_end)],
284+
states={}, fallbacks=[])
285+
message = Message(0, user1, None, self.group, text='ignore', bot=bot)
286+
callback_query = CallbackQuery(0, user1, None, message=message, data='data', bot=bot)
287+
chosen_inline_result = ChosenInlineResult(0, user1, 'query', bot=bot)
288+
inline_query = InlineQuery(0, user1, 'query', 0, bot=bot)
289+
pre_checkout_query = PreCheckoutQuery(0, user1, 'USD', 100, [], bot=bot)
290+
shipping_query = ShippingQuery(0, user1, [], None, bot=bot)
291+
assert not handler.check_update(Update(0, callback_query=callback_query))
292+
assert not handler.check_update(Update(0, chosen_inline_result=chosen_inline_result))
293+
assert not handler.check_update(Update(0, inline_query=inline_query))
294+
assert not handler.check_update(Update(0, message=message))
295+
assert not handler.check_update(Update(0, pre_checkout_query=pre_checkout_query))
296+
assert not handler.check_update(Update(0, shipping_query=shipping_query))

0 commit comments

Comments
 (0)