Skip to content

Instantly share code, notes, and snippets.

@JosXa
Last active January 11, 2017 18:52
Show Gist options
  • Save JosXa/fd4f2122131f3df97fb43748c6165a7c to your computer and use it in GitHub Desktop.
Save JosXa/fd4f2122131f3df97fb43748c6165a7c to your computer and use it in GitHub Desktop.
Final Version
import html
import re
import copy
def parse_markdown_from_update(update):
entities = copy.deepcopy(parse_entities_from_update(update))
text = escape_markdown(message_text_from_update(update))
for e in entities:
modified = None
pos = e.offset
length = e.length
if e.type == "text_link":
text = "{}[{}]({}){}".format(text[:pos], text[pos:pos + length], e.url, text[pos + length:])
modified = 4 + len(e.url)
elif e.type == "bold":
text = text[:pos] + '*' + text[pos:pos + length] + '*' + text[pos + length:]
modified = 2
elif e.type == "italic":
text = text[:pos] + '_' + text[pos:pos + length] + '_' + text[pos + length:]
modified = 2
elif e.type == "code":
text = text[:pos] + '`' + text[pos:pos + length] + '`' + text[pos + length:]
modified = 2
elif e.type == "pre":
text = text[:pos] + '```\n' + text[pos:pos + length] + '\n```' + text[pos + length:]
modified = 6
else:
print("Type of entity `{}` is not supported.".format(e.type))
# update offsets of all entities to the right
if modified:
for other in entities:
if other.offset > pos:
other.offset += modified
return text
def parse_html_from_update(update):
entities = copy.deepcopy(parse_entities_from_update(update))
text = escape_html(message_text_from_update(update))
for e in entities:
modified = None
pos = e.offset
length = e.length
if e.type == "text_link":
text = '{}<a href="{}">{}</a>{}'.format(text[:pos], e.url, text[pos:pos + length], text[pos + length:])
modified = 15 + len(e.url)
elif e.type == "bold":
text = text[:pos] + '<b>' + text[pos:pos + length] + '</b>' + text[pos + length:]
modified = 7
elif e.type == "italic":
text = text[:pos] + '<i>' + text[pos:pos + length] + '</i>' + text[pos + length:]
modified = 7
elif e.type == "code":
text = text[:pos] + '<code>' + text[pos:pos + length] + '</code>' + text[pos + length:]
modified = 13
elif e.type == "pre":
text = text[:pos] + '<pre>' + text[pos:pos + length] + '</pre>' + text[pos + length:]
modified = 11
else:
print("Type of entity `{}` is not supported.".format(e.type))
# update offsets of all entities to the right
if modified:
for other in entities:
if other.offset > pos:
other.offset += modified
return text
def escape_markdown(text):
"""Helper function to escape telegram markup symbols"""
escape_chars = '\*_`\['
return re.sub(r'([%s])' % escape_chars, r'\\\1', text)
def escape_html(text):
return html.escape(text)
def parse_entities_from_update(update):
try:
return update.message.parse_entities()
except (NameError, AttributeError):
try:
return update.callback_query.message.parse_entities()
except (NameError, AttributeError):
return None
def message_text_from_update(update):
try:
return update.message.text
except (NameError, AttributeError):
try:
return update.callback_query.message.text
except (NameError, AttributeError):
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment