2018-06-10 12:30:51 +03:00
|
|
|
import itertools
|
|
|
|
import re
|
|
|
|
|
|
|
|
from .users import UserMethods
|
2018-10-08 12:33:56 +03:00
|
|
|
from .. import utils
|
2018-10-05 15:20:50 +03:00
|
|
|
from ..tl import types
|
2018-06-10 12:30:51 +03:00
|
|
|
|
|
|
|
|
|
|
|
class MessageParseMethods(UserMethods):
|
|
|
|
|
|
|
|
# region Public properties
|
|
|
|
|
|
|
|
@property
|
|
|
|
def parse_mode(self):
|
|
|
|
"""
|
|
|
|
This property is the default parse mode used when sending messages.
|
|
|
|
Defaults to `telethon.extensions.markdown`. It will always
|
|
|
|
be either ``None`` or an object with ``parse`` and ``unparse``
|
|
|
|
methods.
|
|
|
|
|
|
|
|
When setting a different value it should be one of:
|
|
|
|
|
|
|
|
* Object with ``parse`` and ``unparse`` methods.
|
|
|
|
* A ``callable`` to act as the parse method.
|
|
|
|
* A ``str`` indicating the ``parse_mode``. For Markdown ``'md'``
|
|
|
|
or ``'markdown'`` may be used. For HTML, ``'htm'`` or ``'html'``
|
|
|
|
may be used.
|
|
|
|
|
|
|
|
The ``parse`` method should be a function accepting a single
|
|
|
|
parameter, the text to parse, and returning a tuple consisting
|
|
|
|
of ``(parsed message str, [MessageEntity instances])``.
|
|
|
|
|
|
|
|
The ``unparse`` method should be the inverse of ``parse`` such
|
|
|
|
that ``assert text == unparse(*parse(text))``.
|
|
|
|
|
|
|
|
See :tl:`MessageEntity` for allowed message entities.
|
|
|
|
"""
|
|
|
|
return self._parse_mode
|
|
|
|
|
|
|
|
@parse_mode.setter
|
|
|
|
def parse_mode(self, mode):
|
|
|
|
self._parse_mode = utils.sanitize_parse_mode(mode)
|
|
|
|
|
|
|
|
# endregion
|
|
|
|
|
|
|
|
# region Private methods
|
|
|
|
|
2018-06-22 17:18:23 +03:00
|
|
|
async def _replace_with_mention(self, entities, i, user):
|
|
|
|
"""
|
|
|
|
Helper method to replace ``entities[i]`` to mention ``user``,
|
|
|
|
or do nothing if it can't be found.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
entities[i] = types.InputMessageEntityMentionName(
|
|
|
|
entities[i].offset, entities[i].length,
|
|
|
|
await self.get_input_entity(user)
|
|
|
|
)
|
2018-11-24 21:39:05 +03:00
|
|
|
return True
|
2018-06-22 17:18:23 +03:00
|
|
|
except (ValueError, TypeError):
|
2018-11-24 21:39:05 +03:00
|
|
|
return False
|
2018-06-22 17:18:23 +03:00
|
|
|
|
2018-06-10 12:30:51 +03:00
|
|
|
async def _parse_message_text(self, message, parse_mode):
|
|
|
|
"""
|
|
|
|
Returns a (parsed message, entities) tuple depending on ``parse_mode``.
|
|
|
|
"""
|
2018-10-08 12:33:56 +03:00
|
|
|
if parse_mode is ():
|
2018-06-10 12:30:51 +03:00
|
|
|
parse_mode = self._parse_mode
|
|
|
|
else:
|
|
|
|
parse_mode = utils.sanitize_parse_mode(parse_mode)
|
|
|
|
|
|
|
|
if not parse_mode:
|
|
|
|
return message, []
|
|
|
|
|
|
|
|
message, msg_entities = parse_mode.parse(message)
|
2018-11-24 21:39:05 +03:00
|
|
|
for i in reversed(range(len(msg_entities))):
|
|
|
|
e = msg_entities[i]
|
2018-06-10 12:30:51 +03:00
|
|
|
if isinstance(e, types.MessageEntityTextUrl):
|
|
|
|
m = re.match(r'^@|\+|tg://user\?id=(\d+)', e.url)
|
|
|
|
if m:
|
2018-06-22 17:18:23 +03:00
|
|
|
user = int(m.group(1)) if m.group(1) else e.url
|
2018-11-24 21:39:05 +03:00
|
|
|
is_mention = await self._replace_with_mention(msg_entities, i, user)
|
|
|
|
if not is_mention:
|
|
|
|
del msg_entities[i]
|
2018-06-22 17:18:23 +03:00
|
|
|
elif isinstance(e, (types.MessageEntityMentionName,
|
|
|
|
types.InputMessageEntityMentionName)):
|
2018-11-24 21:39:05 +03:00
|
|
|
is_mention = await self._replace_with_mention(msg_entities, i, e.user_id)
|
|
|
|
if not is_mention:
|
|
|
|
del msg_entities[i]
|
2018-06-10 12:30:51 +03:00
|
|
|
|
|
|
|
return message, msg_entities
|
|
|
|
|
|
|
|
def _get_response_message(self, request, result, input_chat):
|
|
|
|
"""
|
|
|
|
Extracts the response message known a request and Update result.
|
|
|
|
The request may also be the ID of the message to match.
|
|
|
|
"""
|
|
|
|
# Telegram seems to send updateMessageID first, then updateNewMessage,
|
|
|
|
# however let's not rely on that just in case.
|
|
|
|
if isinstance(request, int):
|
|
|
|
msg_id = request
|
|
|
|
else:
|
|
|
|
msg_id = None
|
|
|
|
for update in result.updates:
|
|
|
|
if isinstance(update, types.UpdateMessageID):
|
|
|
|
if update.random_id == request.random_id:
|
|
|
|
msg_id = update.id
|
|
|
|
break
|
|
|
|
|
|
|
|
if isinstance(result, types.UpdateShort):
|
|
|
|
updates = [result.update]
|
|
|
|
entities = {}
|
|
|
|
elif isinstance(result, (types.Updates, types.UpdatesCombined)):
|
|
|
|
updates = result.updates
|
|
|
|
entities = {utils.get_peer_id(x): x
|
|
|
|
for x in
|
|
|
|
itertools.chain(result.users, result.chats)}
|
|
|
|
else:
|
2018-12-20 22:33:25 +03:00
|
|
|
return None
|
2018-06-10 12:30:51 +03:00
|
|
|
|
|
|
|
found = None
|
|
|
|
for update in updates:
|
|
|
|
if isinstance(update, (
|
|
|
|
types.UpdateNewChannelMessage, types.UpdateNewMessage)):
|
|
|
|
if update.message.id == msg_id:
|
|
|
|
found = update.message
|
|
|
|
break
|
|
|
|
|
|
|
|
elif (isinstance(update, types.UpdateEditMessage)
|
|
|
|
and not isinstance(request.peer, types.InputPeerChannel)):
|
|
|
|
if request.id == update.message.id:
|
|
|
|
found = update.message
|
|
|
|
break
|
|
|
|
|
|
|
|
elif (isinstance(update, types.UpdateEditChannelMessage)
|
|
|
|
and utils.get_peer_id(request.peer) ==
|
|
|
|
utils.get_peer_id(update.message.to_id)):
|
|
|
|
if request.id == update.message.id:
|
|
|
|
found = update.message
|
|
|
|
break
|
|
|
|
|
|
|
|
if found:
|
2018-07-22 20:20:55 +03:00
|
|
|
found._finish_init(self, entities, input_chat)
|
|
|
|
return found
|
2018-12-20 22:33:25 +03:00
|
|
|
else:
|
|
|
|
return None # explicit is better than implicit
|
2018-06-10 12:30:51 +03:00
|
|
|
|
|
|
|
# endregion
|