Telegram: Escape all necessary characters

Fixes #108.
This commit is contained in:
Harvey Tindall 2021-05-25 23:03:13 +01:00
parent 7035a3fe9c
commit fb6256d1ed
Signed by: hrfee
GPG Key ID: BBC65952848FB1A2
1 changed files with 4 additions and 3 deletions

View File

@ -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"
}