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._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:

View file

@ -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:

View file

@ -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::

View file

@ -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

View file

@ -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'):

View file

@ -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