From 8d7c7a19c075964235efefd117089db0af787fec Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Sun, 3 Jun 2018 11:53:18 +0200 Subject: [PATCH] Add some setters for custom.Message --- telethon/events/newmessage.py | 3 +++ telethon/extensions/markdown.py | 45 +++------------------------------ telethon/tl/custom/message.py | 40 +++++++++++++++++++++++------ telethon/utils.py | 40 +++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 49 deletions(-) diff --git a/telethon/events/newmessage.py b/telethon/events/newmessage.py index 8bba002a..2d15e3ac 100644 --- a/telethon/events/newmessage.py +++ b/telethon/events/newmessage.py @@ -148,3 +148,6 @@ class NewMessage(EventBuilder): def __getattr__(self, item): return getattr(self.message, item) + + def __setattr__(self, name, value): + return setattr(self.original_message, name, value) diff --git a/telethon/extensions/markdown.py b/telethon/extensions/markdown.py index 94a13818..a7af1fa8 100644 --- a/telethon/extensions/markdown.py +++ b/telethon/extensions/markdown.py @@ -4,15 +4,16 @@ for use within the library, which attempts to handle emojies correctly, since they seem to count as two characters and it's a bit strange. """ import re -import struct from ..tl import TLObject - from ..tl.types import ( MessageEntityBold, MessageEntityItalic, MessageEntityCode, MessageEntityPre, MessageEntityTextUrl ) - +from ..utils import ( + add_surrogate as _add_surrogate, + del_surrogate as _del_surrogate +) DEFAULT_DELIMITERS = { '**': MessageEntityBold, @@ -25,19 +26,6 @@ DEFAULT_URL_RE = re.compile(r'\[([\S\s]+?)\]\((.+?)\)') DEFAULT_URL_FORMAT = '[{0}]({1})' -def _add_surrogate(text): - return ''.join( - # SMP -> Surrogate Pairs (Telegram offsets are calculated with these). - # See https://en.wikipedia.org/wiki/Plane_(Unicode)#Overview for more. - ''.join(chr(y) for y in struct.unpack(' 'y!'. - - :param text: the original text. - :param entity: the entity or entities that must be matched. - :return: a single result or a list of the text surrounded by the entities. - """ - if isinstance(entity, TLObject): - entity = (entity,) - multiple = True - else: - multiple = False - - text = _add_surrogate(text) - result = [] - for e in entity: - start = e.offset - end = e.offset + e.length - result.append(_del_surrogate(text[start:end])) - - return result if multiple else result[0] diff --git a/telethon/tl/custom/message.py b/telethon/tl/custom/message.py index 8150ab46..02cd1448 100644 --- a/telethon/tl/custom/message.py +++ b/telethon/tl/custom/message.py @@ -1,6 +1,5 @@ from .. import types -from ...extensions import markdown -from ...utils import get_input_peer, get_peer_id +from ...utils import get_input_peer, get_peer_id, get_inner_text from .messagebutton import MessageButton @@ -55,6 +54,9 @@ class Message: def __getattr__(self, item): return getattr(self.original_message, item) + def __setattr__(self, name, value): + return setattr(self.original_message, name, value) + def __str__(self): return str(self.original_message) @@ -75,17 +77,28 @@ class Message: @property def text(self): """ - The message text, markdown-formatted. + The message text, formatted using the client's default parse mode. Will be ``None`` for :tl:`MessageService`. """ if self._text is None\ and isinstance(self.original_message, types.Message): - if not self.original_message.entities: + if not self._client.parse_mode: return self.original_message.message - self._text = markdown.unparse(self.original_message.message, - self.original_message.entities or []) + self._text = self._client.parse_mode.unparse( + self.original_message.message, self.original_message.entities) return self._text + @text.setter + def text(self, value): + if isinstance(self.original_message, types.Message): + if self._client.parse_mode: + msg, ent = self._client.parse_mode.parse(value) + else: + msg, ent = value, [] + self.original_message.message = msg + self.original_message.entities = ent + self._text = value + @property def raw_text(self): """ @@ -95,6 +108,13 @@ class Message: if isinstance(self.original_message, types.Message): return self.original_message.message + @raw_text.setter + def raw_text(self, value): + if isinstance(self.original_message, types.Message): + self.original_message.message = value + self.original_message.entities = [] + self._text = None + @property def message(self): """ @@ -103,6 +123,10 @@ class Message: """ return self.raw_text + @message.setter + def message(self, value): + self.raw_text = value + @property def action(self): """ @@ -478,8 +502,8 @@ class Message: Returns a list of tuples [(:tl:`MessageEntity`, `str`)], the string being the inner text of the message entity (like bold, italics, etc). """ - texts = markdown.get_inner_text(self.original_message.message, - self.original_message.entities) + texts = get_inner_text(self.original_message.message, + self.original_message.entities) return list(zip(self.original_message.entities, texts)) def click(self, i=None, j=None, *, text=None, filter=None): diff --git a/telethon/utils.py b/telethon/utils.py index c331148f..806f7848 100644 --- a/telethon/utils.py +++ b/telethon/utils.py @@ -6,10 +6,12 @@ import math import mimetypes import os import re +import struct import types from collections import UserList from mimetypes import guess_extension +from .tl import TLObject from .tl.types import ( Channel, ChannelForbidden, Chat, ChatEmpty, ChatForbidden, ChatFull, ChatPhoto, InputPeerChannel, InputPeerChat, InputPeerUser, InputPeerEmpty, @@ -500,6 +502,44 @@ def _fix_peer_id(peer_id): return int(peer_id) +def add_surrogate(text): + return ''.join( + # SMP -> Surrogate Pairs (Telegram offsets are calculated with these). + # See https://en.wikipedia.org/wiki/Plane_(Unicode)#Overview for more. + ''.join(chr(y) for y in struct.unpack(' 'y!'. + + :param text: the original text. + :param entity: the entity or entities that must be matched. + :return: a single result or a list of the text surrounded by the entities. + """ + if isinstance(entity, TLObject): + entity = (entity,) + multiple = True + else: + multiple = False + + text = add_surrogate(text) + result = [] + for e in entity: + start = e.offset + end = e.offset + e.length + result.append(del_surrogate(text[start:end])) + + return result if multiple else result[0] + + def get_peer_id(peer): """ Finds the ID of the given peer, and converts it to the "bot api" format