mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-11 09:55:21 +00:00
Parse message entities (As HTML and markdown) and update markdown utils (add html mode)
This commit is contained in:
parent
bc917ec9f5
commit
42245fc58a
3 changed files with 84 additions and 3 deletions
|
|
@ -8,7 +8,7 @@ from .document import Document
|
||||||
from .game import Game
|
from .game import Game
|
||||||
from .invoice import Invoice
|
from .invoice import Invoice
|
||||||
from .location import Location
|
from .location import Location
|
||||||
from .message_entity import MessageEntity
|
from .message_entity import MessageEntity, MessageEntityType
|
||||||
from .photo_size import PhotoSize
|
from .photo_size import PhotoSize
|
||||||
from .sticker import Sticker
|
from .sticker import Sticker
|
||||||
from .successful_payment import SuccessfulPayment
|
from .successful_payment import SuccessfulPayment
|
||||||
|
|
@ -179,6 +179,26 @@ class Message(Deserializable):
|
||||||
if command:
|
if command:
|
||||||
return command[1].strip()
|
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,
|
async def reply(self, text, parse_mode=None, disable_web_page_preview=None,
|
||||||
disable_notification=None, reply_markup=None) -> 'Message':
|
disable_notification=None, reply_markup=None) -> 'Message':
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
from aiogram.utils import markdown
|
||||||
from .base import Deserializable
|
from .base import Deserializable
|
||||||
from .user import User
|
from .user import User
|
||||||
|
|
||||||
|
|
@ -8,6 +9,7 @@ class MessageEntity(Deserializable):
|
||||||
|
|
||||||
https://core.telegram.org/bots/api#messageentity
|
https://core.telegram.org/bots/api#messageentity
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, type, offset, length, url, user):
|
def __init__(self, type, offset, length, url, user):
|
||||||
self.type: str = type
|
self.type: str = type
|
||||||
self.offset: int = offset
|
self.offset: int = offset
|
||||||
|
|
@ -27,6 +29,41 @@ class MessageEntity(Deserializable):
|
||||||
|
|
||||||
return MessageEntity(type, offset, length, url, user)
|
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:
|
class MessageEntityType:
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,11 @@ MD_SYMBOLS = (
|
||||||
(LIST_MD_SYMBOLS[0], LIST_MD_SYMBOLS[0]),
|
(LIST_MD_SYMBOLS[0], LIST_MD_SYMBOLS[0]),
|
||||||
(LIST_MD_SYMBOLS[1], LIST_MD_SYMBOLS[1]),
|
(LIST_MD_SYMBOLS[1], LIST_MD_SYMBOLS[1]),
|
||||||
(LIST_MD_SYMBOLS[2], LIST_MD_SYMBOLS[2]),
|
(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),
|
||||||
|
('<b>', '</b>'),
|
||||||
|
('<i>', '</i>'),
|
||||||
|
('<code>', '</code>'),
|
||||||
|
('<pre>', '</pre>'),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -24,28 +28,48 @@ def _md(string, symbols=('', '')):
|
||||||
|
|
||||||
|
|
||||||
def text(*content, sep=' '):
|
def text(*content, sep=' '):
|
||||||
return _md('', _join(*content, sep=sep))
|
return _join(*content, sep=sep)
|
||||||
|
|
||||||
|
|
||||||
def bold(*content, sep=' '):
|
def bold(*content, sep=' '):
|
||||||
return _md(_join(*content, sep=sep), symbols=MD_SYMBOLS[0])
|
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=' '):
|
def italic(*content, sep=' '):
|
||||||
return _md(_join(*content, sep=sep), symbols=MD_SYMBOLS[1])
|
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=' '):
|
def code(*content, sep=' '):
|
||||||
return _md(_join(*content, sep=sep), symbols=MD_SYMBOLS[2])
|
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'):
|
def pre(*content, sep='\n'):
|
||||||
return _md(_join(*content, sep=sep), symbols=MD_SYMBOLS[3])
|
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):
|
def link(title, url):
|
||||||
return f"[{_escape(title)}]({url})"
|
return f"[{_escape(title)}]({url})"
|
||||||
|
|
||||||
|
|
||||||
|
def hlink(title, url):
|
||||||
|
return f"<a href=\"{url}\">{_escape(title)}</a>"
|
||||||
|
|
||||||
|
|
||||||
def escape_md(*content, sep=' '):
|
def escape_md(*content, sep=' '):
|
||||||
return _escape(_join(*content, sep=sep))
|
return _escape(_join(*content, sep=sep))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue