Added get_url() method for Message object and shifted_id property for Chat object (#585)

* Added get_url() method for Message object and shifted_id property for Chat object

* Added missing closing bracket to shifted_id description

* Added basic groups to skipped pattern, simplified code

* Return None instead of raising TypeError, removed redundant f-string

* Change get_url typing to Optional[str]

* Better shifted_id method

* get_url tests added

* Added whitespace (E226)

* Code format with black

* Parametrized test
This commit is contained in:
Aleksandr 2021-06-05 13:37:01 +03:00 committed by GitHub
parent 53da50045e
commit 32bc05130f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 86 additions and 0 deletions

View file

@ -1760,6 +1760,25 @@ class Message(TelegramObject):
return DeleteMessage(chat_id=self.chat.id, message_id=self.message_id)
def get_url(self, force_private: bool = False) -> Optional[str]:
"""
Returns message URL. Cannot be used in private (one-to-one) chats.
If chat has a username, returns URL like https://t.me/username/message_id
Otherwise (or if {force_private} flag is set), returns https://t.me/c/shifted_chat_id/message_id
:param force_private: if set, a private URL is returned even for a public chat
:return: string with full message URL
"""
if self.chat.type in ("private", "group"):
return None
if not self.chat.username or force_private:
chat_value = f"c/{self.chat.shifted_id}"
else:
chat_value = self.chat.username
return f"https://t.me/{chat_value}/{self.message_id}"
class ContentType(helper.Helper):
mode = helper.HelperMode.snake_case