From fb6256d1ed6f5680a0e66d0ec28b316c27d8a2aa Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Tue, 25 May 2021 23:03:13 +0100 Subject: [PATCH] Telegram: Escape all necessary characters Fixes #108. --- telegram.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/telegram.go b/telegram.go index 1975757..4cca16f 100644 --- a/telegram.go +++ b/telegram.go @@ -146,6 +146,9 @@ func (t *TelegramDaemon) QuoteReply(upd *tg.Update, content string) error { return err } +var escapedChars = []string{"_", "\\_", "*", "\\*", "[", "\\[", "]", "\\]", "(", "\\(", ")", "\\)", "~", "\\~", "`", "\\`", ">", "\\>", "#", "\\#", "+", "\\+", "-", "\\-", "=", "\\=", "|", "\\|", "{", "\\{", "}", "\\}", ".", "\\.", "!", "\\!"} +var escaper = strings.NewReplacer(escapedChars...) + // Send will send a telegram message to a list of chat IDs. message.text is used if no markdown is given. func (t *TelegramDaemon) Send(message *Message, ID ...int64) error { for _, id := range ID { @@ -153,9 +156,7 @@ func (t *TelegramDaemon) Send(message *Message, ID ...int64) error { if message.Markdown == "" { msg = tg.NewMessage(id, message.Text) } else { - text := strings.ReplaceAll(message.Markdown, ".", "\\.") - text = strings.ReplaceAll(text, "![", "[") - text = strings.ReplaceAll(text, "!", "\\!") + text := escaper.Replace(message.Markdown) msg = tg.NewMessage(id, text) msg.ParseMode = "MarkdownV2" }