Created
October 16, 2023 14:16
-
-
Save ZiTAL/c6d2e8ff919f630aba5757c65c746ce7 to your computer and use it in GitHub Desktop.
python: telegram bot: escape markdown special chars
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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