Added always_private argument to url() method

There are some cases when you want to be sure that your link will work regardless of chat username. This change makes it so you can force creation of private link
This commit is contained in:
Evgeny Petrov 2020-05-17 15:38:21 +03:00 committed by GitHub
parent 5eecec7c25
commit 462524a1e5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -228,22 +228,23 @@ class Message(base.TelegramObject):
return self.parse_entities() return self.parse_entities()
@property @property
def url(self) -> str: def url(self, always_private=False) -> str:
""" """
Get URL for the message Get URL for the message
:param always_private: Force generate a private URL even when username is available
:return: str :return: str
""" """
if ChatType.is_private(self.chat): if ChatType.is_private(self.chat):
raise TypeError('Invalid chat type!') raise TypeError('Invalid chat type!')
url = 'https://t.me/' url = 'https://t.me/'
if self.chat.username: if not self.chat.username or always_private:
# Generates public link
url += f'{self.chat.username}/'
else:
# Generates private link available for chat members # Generates private link available for chat members
url += f'c/{self.chat.shifted_id}/' url += f'c/{self.chat.shifted_id}/'
else:
# Generates public link
url += f'{self.chat.username}/'
url += f'{self.message_id}' url += f'{self.message_id}'
return url return url