@@ -438,6 +438,63 @@ def __init__(self, message_id, from_user, date, chat, content_type, options, jso
438438 setattr (self , key , options [key ])
439439 self .json = json_string
440440
441+ @property
442+ def html_text (self ):
443+ """
444+ Author: @sviat9440
445+ Message: "*Test* parse _formatting_, [url](https://example.com), [text_mention](tg://user?id=123456) and mention @username"
446+
447+ Example:
448+ message.html_text
449+ >> "<b>Test</b> parse <i>formatting</i>, <a href=\" https://example.com\" >url</a>, <a href=\" tg://user?id=123456\" >text_mention</a> and mention @username"
450+
451+ Cusom subs:
452+ You can customize the substitutes. By default, there is no substitute for the entities: hashtag, bot_command, email. You can add or modify substitute an existing entity.
453+ Example:
454+ message.custom_subs = {"bold": "<strong class=\" example\" >{text}</strong>", "italic": "<i class=\" example\" >{text}</i>", "mention": "<a href={url}>{text}</a>"}
455+ message.html_text
456+ >> "<strong class=\" example\" >Test</strong> parse <i class=\" example\" >formatting</i>, <a href=\" https://example.com\" >url</a> and <a href=\" tg://user?id=123456\" >text_mention</a> and mention <a href=\" https://t.me/username\" >@username</a>"
457+ """
458+
459+ if not self .entities :
460+ return self .text
461+ _subs = {
462+ "bold" : "<b>{text}</b>" ,
463+ "italic" : "<i>{text}</i>" ,
464+ "pre" : "<pre>{text}</pre>" ,
465+ "code" : "<code>{text}</code>" ,
466+ "url" : "<a href=\" {url}\" >{text}</a>"
467+ }
468+ if hasattr (self , "custom_subs" ):
469+ for type in self .custom_subs :
470+ _subs [type ] = self .custom_subs [type ]
471+ html_text = ""
472+ def func (text , type = None , url = None , user = None ):
473+ if type == "text_mention" :
474+ type = "url"
475+ url = "tg://user?id={0}" .format (user .id )
476+ elif type == "mention" :
477+ url = "https://t.me/{0}" .format (text [1 :])
478+ if not type or not _subs .get (type ):
479+ return text
480+ subs = _subs .get (type )
481+ text = text .replace ("&" , "&" ).replace ("<" , "<" ).replace (">" , ">" )
482+ return subs .format (text = text , url = url )
483+
484+ offset = 0
485+ for entity in self .entities :
486+ if entity .type == "bot_command" :
487+ entity .offset -= 1
488+ entity .length += 1
489+ if entity .offset > offset :
490+ html_text += func (self .text [offset :entity .offset ])
491+ offset = entity .offset
492+ html_text += func (self .text [offset :offset + entity .length ], entity .type , entity .url , entity .user )
493+ offset += entity .length
494+ if offset < len (self .text ):
495+ html_text += func (self .text [offset :])
496+ return html_text
497+
441498
442499class MessageEntity (JsonDeserializable ):
443500 @classmethod
@@ -1822,7 +1879,7 @@ def __init__(self, id, title):
18221879 def add_price (self , * args ):
18231880 """
18241881 Add LabeledPrice to ShippingOption
1825- :param args: LabeledPrices
1882+ :param args: LabeledPrices
18261883 """
18271884 for price in args :
18281885 self .prices .append (price )
0 commit comments