From 6fdab3a58d438cf998e0bde6b77f44d37bfef058 Mon Sep 17 00:00:00 2001 From: karin0 Date: Thu, 21 May 2026 06:44:41 +0900 Subject: [PATCH] Check `channel_post` and `edited_channel_post` in `Update.effective_user` (#5237) --- AUTHORS.rst | 1 + changes/unreleased/5237.effective-user-fix.toml | 5 +++++ src/telegram/_update.py | 11 ++++++++--- tests/ext/test_messagehandler.py | 17 +---------------- tests/test_update.py | 12 ++++++++++++ 5 files changed, 27 insertions(+), 19 deletions(-) create mode 100644 changes/unreleased/5237.effective-user-fix.toml diff --git a/AUTHORS.rst b/AUTHORS.rst index 299f88ad3..848677d08 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -75,6 +75,7 @@ The following wonderful people contributed directly or indirectly to this projec - `jossalgon `_ - `JRoot3D `_ - `Juan Cuevas ` +- `karin0 `_ - `kenjitagawa `_ - `kennethcheo `_ - `Kirill Vasin `_ diff --git a/changes/unreleased/5237.effective-user-fix.toml b/changes/unreleased/5237.effective-user-fix.toml new file mode 100644 index 000000000..ae391c5d9 --- /dev/null +++ b/changes/unreleased/5237.effective-user-fix.toml @@ -0,0 +1,5 @@ +bugfixes = "`Update.effective_user` now checks for `channel_post` and `edited_channel_post`" +[[pull_requests]] +uid = "5237" +author_uids = ["karin0"] +closes_threads = ["5236"] diff --git a/src/telegram/_update.py b/src/telegram/_update.py index a8b8668ad..dc2a7f2f1 100644 --- a/src/telegram/_update.py +++ b/src/telegram/_update.py @@ -500,8 +500,6 @@ class Update(TelegramObject): is. If no user is associated with this update, this gives :obj:`None`. This is the case if any of - * :attr:`channel_post` - * :attr:`edited_channel_post` * :attr:`poll` * :attr:`chat_boost` * :attr:`removed_chat_boost` @@ -518,7 +516,8 @@ class Update(TelegramObject): This property now also considers :attr:`purchased_paid_media`. .. versionchanged:: NEXT.VERSION - This property now also considers :attr:`managed_bot`. + This property now also considers :attr:`managed_bot`, :attr:`channel_post` + and :attr:`edited_channel_post`. Example: * If :attr:`message` is present, this will give @@ -537,6 +536,12 @@ class Update(TelegramObject): elif self.edited_message: user = self.edited_message.from_user + elif self.channel_post: + user = self.channel_post.from_user + + elif self.edited_channel_post: + user = self.edited_channel_post.from_user + elif self.inline_query: user = self.inline_query.from_user diff --git a/tests/ext/test_messagehandler.py b/tests/ext/test_messagehandler.py index aeb2e1b03..8df5de7d1 100644 --- a/tests/ext/test_messagehandler.py +++ b/tests/ext/test_messagehandler.py @@ -95,22 +95,7 @@ class TestMessageHandler: and isinstance(context.job_queue, JobQueue) and isinstance(context.chat_data, dict) and isinstance(context.bot_data, dict) - and ( - ( - isinstance(context.user_data, dict) - and ( - isinstance(update.message, Message) - or isinstance(update.edited_message, Message) - ) - ) - or ( - context.user_data is None - and ( - isinstance(update.channel_post, Message) - or isinstance(update.edited_channel_post, Message) - ) - ) - ) + and isinstance(context.user_data, dict) ) def callback_regex1(self, update, context): diff --git a/tests/test_update.py b/tests/test_update.py index 8a8e0c485..7b034090a 100644 --- a/tests/test_update.py +++ b/tests/test_update.py @@ -401,6 +401,18 @@ class TestUpdateWithoutRequest(UpdateTestBase): cached = update.effective_sender assert cached is sender + def test_effective_sender_signed_channel_post(self): + # channel_post with signatures can have its from_user + user = User(1, "", False) + post = Message( + 1, dtm.datetime.utcnow(), Chat(1, ""), author_signature="", from_user=user, text="Text" + ) + update = Update(update_id=1, channel_post=post) + assert update.effective_sender == update.effective_user == user + + update = Update(update_id=2, edited_channel_post=post) + assert update.effective_sender == update.effective_user == user + def test_effective_message(self, update): # Test that it's sometimes None per docstring eff_message = update.effective_message