mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2025-03-17 04:39:55 +01:00
add private /c links to message.links object (#1619)
* add private /c links to message.links object * fixing ids for basic groups * fixing ids for non basic chats and the test * Improve tests for Message.link * Simplify id_to_link
This commit is contained in:
parent
5e8a961669
commit
2c67a9833b
2 changed files with 35 additions and 11 deletions
|
@ -338,10 +338,20 @@ class Message(TelegramObject):
|
|||
|
||||
@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)
|
||||
""":obj:`str`: Convenience property. If the chat of the message is not
|
||||
a private chat, returns a t.me link of the message."""
|
||||
if self.chat.type != Chat.PRIVATE:
|
||||
if self.chat.username:
|
||||
to_link = self.chat.username
|
||||
else:
|
||||
if self.chat.type != Chat.GROUP:
|
||||
# Get rid of leading -100 for supergroups
|
||||
id_to_link = str(self.chat.id)[4:]
|
||||
else:
|
||||
# Get rid of leading minus for regular groups
|
||||
id_to_link = str(self.chat.id)[1:]
|
||||
to_link = "c/{}".format(id_to_link)
|
||||
return "https://t.me/{}/{}".format(to_link, self.message_id)
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
|
|
|
@ -303,16 +303,30 @@ 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
|
||||
@pytest.mark.parametrize('type', argvalues=[Chat.SUPERGROUP, Chat.CHANNEL])
|
||||
def test_link_with_username(self, message, type):
|
||||
message.chat.username = 'username'
|
||||
message.chat.type = 'supergroup'
|
||||
message.chat.type = type
|
||||
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'
|
||||
|
||||
@pytest.mark.parametrize('type, id', argvalues=[
|
||||
(Chat.CHANNEL, -1003), (Chat.SUPERGROUP, -1003), (Chat.GROUP, -3)
|
||||
])
|
||||
def test_link_with_id(self, message, type, id):
|
||||
message.chat.username = None
|
||||
message.chat.id = id
|
||||
message.chat.type = type
|
||||
# The leading - for group ids/ -100 for supergroup ids isn't supposed to be in the link
|
||||
assert message.link == 'https://t.me/c/{}/{}'.format(3, message.message_id)
|
||||
|
||||
@pytest.mark.parametrize('id, username', argvalues=[
|
||||
(None, 'username'), (-3, None)
|
||||
])
|
||||
def test_link_private_chats(self, message, id, username):
|
||||
message.chat.type = Chat.PRIVATE
|
||||
message.chat.id = id
|
||||
message.chat.username = username
|
||||
assert message.link is None
|
||||
|
||||
def test_effective_attachment(self, message_params):
|
||||
|
|
Loading…
Add table
Reference in a new issue