diff --git a/pyrogram/parser/html.py b/pyrogram/parser/html.py
index 4afeea2b..c574e6ff 100644
--- a/pyrogram/parser/html.py
+++ b/pyrogram/parser/html.py
@@ -233,8 +233,10 @@ class HTML:
while i < len(entities):
i += recursive(i)
+ last_offset = entities_offsets[-1][1]
# no need to sort, but still add entities starting from the end
for entity, offset in reversed(entities_offsets):
- text = text[:offset] + entity + text[offset:]
+ text = text[:offset] + entity + html.escape(text[offset:last_offset]) + text[last_offset:]
+ last_offset = offset
return utils.remove_surrogates(text)