all repos — telegram-bot-api @ 85ecb116757bcc6b4ec985e495d206c5e4443063

Golang bindings for the Telegram Bot API

Added EscapeText function
Erik Pellizzon erikpellizzon1@gmail.com
Mon, 26 Oct 2020 17:41:02 +0100
commit

85ecb116757bcc6b4ec985e495d206c5e4443063

parent

b6df6c273aa804a6c7dd2f719ac8be1a95ce2790

1 files changed, 27 insertions(+), 0 deletions(-)

jump to
M bot.gobot.go

@@ -1065,3 +1065,30 @@ return err

} return nil } + +// EscapeText takes an input text and escape Telegram markup symbols. +// In this way we can send a text without being afraid of having to escape the characters manually. +// Note that you don't have to include the formatting style in the input text, or it will be escaped too. +// +// parseMode is the text formatting mode (ModeMarkdown, ModeMarkdownV2 or ModeHTML) +// text is the input string that will be escaped +func (*BotAPI) EscapeText(parseMode string, text string) string { + var replacer *strings.Replacer + + if parseMode == ModeHTML { + replacer = strings.NewReplacer("<", "&lt;", ">", "&gt;", "&", "&amp;") + } else if parseMode == ModeMarkdown { + replacer = strings.NewReplacer("_", "\\_", "*", "\\*", "`", "\\`", "[", "\\[") + } else if parseMode == ModeMarkdownV2 { + replacer = strings.NewReplacer( + "_", "\\_", "*", "\\*", "[", "\\[", "]", "\\]", "(", + "\\(", ")", "\\)", "~", "\\~", "`", "\\`", ">", "\\>", + "#", "\\#", "+", "\\+", "-", "\\-", "=", "\\=", "|", + "\\|", "{", "\\{", "}", "\\}", ".", "\\.", "!", "\\!", + ) + } else { + return "" + } + + return replacer.Replace(text) +}