From 8df66c0b47ca06c06d8194d651267ea7d47939c3 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Tue, 8 Feb 2022 11:40:40 +0100 Subject: [PATCH] Add markdown and html properties to Message --- readthedocs/misc/v2-migration-guide.rst | 6 ----- telethon/types/_custom/message.py | 36 ++++++++++++++++++------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/readthedocs/misc/v2-migration-guide.rst b/readthedocs/misc/v2-migration-guide.rst index 23eddcc8..16023b67 100644 --- a/readthedocs/misc/v2-migration-guide.rst +++ b/readthedocs/misc/v2-migration-guide.rst @@ -639,12 +639,6 @@ The message sender no longer is the channel when no sender is provided by Telegr to patch this value for channels to be the same as the chat, but now it will be faithful to Telegram's value. -In order to avoid breaking more code than strictly necessary, ``.raw_text`` will remain a synonym -of ``.message``, and ``.text`` will still be the text formatted through the ``client.parse_mode``. -However, you're encouraged to change uses of ``.raw_text`` with ``.message``, and ``.text`` with -either ``.md_text`` or ``.html_text`` as needed. This is because both ``.text`` and ``.raw_text`` -may disappear in future versions, and their behaviour is not immediately obvious. - // TODO actually provide the things mentioned here diff --git a/telethon/types/_custom/message.py b/telethon/types/_custom/message.py index c108b6ae..730d2b49 100644 --- a/telethon/types/_custom/message.py +++ b/telethon/types/_custom/message.py @@ -9,7 +9,7 @@ from .file import File from .inputfile import InputFile from .inputmessage import InputMessage from .button import build_reply_markup -from ..._misc import utils, helpers, tlobject +from ..._misc import utils, helpers, tlobject, markdown, html from ... import _tl, _misc @@ -435,7 +435,6 @@ class Message(ChatGetter, SenderGetter): self._message = message # Convenient storage for custom functions - self._text = None self._file = None self._reply_message = None self._buttons = None @@ -533,8 +532,8 @@ class Message(ChatGetter, SenderGetter): @property def text(self): """ - The message text, formatted using the client's default - parse mode. Will be `None` for :tl:`MessageService`. + The message text, formatted using the default parse mode. + Will be `None` for :tl:`MessageService`. """ return InputMessage._default_parse_mode[1](self.message, self.entities) @@ -545,11 +544,9 @@ class Message(ChatGetter, SenderGetter): @property def raw_text(self): """ - The raw message text, ignoring any formatting. - Will be `None` for :tl:`MessageService`. + The plain message text, ignoring any formatting. Will be `None` for :tl:`MessageService`. - Setting a value to this field will erase the - `entities`, unlike changing the `message` member. + Setting a value to this field will erase the `entities`, unlike changing the `message` member. """ return self.message @@ -557,7 +554,28 @@ class Message(ChatGetter, SenderGetter): def raw_text(self, value): self.message = value self.entities = [] - self._text = None + + @property + def markdown(self): + """ + The message text, formatted using markdown. Will be `None` for :tl:`MessageService`. + """ + return markdown.unparse(self.message, self.entities) + + @markdown.setter + def markdown(self, value): + self.message, self.entities = markdown.parse(value) + + @property + def html(self): + """ + The message text, formatted using HTML. Will be `None` for :tl:`MessageService`. + """ + return html.unparse(self.message, self.entities) + + @html.setter + def html(self, value): + self.message, self.entities = html.parse(value) @property def is_reply(self):