From 85ecb116757bcc6b4ec985e495d206c5e4443063 Mon Sep 17 00:00:00 2001 From: Erik Pellizzon Date: Mon, 26 Oct 2020 17:41:02 +0100 Subject: [PATCH 1/3] Added EscapeText function --- bot.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/bot.go b/bot.go index cffe4c24..f1abe30a 100644 --- a/bot.go +++ b/bot.go @@ -1065,3 +1065,30 @@ func (bot *BotAPI) SetMyCommands(commands []BotCommand) error { } 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("<", "<", ">", ">", "&", "&") + } else if parseMode == ModeMarkdown { + replacer = strings.NewReplacer("_", "\\_", "*", "\\*", "`", "\\`", "[", "\\[") + } else if parseMode == ModeMarkdownV2 { + replacer = strings.NewReplacer( + "_", "\\_", "*", "\\*", "[", "\\[", "]", "\\]", "(", + "\\(", ")", "\\)", "~", "\\~", "`", "\\`", ">", "\\>", + "#", "\\#", "+", "\\+", "-", "\\-", "=", "\\=", "|", + "\\|", "{", "\\{", "}", "\\}", ".", "\\.", "!", "\\!", + ) + } else { + return "" + } + + return replacer.Replace(text) +} From 41e859781613898211b9b0d20d04a34c5fa27b69 Mon Sep 17 00:00:00 2001 From: Erik Pellizzon Date: Fri, 6 Nov 2020 11:25:32 +0100 Subject: [PATCH 2/3] Remove function from BotApi struct Co-authored-by: Syfaro --- bot.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot.go b/bot.go index f1abe30a..f4075157 100644 --- a/bot.go +++ b/bot.go @@ -1072,7 +1072,7 @@ func (bot *BotAPI) SetMyCommands(commands []BotCommand) error { // // 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 { +func EscapeText(parseMode string, text string) string { var replacer *strings.Replacer if parseMode == ModeHTML { From 7d4ae712ae1064f8ad4baa8f66105ac55de21ed0 Mon Sep 17 00:00:00 2001 From: Erik Pellizzon Date: Fri, 6 Nov 2020 22:07:29 +0100 Subject: [PATCH 3/3] Added empty string error return in docs --- bot.go | 1 + 1 file changed, 1 insertion(+) diff --git a/bot.go b/bot.go index f4075157..626024e1 100644 --- a/bot.go +++ b/bot.go @@ -1069,6 +1069,7 @@ func (bot *BotAPI) SetMyCommands(commands []BotCommand) error { // 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. +// If there is an error, an empty string will be returned. // // parseMode is the text formatting mode (ModeMarkdown, ModeMarkdownV2 or ModeHTML) // text is the input string that will be escaped