diff --git a/telethon/extensions/html.py b/telethon/extensions/html.py
index d4233473..8a397336 100644
--- a/telethon/extensions/html.py
+++ b/telethon/extensions/html.py
@@ -9,8 +9,8 @@ from html.parser import HTMLParser
from ..tl.types import (
MessageEntityBold, MessageEntityItalic, MessageEntityCode,
MessageEntityPre, MessageEntityEmail, MessageEntityUrl,
- MessageEntityTextUrl
-)
+ MessageEntityTextUrl, MessageEntityMentionName
+ )
# Helpers from markdown.py
@@ -178,6 +178,9 @@ def unparse(text, entities):
elif entity_type == MessageEntityTextUrl:
html.append('{}'
.format(escape(entity.url), entity_text))
+ elif entity_type == MessageEntityMentionName:
+ html.append('{}'
+ .format(entity.user_id, entity_text))
else:
skip_entity = True
last_offset = entity.offset + (0 if skip_entity else entity.length)
diff --git a/telethon/extensions/markdown.py b/telethon/extensions/markdown.py
index 5274dc85..be0ea507 100644
--- a/telethon/extensions/markdown.py
+++ b/telethon/extensions/markdown.py
@@ -9,8 +9,8 @@ from ..helpers import add_surrogate, del_surrogate
from ..tl import TLObject
from ..tl.types import (
MessageEntityBold, MessageEntityItalic, MessageEntityCode,
- MessageEntityPre, MessageEntityTextUrl
-)
+ MessageEntityPre, MessageEntityTextUrl, MessageEntityMentionName
+ )
DEFAULT_DELIMITERS = {
'**': MessageEntityBold,
@@ -161,11 +161,17 @@ def unparse(text, entities, delimiters=None, url_fmt=None):
delimiter = delimiters.get(type(entity), None)
if delimiter:
text = text[:s] + delimiter + text[s:e] + delimiter + text[e:]
- elif isinstance(entity, MessageEntityTextUrl) and url_fmt:
- text = (
- text[:s] +
- add_surrogate(url_fmt.format(text[s:e], entity.url)) +
- text[e:]
- )
+ elif url_fmt:
+ url = None
+ if isinstance(entity, MessageEntityTextUrl):
+ url = entity.url
+ elif isinstance(entity, MessageEntityMentionName):
+ url = 'tg://user?id={}'.format(entity.user_id)
+ if url:
+ text = (
+ text[:s] +
+ add_surrogate(url_fmt.format(text[s:e], url)) +
+ text[e:]
+ )
return del_surrogate(text)