Fix Telegram notification error (#162)

This commit is contained in:
CrazyMax
2020-08-28 23:43:24 +02:00
parent a82cef7065
commit 17f72c7bb9

View File

@@ -3,6 +3,7 @@ package telegram
import (
"bytes"
"fmt"
"strings"
"text/template"
"github.com/crazy-max/diun/v4/internal/model"
@@ -45,13 +46,13 @@ func (c *Client) Send(entry model.NotifEntry) error {
}
var msgBuf bytes.Buffer
msgTpl := template.Must(template.New("email").Parse(fmt.Sprintf("Docker tag %s which you subscribed to through {{ .Entry.Provider }} provider has been {{ if (eq .Entry.Status \"new\") }}newly added{{ else }}updated{{ end }} on {{ .Meta.Hostname }}.", tagTpl)))
msgTpl := template.Must(template.New("email").Parse(fmt.Sprintf("Docker tag %s which you subscribed to through {{ .Entry.Provider }} provider has been {{ if (eq .Entry.Status \"new\") }}newly added{{ else }}updated{{ end }} on {{ .Hostname }}.", tagTpl)))
if err := msgTpl.Execute(&msgBuf, struct {
Meta model.Meta
Entry model.NotifEntry
Hostname string
Entry model.NotifEntry
}{
Meta: c.meta,
Entry: entry,
Hostname: escapeMarkdown(c.meta.Hostname),
Entry: entry,
}); err != nil {
return err
}
@@ -72,3 +73,11 @@ func (c *Client) Send(entry model.NotifEntry) error {
return nil
}
func escapeMarkdown(txt string) string {
txt = strings.ReplaceAll(txt, "_", "\\_")
txt = strings.ReplaceAll(txt, "*", "\\*")
txt = strings.ReplaceAll(txt, "[", "\\[")
txt = strings.ReplaceAll(txt, "`", "\\`")
return txt
}