Advertisement
disk6969

CustomHtml

Jun 21st, 2024
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.02 KB | None | 0 0
  1. from telethon.extensions import html
  2. from telethon import types
  3.  
  4. class CustomHtml:
  5.     @staticmethod
  6.     def parse(text):
  7.         text, entities = html.parse(text)
  8.         for i, e in enumerate(entities):
  9.             if isinstance(e, types.MessageEntityTextUrl):
  10.                 if e.url == 'spoiler':
  11.                     entities[i] = types.MessageEntitySpoiler(e.offset, e.length)
  12.                 elif e.url.startswith('emoji/'):
  13.                     entities[i] = types.MessageEntityCustomEmoji(e.offset, e.length, int(e.url.split('/')[1]))
  14.         return text, entities
  15.     @staticmethod
  16.     def unparse(text, entities):
  17.         for i, e in enumerate(entities or []):
  18.             if isinstance(e, types.MessageEntityCustomEmoji):
  19.                 entities[i] = types.MessageEntityTextUrl(e.offset, e.length, f'emoji/{e.document_id}')
  20.             if isinstance(e, types.MessageEntitySpoiler):
  21.                 entities[i] = types.MessageEntityTextUrl(e.offset, e.length, 'spoiler')
  22.         return html.unparse(text, entities)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement