From 2384b370a71612c406c9f5c72aa40771cf37d60d Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Sun, 13 Jan 2019 00:48:32 +0200 Subject: [PATCH] Implement message URL generator --- aiogram/types/message.py | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/aiogram/types/message.py b/aiogram/types/message.py index 7a6011b7..4f5e0914 100644 --- a/aiogram/types/message.py +++ b/aiogram/types/message.py @@ -9,7 +9,7 @@ from . import base from . import fields from .animation import Animation from .audio import Audio -from .chat import Chat +from .chat import Chat, ChatType from .contact import Contact from .document import Document from .game import Game @@ -241,6 +241,39 @@ class Message(base.TelegramObject): """ return self.parse_entities() + @property + def url(self) -> str: + """ + Get URL for the message + + :return: str + """ + if self.chat.type not in [ChatType.SUPER_GROUP, ChatType.CHANNEL]: + 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}" + + def link(self, text, as_html=True) -> str: + """ + Generate URL for using in text messages with HTML or MD parse mode + + :param text: link label + :param as_html: generate as HTML + :return: str + """ + try: + url = self.url + except TypeError: # URL is not accessible + if as_html: + return md.quote_html(text) + return md.escape_md(text) + + if as_html: + return md.hlink(text, url) + return md.link(text, url) + async def reply(self, text, parse_mode=None, disable_web_page_preview=None, disable_notification=None, reply_markup=None, reply=True) -> Message: """