1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-12-23 01:20:11 +00:00

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

View File

@ -146,6 +146,9 @@ func (t *TelegramDaemon) QuoteReply(upd *tg.Update, content string) error {
return err 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. // 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 { func (t *TelegramDaemon) Send(message *Message, ID ...int64) error {
for _, id := range ID { for _, id := range ID {
@ -153,9 +156,7 @@ func (t *TelegramDaemon) Send(message *Message, ID ...int64) error {
if message.Markdown == "" { if message.Markdown == "" {
msg = tg.NewMessage(id, message.Text) msg = tg.NewMessage(id, message.Text)
} else { } else {
text := strings.ReplaceAll(message.Markdown, ".", "\\.") text := escaper.Replace(message.Markdown)
text = strings.ReplaceAll(text, "![", "[")
text = strings.ReplaceAll(text, "!", "\\!")
msg = tg.NewMessage(id, text) msg = tg.NewMessage(id, text)
msg.ParseMode = "MarkdownV2" msg.ParseMode = "MarkdownV2"
} }