Add markdown and html properties to Message

This commit is contained in:
Lonami Exo 2022-02-08 11:40:40 +01:00
parent 9b4808a558
commit 8df66c0b47
2 changed files with 27 additions and 15 deletions

View File

@ -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 to patch this value for channels to be the same as the chat, but now it will be faithful to
Telegram's value. 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 // TODO actually provide the things mentioned here

View File

@ -9,7 +9,7 @@ from .file import File
from .inputfile import InputFile from .inputfile import InputFile
from .inputmessage import InputMessage from .inputmessage import InputMessage
from .button import build_reply_markup from .button import build_reply_markup
from ..._misc import utils, helpers, tlobject from ..._misc import utils, helpers, tlobject, markdown, html
from ... import _tl, _misc from ... import _tl, _misc
@ -435,7 +435,6 @@ class Message(ChatGetter, SenderGetter):
self._message = message self._message = message
# Convenient storage for custom functions # Convenient storage for custom functions
self._text = None
self._file = None self._file = None
self._reply_message = None self._reply_message = None
self._buttons = None self._buttons = None
@ -533,8 +532,8 @@ class Message(ChatGetter, SenderGetter):
@property @property
def text(self): def text(self):
""" """
The message text, formatted using the client's default The message text, formatted using the default parse mode.
parse mode. Will be `None` for :tl:`MessageService`. Will be `None` for :tl:`MessageService`.
""" """
return InputMessage._default_parse_mode[1](self.message, self.entities) return InputMessage._default_parse_mode[1](self.message, self.entities)
@ -545,11 +544,9 @@ class Message(ChatGetter, SenderGetter):
@property @property
def raw_text(self): def raw_text(self):
""" """
The raw message text, ignoring any formatting. The plain message text, ignoring any formatting. Will be `None` for :tl:`MessageService`.
Will be `None` for :tl:`MessageService`.
Setting a value to this field will erase the Setting a value to this field will erase the `entities`, unlike changing the `message` member.
`entities`, unlike changing the `message` member.
""" """
return self.message return self.message
@ -557,7 +554,28 @@ class Message(ChatGetter, SenderGetter):
def raw_text(self, value): def raw_text(self, value):
self.message = value self.message = value
self.entities = [] 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 @property
def is_reply(self): def is_reply(self):