enh: private chat links

This commit is contained in:
uburuntu 2019-11-10 00:34:20 +03:00
parent 9d94ac62c9
commit 1863ac8571
2 changed files with 21 additions and 4 deletions

View file

@ -63,6 +63,16 @@ class Chat(base.TelegramObject):
return f"tg://user?id={self.id}"
@property
def shifted_id(self) -> int:
"""
Get shifted id of chat, e.g. for private links
For example: -1001122334455 -> 1122334455
"""
shift = -1000000000000
return shift - self.id
def get_mention(self, name=None, as_html=False):
if name is None:
name = self.mention

View file

@ -258,12 +258,19 @@ class Message(base.TelegramObject):
:return: str
"""
if self.chat.type not in [ChatType.SUPER_GROUP, ChatType.CHANNEL]:
if ChatType.is_private(self.chat):
raise TypeError('Invalid chat type!')
elif not self.chat.username:
raise TypeError('This chat does not have @username')
return f"https://t.me/{self.chat.username}/{self.message_id}"
url = 'https://t.me/'
if self.chat.username:
# Generates public link
url += f'{self.chat.username}/'
else:
# Generates private link available for chat members
url += f'c/{self.chat.shifted_id}/'
url += f'{self.message_id}'
return url
def link(self, text, as_html=True) -> str:
"""