From b109523cdad8e31b455d1aa3c8e2ff3dbf7b370b Mon Sep 17 00:00:00 2001 From: Oleg A Date: Wed, 23 May 2018 00:10:34 +0300 Subject: [PATCH 1/3] Add .get_url() method Use this method to get chat link. Private chat returns user link. Other chat types return either username link (if they are public) or invite link (if they are private). --- aiogram/types/chat.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/aiogram/types/chat.py b/aiogram/types/chat.py index cb8d9af5..495e3ab4 100644 --- a/aiogram/types/chat.py +++ b/aiogram/types/chat.py @@ -64,6 +64,19 @@ class Chat(base.TelegramObject): if as_html: return markdown.hlink(name, self.user_url) return markdown.link(name, self.user_url) + + async def get_url(self): + """ + Use this method to get chat link. + Private chat returns user link. + Other chat types return either username link (if they are public) or invite link (if they are private). + :return: link + :rtype: :obj:`base.String` + """ + if self.type == ChatType.PRIVATE: + return f"tg://user?id={self.id}" + + return f'https://t.me/{self.username}' if self.username else await self.export_invite_link() async def set_photo(self, photo): """ From 09f540cc68ba78fa7f9fd470c76885aa97b48634 Mon Sep 17 00:00:00 2001 From: Oleg A Date: Fri, 25 May 2018 00:21:30 +0300 Subject: [PATCH 2/3] Update Chat method and get_link method fix --- aiogram/types/chat.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/aiogram/types/chat.py b/aiogram/types/chat.py index 495e3ab4..ae70c519 100644 --- a/aiogram/types/chat.py +++ b/aiogram/types/chat.py @@ -76,7 +76,25 @@ class Chat(base.TelegramObject): if self.type == ChatType.PRIVATE: return f"tg://user?id={self.id}" - return f'https://t.me/{self.username}' if self.username else await self.export_invite_link() + if self.username: + return f'https://t.me/{self.username}' + + if self.invite_link: + return self.invite_link + + await self.update_chat() + return self.invite_link + + async def update_chat(self): + """ + User this method to update Chat data + + :return: None + """ + other = await self.bot.get_chat(self.id) + + for key, value in other: + self[key] = value async def set_photo(self, photo): """ From 693571f2757dcc2af27e89a31017326442b790dc Mon Sep 17 00:00:00 2001 From: Oleg A Date: Sat, 26 May 2018 01:13:04 +0300 Subject: [PATCH 3/3] CantRestrictSelf exception Exception CantRestrictSelf happens if admin try to restrict self --- aiogram/utils/exceptions.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/aiogram/utils/exceptions.py b/aiogram/utils/exceptions.py index d305b9b5..aec980c1 100644 --- a/aiogram/utils/exceptions.py +++ b/aiogram/utils/exceptions.py @@ -26,6 +26,7 @@ TelegramAPIError PhotoAsInputFileRequired InvalidStickersSet ChatAdminRequired + CantRestrictSelf PhotoDimensions UnavailableMembers TypeOfFileMismatch @@ -206,7 +207,12 @@ class ChatAdminRequired(BadRequest): match = 'CHAT_ADMIN_REQUIRED' text = 'Admin permissions is required!' + +class CantRestrictSelf(BadRequest): + match = "can't restrict self" + text = "Admin can't restrict self." + class PhotoDimensions(BadRequest): match = 'PHOTO_INVALID_DIMENSIONS' text = 'Invalid photo dimensions'