Add t.me links for User, Chat and Message if available and update User.mention_* (#1092)

* Add User.link and update User.mention_*
* Add Chat.link
* Add Message.link
* Link returns None on default
* Add test link
This commit is contained in:
Trainer Jono 2018-05-09 17:42:12 +08:00 committed by Eldinnie
parent 3d8abc184a
commit 94abf16a7f
6 changed files with 64 additions and 18 deletions

View file

@ -116,6 +116,14 @@ class Chat(TelegramObject):
self.bot = bot self.bot = bot
self._id_attrs = (self.id,) 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 @classmethod
def de_json(cls, data, bot): def de_json(cls, data, bot):
if not data: if not data:

View file

@ -303,6 +303,14 @@ class Message(TelegramObject):
""":obj:`int`: Shortcut for :attr:`telegram.Chat.id` for :attr:`chat`.""" """:obj:`int`: Shortcut for :attr:`telegram.Chat.id` for :attr:`chat`."""
return self.chat.id 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 @classmethod
def de_json(cls, data, bot): def de_json(cls, data, bot):
if not data: if not data:

View file

@ -71,26 +71,30 @@ class User(TelegramObject):
@property @property
def name(self): def name(self):
""" """:obj:`str`: Convenience property. If available, returns the user's :attr:`username`
:obj:`str`: Convenience property. If available, returns the user's :attr:`username` prefixed with "@". If :attr:`username` is not available, returns :attr:`full_name`."""
prefixed with "@". If :attr:`username` is not available, returns :attr:`full_name`.
"""
if self.username: if self.username:
return '@{}'.format(self.username) return '@{}'.format(self.username)
return self.full_name return self.full_name
@property @property
def full_name(self): def full_name(self):
""" """:obj:`str`: Convenience property. The user's :attr:`first_name`, followed by (if
:obj:`str`: Convenience property. The user's :attr:`first_name`, followed by (if available) available) :attr:`last_name`."""
:attr:`last_name`.
"""
if self.last_name: if self.last_name:
return u'{} {}'.format(self.first_name, self.last_name) return u'{} {}'.format(self.first_name, self.last_name)
return self.first_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 @classmethod
def de_json(cls, data, bot): def de_json(cls, data, bot):
if not data: if not data:
@ -124,28 +128,28 @@ class User(TelegramObject):
def mention_markdown(self, name=None): def mention_markdown(self, name=None):
""" """
Args: 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: Returns:
:obj:`str`: The inline mention for the user as markdown. :obj:`str`: The inline mention for the user as markdown.
""" """
if not name: if name:
return util_mention_markdown(self.id, self.name)
else:
return util_mention_markdown(self.id, name) return util_mention_markdown(self.id, name)
return util_mention_markdown(self.id, self.full_name)
def mention_html(self, name=None): def mention_html(self, name=None):
""" """
Args: 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: Returns:
:obj:`str`: The inline mention for the user as HTML. :obj:`str`: The inline mention for the user as HTML.
""" """
if not name: if name:
return util_mention_html(self.id, self.name)
else:
return util_mention_html(self.id, name) return util_mention_html(self.id, name)
return util_mention_html(self.id, self.full_name)
def send_message(self, *args, **kwargs): def send_message(self, *args, **kwargs):
"""Shortcut for:: """Shortcut for::

View file

@ -25,7 +25,7 @@ from telegram import User
@pytest.fixture(scope='class') @pytest.fixture(scope='class')
def chat(bot): 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, all_members_are_administrators=TestChat.all_members_are_administrators,
bot=bot, sticker_set_name=TestChat.sticker_set_name, bot=bot, sticker_set_name=TestChat.sticker_set_name,
can_set_sticker_set=TestChat.can_set_sticker_set) can_set_sticker_set=TestChat.can_set_sticker_set)
@ -35,6 +35,7 @@ class TestChat(object):
id = -28767330 id = -28767330
title = 'ToledosPalaceBot - Group' title = 'ToledosPalaceBot - Group'
type = 'group' type = 'group'
username = 'username'
all_members_are_administrators = False all_members_are_administrators = False
sticker_set_name = 'stickers' sticker_set_name = 'stickers'
can_set_sticker_set = False can_set_sticker_set = False
@ -44,6 +45,7 @@ class TestChat(object):
'id': self.id, 'id': self.id,
'title': self.title, 'title': self.title,
'type': self.type, 'type': self.type,
'username': self.username,
'all_members_are_administrators': self.all_members_are_administrators, 'all_members_are_administrators': self.all_members_are_administrators,
'sticker_set_name': self.sticker_set_name, 'sticker_set_name': self.sticker_set_name,
'can_set_sticker_set': self.can_set_sticker_set 'can_set_sticker_set': self.can_set_sticker_set
@ -53,6 +55,7 @@ class TestChat(object):
assert chat.id == self.id assert chat.id == self.id
assert chat.title == self.title assert chat.title == self.title
assert chat.type == self.type assert chat.type == self.type
assert chat.username == self.username
assert chat.all_members_are_administrators == self.all_members_are_administrators assert chat.all_members_are_administrators == self.all_members_are_administrators
assert chat.sticker_set_name == self.sticker_set_name assert chat.sticker_set_name == self.sticker_set_name
assert chat.can_set_sticker_set == self.can_set_sticker_set 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['id'] == chat.id
assert chat_dict['title'] == chat.title assert chat_dict['title'] == chat.title
assert chat_dict['type'] == chat.type assert chat_dict['type'] == chat.type
assert chat_dict['username'] == chat.username
assert chat_dict['all_members_are_administrators'] == chat.all_members_are_administrators 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_send_action(self, monkeypatch, chat):
def test(*args, **kwargs): def test(*args, **kwargs):
id = args[1] == chat.id id = args[1] == chat.id

View file

@ -282,6 +282,18 @@ class TestMessage(object):
def test_chat_id(self, message): def test_chat_id(self, message):
assert message.chat_id == message.chat.id 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): def test_effective_attachment(self, message_params):
for i in ('audio', 'game', 'document', 'photo', 'sticker', 'video', 'voice', 'video_note', for i in ('audio', 'game', 'document', 'photo', 'sticker', 'video', 'voice', 'video_note',
'contact', 'location', 'venue', 'invoice', 'invoice', 'successful_payment'): 'contact', 'location', 'venue', 'invoice', 'invoice', 'successful_payment'):

View file

@ -96,6 +96,11 @@ class TestUser(object):
user.last_name = None user.last_name = None
assert user.full_name == u'first\u2022name' 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_get_profile_photos(self, monkeypatch, user):
def test(_, *args, **kwargs): def test(_, *args, **kwargs):
return args[0] == user.id return args[0] == user.id