diff --git a/aiogram/types/message.py b/aiogram/types/message.py
index dd8828c4..c047ef7f 100644
--- a/aiogram/types/message.py
+++ b/aiogram/types/message.py
@@ -8,7 +8,7 @@ from .document import Document
from .game import Game
from .invoice import Invoice
from .location import Location
-from .message_entity import MessageEntity
+from .message_entity import MessageEntity, MessageEntityType
from .photo_size import PhotoSize
from .sticker import Sticker
from .successful_payment import SuccessfulPayment
@@ -179,6 +179,26 @@ class Message(Deserializable):
if command:
return command[1].strip()
+ @property
+ def md_text(self):
+ text = self.text
+
+ if self.text and self.entities:
+ for entity in reversed(self.entities):
+ text = entity.apply_md(text)
+
+ return text
+
+ @property
+ def html_text(self):
+ text = self.text
+
+ if self.text and self.entities:
+ for entity in reversed(self.entities):
+ text = entity.apply_html(text)
+
+ return text
+
async def reply(self, text, parse_mode=None, disable_web_page_preview=None,
disable_notification=None, reply_markup=None) -> 'Message':
"""
diff --git a/aiogram/types/message_entity.py b/aiogram/types/message_entity.py
index cac0a192..2c183725 100644
--- a/aiogram/types/message_entity.py
+++ b/aiogram/types/message_entity.py
@@ -1,3 +1,4 @@
+from aiogram.utils import markdown
from .base import Deserializable
from .user import User
@@ -8,6 +9,7 @@ class MessageEntity(Deserializable):
https://core.telegram.org/bots/api#messageentity
"""
+
def __init__(self, type, offset, length, url, user):
self.type: str = type
self.offset: int = offset
@@ -27,6 +29,41 @@ class MessageEntity(Deserializable):
return MessageEntity(type, offset, length, url, user)
+ def _apply(self, text, func):
+ return text[:self.offset] + \
+ func(text[self.offset:self.offset + self.length]) + \
+ text[self.offset + self.length:]
+
+ def apply_md(self, text):
+ if self.type == MessageEntityType.BOLD:
+ return self._apply(text, markdown.bold)
+ elif self.type == MessageEntityType.ITALIC:
+ return self._apply(text, markdown.italic)
+ elif self.type == MessageEntityType.PRE:
+ return self._apply(text, markdown.pre)
+ elif self.type == MessageEntityType.CODE:
+ return self._apply(text, markdown.code)
+ elif self.type == MessageEntityType.URL:
+ return self._apply(text, lambda url: markdown.link(url, url))
+ elif self.type == MessageEntityType.TEXT_LINK:
+ return self._apply(text, lambda url: markdown.link(url, self.url))
+ return text
+
+ def apply_html(self, text):
+ if self.type == MessageEntityType.BOLD:
+ return self._apply(text, markdown.hbold)
+ elif self.type == MessageEntityType.ITALIC:
+ return self._apply(text, markdown.hitalic)
+ elif self.type == MessageEntityType.PRE:
+ return self._apply(text, markdown.hpre)
+ elif self.type == MessageEntityType.CODE:
+ return self._apply(text, markdown.hcode)
+ elif self.type == MessageEntityType.URL:
+ return self._apply(text, lambda url: markdown.hlink(url, url))
+ elif self.type == MessageEntityType.TEXT_LINK:
+ return self._apply(text, lambda url: markdown.hlink(url, self.url))
+ return text
+
class MessageEntityType:
"""
diff --git a/aiogram/utils/markdown.py b/aiogram/utils/markdown.py
index 26976054..f699f4ef 100644
--- a/aiogram/utils/markdown.py
+++ b/aiogram/utils/markdown.py
@@ -4,7 +4,11 @@ MD_SYMBOLS = (
(LIST_MD_SYMBOLS[0], LIST_MD_SYMBOLS[0]),
(LIST_MD_SYMBOLS[1], LIST_MD_SYMBOLS[1]),
(LIST_MD_SYMBOLS[2], LIST_MD_SYMBOLS[2]),
- (LIST_MD_SYMBOLS[2] * 3 + '\n', '\n' + LIST_MD_SYMBOLS[2] * 3)
+ (LIST_MD_SYMBOLS[2] * 3 + '\n', '\n' + LIST_MD_SYMBOLS[2] * 3),
+ ('', ''),
+ ('', ''),
+ ('', ''),
+ ('
', ''), ) @@ -24,28 +28,48 @@ def _md(string, symbols=('', '')): def text(*content, sep=' '): - return _md('', _join(*content, sep=sep)) + return _join(*content, sep=sep) def bold(*content, sep=' '): return _md(_join(*content, sep=sep), symbols=MD_SYMBOLS[0]) +def hbold(*content, sep=' '): + return _md(_join(*content, sep=sep), symbols=MD_SYMBOLS[4]) + + def italic(*content, sep=' '): return _md(_join(*content, sep=sep), symbols=MD_SYMBOLS[1]) +def hitalic(*content, sep=' '): + return _md(_join(*content, sep=sep), symbols=MD_SYMBOLS[5]) + + def code(*content, sep=' '): return _md(_join(*content, sep=sep), symbols=MD_SYMBOLS[2]) +def hcode(*content, sep=' '): + return _md(_join(*content, sep=sep), symbols=MD_SYMBOLS[6]) + + def pre(*content, sep='\n'): return _md(_join(*content, sep=sep), symbols=MD_SYMBOLS[3]) +def hpre(*content, sep='\n'): + return _md(_join(*content, sep=sep), symbols=MD_SYMBOLS[7]) + + def link(title, url): return f"[{_escape(title)}]({url})" +def hlink(title, url): + return f"{_escape(title)}" + + def escape_md(*content, sep=' '): return _escape(_join(*content, sep=sep))