Skip to content

Instantly share code, notes, and snippets.

@ZiTAL
Created October 16, 2023 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZiTAL/c6d2e8ff919f630aba5757c65c746ce7 to your computer and use it in GitHub Desktop.
Save ZiTAL/c6d2e8ff919f630aba5757c65c746ce7 to your computer and use it in GitHub Desktop.
python: telegram bot: escape markdown special chars
# https://core.telegram.org/bots/api#markdownv2-style
def mdEscapeSpecialChars(msg):
special = r'_*[]()~`>#+-=|{}.!'
pattern = r'([' + re.escape(special) + '])'
replacer = lambda match: '\\' + match.group(1)
output = re.sub(pattern, replacer, msg)
return output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment