TelegramAktienBot/telegram_bot/helper_functions.py

71 lines
2.1 KiB
Python
Raw Permalink Normal View History

2022-05-10 15:44:45 +00:00
"""
script for helper functions for bot related stuff
"""
__author__ = "Florian Kellermann, Linus Eickhoff"
__date__ = "10.05.2022"
2022-05-11 21:33:48 +00:00
__version__ = "1.0.0"
2022-05-10 15:44:45 +00:00
__license__ = "None"
2022-05-11 21:33:48 +00:00
2022-05-10 16:43:41 +00:00
def contains_markdownv1_symbols(text):
2022-05-10 15:44:45 +00:00
""" checks if text contains markdown symbols
:type text: string
:param text: text to check
:return: true if text contains markdown symbols
:rtype: bool
"""
2022-05-11 21:33:48 +00:00
if text.find("_") != -1 or text.find("*") != -1 or text.find("`") != -1: # check if text contains relevant markdown symbols
2022-05-10 15:44:45 +00:00
return True
2022-05-11 21:33:48 +00:00
2022-05-10 15:44:45 +00:00
return False
2022-05-11 21:33:48 +00:00
def make_markdown_proof(text): # used to avoid errors related to markdown parsemode for telegram messaging
2022-05-10 15:44:45 +00:00
""" makes text markdown proof
:type text: string
:param text: text to make markdown proof
:return: markdown proof text
:rtype: string
"""
2022-05-10 17:16:44 +00:00
text = str(text)
2022-05-10 17:19:40 +00:00
2022-05-11 21:33:48 +00:00
text = text.replace("_", "\\_") # replace _ with \_ because \ is used as escape character in markdown, double escape is needed because \ is also a escape character in strings
2022-05-10 15:44:45 +00:00
text = text.replace("*", "\\*")
text = text.replace("`", "\\`")
2022-05-10 16:43:41 +00:00
text = text.replace("[", "\\[")
text = text.replace("]", "\\]")
text = text.replace("(", "\\(")
text = text.replace(")", "\\)")
text = text.replace("#", "\\#")
text = text.replace("+", "\\+")
text = text.replace("-", "\\-")
text = text.replace("!", "\\!")
text = text.replace(".", "\\.")
text = text.replace("?", "\\?")
text = text.replace("/", "\\/")
text = text.replace("~", "\\~")
text = text.replace("|", "\\|")
text = text.replace("<", "\\<")
text = text.replace(">", "\\>")
text = text.replace("&", "\\&")
text = text.replace("^", "\\^")
text = text.replace("$", "\\$")
text = text.replace("%", "\\%")
2022-05-12 07:28:37 +00:00
text = text.replace("=", "\\=")
text = text.replace("@", "\\@")
2022-05-10 16:43:41 +00:00
2022-05-10 15:44:45 +00:00
return text
if __name__ == '__main__':
2022-05-10 16:22:03 +00:00
print("this is a module for helper functions for the bot and should not be run directly")
print(make_markdown_proof("_test_"))
text = make_markdown_proof("_test_")
2022-05-11 21:33:48 +00:00
print(f"{text}")