Skip to content

Commit ead47cd

Browse files
Merge pull request #3183 from simonpasquier/fix-3061
telegram: use HTML template with HTML parse mode
2 parents aace20a + aa6a929 commit ead47cd

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

notify/telegram/telegram.go

+4
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ func (n *Notifier) Notify(ctx context.Context, alert ...*types.Alert) (bool, err
6969
tmpl = notify.TmplText(n.tmpl, data, &err)
7070
)
7171

72+
if n.conf.ParseMode == "HTML" {
73+
tmpl = notify.TmplHTML(n.tmpl, data, &err)
74+
}
75+
7276
key, ok := notify.GroupKey(ctx)
7377
if !ok {
7478
return false, fmt.Errorf("group key missing")

notify/telegram/telegram_test.go

+77
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,26 @@
1414
package telegram
1515

1616
import (
17+
"context"
18+
"encoding/json"
1719
"fmt"
20+
"io"
21+
"net/http"
22+
"net/http/httptest"
1823
"net/url"
1924
"testing"
25+
"time"
2026

2127
"github.com/go-kit/log"
2228
commoncfg "github.com/prometheus/common/config"
29+
"github.com/prometheus/common/model"
2330
"github.com/stretchr/testify/require"
2431
"gopkg.in/yaml.v2"
2532

2633
"github.com/prometheus/alertmanager/config"
34+
"github.com/prometheus/alertmanager/notify"
2735
"github.com/prometheus/alertmanager/notify/test"
36+
"github.com/prometheus/alertmanager/types"
2837
)
2938

3039
func TestTelegramUnmarshal(t *testing.T) {
@@ -73,3 +82,71 @@ func TestTelegramRetry(t *testing.T) {
7382
require.Equal(t, expected, actual, fmt.Sprintf("error on status %d", statusCode))
7483
}
7584
}
85+
86+
func TestTelegramNotify(t *testing.T) {
87+
for _, tc := range []struct {
88+
name string
89+
cfg config.TelegramConfig
90+
expText string
91+
}{
92+
{
93+
name: "No escaping by default",
94+
cfg: config.TelegramConfig{
95+
Message: "<code>x < y</code>",
96+
HTTPConfig: &commoncfg.HTTPClientConfig{},
97+
},
98+
expText: "<code>x < y</code>",
99+
},
100+
{
101+
name: "Characters escaped in HTML mode",
102+
cfg: config.TelegramConfig{
103+
ParseMode: "HTML",
104+
Message: "<code>x < y</code>",
105+
HTTPConfig: &commoncfg.HTTPClientConfig{},
106+
},
107+
expText: "<code>x &lt; y</code>",
108+
},
109+
} {
110+
t.Run(tc.name, func(t *testing.T) {
111+
var out []byte
112+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
113+
var err error
114+
out, err = io.ReadAll(r.Body)
115+
require.NoError(t, err)
116+
w.Write([]byte(`{"ok":true,"result":{"chat":{}}}`))
117+
}))
118+
defer srv.Close()
119+
u, _ := url.Parse(srv.URL)
120+
121+
tc.cfg.APIUrl = &config.URL{URL: u}
122+
123+
notifier, err := New(&tc.cfg, test.CreateTmpl(t), log.NewNopLogger())
124+
require.NoError(t, err)
125+
126+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
127+
defer cancel()
128+
ctx = notify.WithGroupKey(ctx, "1")
129+
130+
retry, err := notifier.Notify(ctx, []*types.Alert{
131+
{
132+
Alert: model.Alert{
133+
Labels: model.LabelSet{
134+
"lbl1": "val1",
135+
"lbl3": "val3",
136+
},
137+
StartsAt: time.Now(),
138+
EndsAt: time.Now().Add(time.Hour),
139+
},
140+
},
141+
}...)
142+
143+
require.False(t, retry)
144+
require.NoError(t, err)
145+
146+
req := map[string]string{}
147+
err = json.Unmarshal(out, &req)
148+
require.NoError(t, err)
149+
require.Equal(t, tc.expText, req["text"])
150+
})
151+
}
152+
}

0 commit comments

Comments
 (0)