From 544a3fbf4855e82b4e940c8b59a6b91306129b27 Mon Sep 17 00:00:00 2001 From: Bibo-Joshi Date: Mon, 1 Feb 2021 19:26:03 +0100 Subject: [PATCH] ConversationHandler: Docs & edited_channel_post behavior (#2339) * Update docs & ignore edited channel posts * typo * Apply suggestions from code review Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com> * Update telegram/ext/conversationhandler.py * fix pre-commit Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com> --- telegram/ext/conversationhandler.py | 20 +++++++++++++++++--- tests/test_conversationhandler.py | 10 +++++++--- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/telegram/ext/conversationhandler.py b/telegram/ext/conversationhandler.py index 1dfd828a1..ae615935c 100644 --- a/telegram/ext/conversationhandler.py +++ b/telegram/ext/conversationhandler.py @@ -58,8 +58,22 @@ class _ConversationTimeoutContext: class ConversationHandler(Handler[Update]): """ - A handler to hold a conversation with a single user by managing four collections of other - handlers. + A handler to hold a conversation with a single or multiple users through Telegram updates by + managing four collections of other handlers. + + Note: + ``ConversationHandler`` will only accept updates that are (subclass-)instances of + :class:`telegram.Update`. This is, because depending on the :attr:`per_user` and + :attr:`per_chat` ``ConversationHandler`` relies on + :attr:`telegram.Update.effective_user` and/or :attr:`telegram.Update.effective_chat` in + order to determine which conversation an update should belong to. For ``per_message=True``, + ``ConversationHandler`` uses ``update.callback_query.message.message_id`` when + ``per_chat=True`` and ``update.callback_query.inline_message_id`` when ``per_chat=False``. + For a more detailed explanation, please see our `FAQ`_. + + Finally, ``ConversationHandler``, does *not* handle (edited) channel posts. + + .. _`FAQ`: https://git.io/JtcyU The first collection, a ``list`` named :attr:`entry_points`, is used to initiate the conversation, for example with a :class:`telegram.ext.CommandHandler` or @@ -424,7 +438,7 @@ class ConversationHandler(Handler[Update]): if not isinstance(update, Update): return None # Ignore messages in channels - if update.channel_post: + if update.channel_post or update.edited_channel_post: return None if self.per_chat and not update.effective_chat: return None diff --git a/tests/test_conversationhandler.py b/tests/test_conversationhandler.py index 4bb36c644..f8db5dafa 100644 --- a/tests/test_conversationhandler.py +++ b/tests/test_conversationhandler.py @@ -726,10 +726,14 @@ class TestConversationHandler: def test_channel_message_without_chat(self, bot): handler = ConversationHandler( - entry_points=[CommandHandler('start', self.start_end)], states={}, fallbacks=[] + entry_points=[MessageHandler(Filters.all, self.start_end)], states={}, fallbacks=[] ) - message = Message(0, None, None, Chat(0, Chat.CHANNEL, 'Misses Test'), bot=bot) - update = Update(0, message=message) + message = Message(0, date=None, chat=Chat(0, Chat.CHANNEL, 'Misses Test'), bot=bot) + + update = Update(0, channel_post=message) + assert not handler.check_update(update) + + update = Update(0, edited_channel_post=message) assert not handler.check_update(update) def test_all_update_types(self, dp, bot, user1):