diff --git a/telegram/_chat.py b/telegram/_chat.py index 1b6a18d1f..f562e9ffb 100644 --- a/telegram/_chat.py +++ b/telegram/_chat.py @@ -388,10 +388,24 @@ class Chat(TelegramObject): self._freeze() + @property + def effective_name(self) -> Optional[str]: + """ + :obj:`str`: Convenience property. Gives :attr:`title` if not :obj:`None`, + else :attr:`full_name` if not :obj:`None`. + + .. versionadded:: 20.1 + """ + if self.title is not None: + return self.title + if self.full_name is not None: + return self.full_name + return None + @property def full_name(self) -> Optional[str]: """ - :obj:`str`: Convenience property. If :attr:`first_name` is not :obj:`None` gives, + :obj:`str`: Convenience property. If :attr:`first_name` is not :obj:`None`, gives :attr:`first_name` followed by (if available) :attr:`last_name`. Note: diff --git a/tests/test_chat.py b/tests/test_chat.py index ca700cc73..f32cc60b4 100644 --- a/tests/test_chat.py +++ b/tests/test_chat.py @@ -212,6 +212,16 @@ class TestChat: ) assert chat.full_name is None + def test_effective_name(self): + chat = Chat(id=1, type=Chat.PRIVATE, first_name="first\u2022name") + assert chat.effective_name == "first\u2022name" + chat = Chat(id=1, type=Chat.GROUP, title="group") + assert chat.effective_name == "group" + chat = Chat(id=1, type=Chat.GROUP, first_name="first\u2022name", title="group") + assert chat.effective_name == "group" + chat = Chat(id=1, type=Chat.GROUP) + assert chat.effective_name is None + async def test_send_action(self, monkeypatch, chat): async def make_assertion(*_, **kwargs): id_ = kwargs["chat_id"] == chat.id