diff --git a/telegram/chat.py b/telegram/chat.py index d6f7e0e1a..d5bc3da2b 100644 --- a/telegram/chat.py +++ b/telegram/chat.py @@ -116,6 +116,14 @@ class Chat(TelegramObject): self.bot = bot self._id_attrs = (self.id,) + @property + def link(self): + """:obj:`str`: Convenience property. If the chat has a :attr:`username`, returns a t.me + link of the chat.""" + if self.username: + return "https://t.me/{}".format(self.username) + return None + @classmethod def de_json(cls, data, bot): if not data: diff --git a/telegram/message.py b/telegram/message.py index 38a30f962..14d579932 100644 --- a/telegram/message.py +++ b/telegram/message.py @@ -303,6 +303,14 @@ class Message(TelegramObject): """:obj:`int`: Shortcut for :attr:`telegram.Chat.id` for :attr:`chat`.""" return self.chat.id + @property + def link(self): + """:obj:`str`: Convenience property. If the chat of the message is a supergroup or a + channel and has a :attr:`Chat.username`, returns a t.me link of the message.""" + if self.chat.type in (Chat.SUPERGROUP, Chat.CHANNEL) and self.chat.username: + return "https://t.me/{}/{}".format(self.chat.username, self.message_id) + return None + @classmethod def de_json(cls, data, bot): if not data: diff --git a/telegram/user.py b/telegram/user.py index f886c35e7..3c5b4b601 100644 --- a/telegram/user.py +++ b/telegram/user.py @@ -71,26 +71,30 @@ class User(TelegramObject): @property def name(self): - """ - :obj:`str`: Convenience property. If available, returns the user's :attr:`username` - prefixed with "@". If :attr:`username` is not available, returns :attr:`full_name`. - - """ + """:obj:`str`: Convenience property. If available, returns the user's :attr:`username` + prefixed with "@". If :attr:`username` is not available, returns :attr:`full_name`.""" if self.username: return '@{}'.format(self.username) return self.full_name @property def full_name(self): - """ - :obj:`str`: Convenience property. The user's :attr:`first_name`, followed by (if available) - :attr:`last_name`. + """:obj:`str`: Convenience property. The user's :attr:`first_name`, followed by (if + available) :attr:`last_name`.""" - """ if self.last_name: return u'{} {}'.format(self.first_name, self.last_name) return self.first_name + @property + def link(self): + """:obj:`str`: Convenience property. If :attr:`username` is available, returns a t.me link + of the user.""" + + if self.username: + return "https://t.me/{}".format(self.username) + return None + @classmethod def de_json(cls, data, bot): if not data: @@ -124,28 +128,28 @@ class User(TelegramObject): def mention_markdown(self, name=None): """ Args: - name (:obj:`str`): If provided, will overwrite the user's name. + name (:obj:`str`): The name used as a link for the user. Defaults to :attr:`full_name`. Returns: :obj:`str`: The inline mention for the user as markdown. + """ - if not name: - return util_mention_markdown(self.id, self.name) - else: + if name: return util_mention_markdown(self.id, name) + return util_mention_markdown(self.id, self.full_name) def mention_html(self, name=None): """ Args: - name (:obj:`str`): If provided, will overwrite the user's name. + name (:obj:`str`): The name used as a link for the user. Defaults to :attr:`full_name`. Returns: :obj:`str`: The inline mention for the user as HTML. + """ - if not name: - return util_mention_html(self.id, self.name) - else: + if name: return util_mention_html(self.id, name) + return util_mention_html(self.id, self.full_name) def send_message(self, *args, **kwargs): """Shortcut for:: diff --git a/tests/test_chat.py b/tests/test_chat.py index ce686e42e..f88162de0 100644 --- a/tests/test_chat.py +++ b/tests/test_chat.py @@ -25,7 +25,7 @@ from telegram import User @pytest.fixture(scope='class') def chat(bot): - return Chat(TestChat.id, TestChat.title, TestChat.type, + return Chat(TestChat.id, TestChat.title, TestChat.type, username=TestChat.username, all_members_are_administrators=TestChat.all_members_are_administrators, bot=bot, sticker_set_name=TestChat.sticker_set_name, can_set_sticker_set=TestChat.can_set_sticker_set) @@ -35,6 +35,7 @@ class TestChat(object): id = -28767330 title = 'ToledosPalaceBot - Group' type = 'group' + username = 'username' all_members_are_administrators = False sticker_set_name = 'stickers' can_set_sticker_set = False @@ -44,6 +45,7 @@ class TestChat(object): 'id': self.id, 'title': self.title, 'type': self.type, + 'username': self.username, 'all_members_are_administrators': self.all_members_are_administrators, 'sticker_set_name': self.sticker_set_name, 'can_set_sticker_set': self.can_set_sticker_set @@ -53,6 +55,7 @@ class TestChat(object): assert chat.id == self.id assert chat.title == self.title assert chat.type == self.type + assert chat.username == self.username assert chat.all_members_are_administrators == self.all_members_are_administrators assert chat.sticker_set_name == self.sticker_set_name assert chat.can_set_sticker_set == self.can_set_sticker_set @@ -64,8 +67,14 @@ class TestChat(object): assert chat_dict['id'] == chat.id assert chat_dict['title'] == chat.title assert chat_dict['type'] == chat.type + assert chat_dict['username'] == chat.username assert chat_dict['all_members_are_administrators'] == chat.all_members_are_administrators + def test_link(self, chat): + assert chat.link == 'https://t.me/{}'.format(chat.username) + chat.username = None + assert chat.link is None + def test_send_action(self, monkeypatch, chat): def test(*args, **kwargs): id = args[1] == chat.id diff --git a/tests/test_message.py b/tests/test_message.py index b09971ff7..2f1a04fb7 100644 --- a/tests/test_message.py +++ b/tests/test_message.py @@ -282,6 +282,18 @@ class TestMessage(object): def test_chat_id(self, message): assert message.chat_id == message.chat.id + def test_link(self, message): + assert message.link is None + message.chat.username = 'username' + message.chat.type = 'supergroup' + assert message.link == 'https://t.me/{}/{}'.format(message.chat.username, + message.message_id) + message.chat.type = 'channel' + assert message.link == 'https://t.me/{}/{}'.format(message.chat.username, + message.message_id) + message.chat.type = 'private' + assert message.link is None + def test_effective_attachment(self, message_params): for i in ('audio', 'game', 'document', 'photo', 'sticker', 'video', 'voice', 'video_note', 'contact', 'location', 'venue', 'invoice', 'invoice', 'successful_payment'): diff --git a/tests/test_user.py b/tests/test_user.py index 216ed33d8..8fecfbf06 100644 --- a/tests/test_user.py +++ b/tests/test_user.py @@ -96,6 +96,11 @@ class TestUser(object): user.last_name = None assert user.full_name == u'first\u2022name' + def test_link(self, user): + assert user.link == 'https://t.me/{}'.format(user.username) + user.username = None + assert user.link is None + def test_get_profile_photos(self, monkeypatch, user): def test(_, *args, **kwargs): return args[0] == user.id