@@ -1065,3 +1065,31 @@ func (bot *BotAPI) SetMyCommands(commands []BotCommand) error {
10651065 }
10661066 return nil
10671067}
1068+
1069+ // EscapeText takes an input text and escape Telegram markup symbols.
1070+ // In this way we can send a text without being afraid of having to escape the characters manually.
1071+ // Note that you don't have to include the formatting style in the input text, or it will be escaped too.
1072+ // If there is an error, an empty string will be returned.
1073+ //
1074+ // parseMode is the text formatting mode (ModeMarkdown, ModeMarkdownV2 or ModeHTML)
1075+ // text is the input string that will be escaped
1076+ func EscapeText (parseMode string , text string ) string {
1077+ var replacer * strings.Replacer
1078+
1079+ if parseMode == ModeHTML {
1080+ replacer = strings .NewReplacer ("<" , "<" , ">" , ">" , "&" , "&" )
1081+ } else if parseMode == ModeMarkdown {
1082+ replacer = strings .NewReplacer ("_" , "\\ _" , "*" , "\\ *" , "`" , "\\ `" , "[" , "\\ [" )
1083+ } else if parseMode == ModeMarkdownV2 {
1084+ replacer = strings .NewReplacer (
1085+ "_" , "\\ _" , "*" , "\\ *" , "[" , "\\ [" , "]" , "\\ ]" , "(" ,
1086+ "\\ (" , ")" , "\\ )" , "~" , "\\ ~" , "`" , "\\ `" , ">" , "\\ >" ,
1087+ "#" , "\\ #" , "+" , "\\ +" , "-" , "\\ -" , "=" , "\\ =" , "|" ,
1088+ "\\ |" , "{" , "\\ {" , "}" , "\\ }" , "." , "\\ ." , "!" , "\\ !" ,
1089+ )
1090+ } else {
1091+ return ""
1092+ }
1093+
1094+ return replacer .Replace (text )
1095+ }
0 commit comments