fixing message.link (#1741)

* fixing message.link

* improving if clause
This commit is contained in:
Poolitzer 2020-02-02 11:27:53 -08:00 committed by GitHub
parent a397e6c3c6
commit 423794f473
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 11 deletions

View file

@ -339,18 +339,13 @@ class Message(TelegramObject):
@property
def link(self):
""":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:
a private chat or normal group, returns a t.me link of the message."""
if self.chat.type not in [Chat.PRIVATE, Chat.GROUP]:
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)
# Get rid of leading -100 for supergroups
to_link = "c/{}".format(str(self.chat.id)[4:])
return "https://t.me/{}/{}".format(to_link, self.message_id)
return None

View file

@ -311,8 +311,7 @@ class TestMessage(object):
message.message_id)
@pytest.mark.parametrize('type, id', argvalues=[
(Chat.CHANNEL, -1003), (Chat.SUPERGROUP, -1003), (Chat.GROUP, -3)
])
(Chat.CHANNEL, -1003), (Chat.SUPERGROUP, -1003)])
def test_link_with_id(self, message, type, id):
message.chat.username = None
message.chat.id = id
@ -328,6 +327,8 @@ class TestMessage(object):
message.chat.id = id
message.chat.username = username
assert message.link is None
message.chat.type = Chat.GROUP
assert message.link is None
def test_effective_attachment(self, message_params):
for i in ('audio', 'game', 'document', 'animation', 'photo', 'sticker', 'video', 'voice',