Support custom parse_mode to use MessageEntityMentionName

This commit is contained in:
Lonami Exo 2018-06-22 16:18:23 +02:00
parent f614d3836b
commit 51da48ddd2

View File

@ -45,6 +45,19 @@ class MessageParseMethods(UserMethods):
# region Private methods # region Private methods
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)
)
except (ValueError, TypeError):
pass
async def _parse_message_text(self, message, parse_mode): async def _parse_message_text(self, message, parse_mode):
""" """
Returns a (parsed message, entities) tuple depending on ``parse_mode``. Returns a (parsed message, entities) tuple depending on ``parse_mode``.
@ -62,15 +75,11 @@ class MessageParseMethods(UserMethods):
if isinstance(e, types.MessageEntityTextUrl): if isinstance(e, types.MessageEntityTextUrl):
m = re.match(r'^@|\+|tg://user\?id=(\d+)', e.url) m = re.match(r'^@|\+|tg://user\?id=(\d+)', e.url)
if m: if m:
try: user = int(m.group(1)) if m.group(1) else e.url
msg_entities[i] = types.InputMessageEntityMentionName( await self._replace_with_mention(msg_entities, i, user)
e.offset, e.length, await self.get_input_entity( elif isinstance(e, (types.MessageEntityMentionName,
int(m.group(1)) if m.group(1) else e.url types.InputMessageEntityMentionName)):
) await self._replace_with_mention(msg_entities, i, e.user_id)
)
except (ValueError, TypeError):
# Make no replacement
pass
return message, msg_entities return message, msg_entities