From aa6a9293164bb28c785aa19ea0183dbf27b92705 Mon Sep 17 00:00:00 2001 From: Simon Pasquier Date: Tue, 20 Dec 2022 17:17:40 +0100 Subject: [PATCH] telegram: use HTML template with HTML parse mode Closes #3061 Signed-off-by: Simon Pasquier --- notify/telegram/telegram.go | 4 ++ notify/telegram/telegram_test.go | 77 ++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/notify/telegram/telegram.go b/notify/telegram/telegram.go index 90e0ca35af..64f6ce806d 100644 --- a/notify/telegram/telegram.go +++ b/notify/telegram/telegram.go @@ -69,6 +69,10 @@ func (n *Notifier) Notify(ctx context.Context, alert ...*types.Alert) (bool, err tmpl = notify.TmplText(n.tmpl, data, &err) ) + if n.conf.ParseMode == "HTML" { + tmpl = notify.TmplHTML(n.tmpl, data, &err) + } + key, ok := notify.GroupKey(ctx) if !ok { return false, fmt.Errorf("group key missing") diff --git a/notify/telegram/telegram_test.go b/notify/telegram/telegram_test.go index ecae7774f0..c7a8f3330c 100644 --- a/notify/telegram/telegram_test.go +++ b/notify/telegram/telegram_test.go @@ -14,17 +14,26 @@ package telegram import ( + "context" + "encoding/json" "fmt" + "io" + "net/http" + "net/http/httptest" "net/url" "testing" + "time" "github.com/go-kit/log" commoncfg "github.com/prometheus/common/config" + "github.com/prometheus/common/model" "github.com/stretchr/testify/require" "gopkg.in/yaml.v2" "github.com/prometheus/alertmanager/config" + "github.com/prometheus/alertmanager/notify" "github.com/prometheus/alertmanager/notify/test" + "github.com/prometheus/alertmanager/types" ) func TestTelegramUnmarshal(t *testing.T) { @@ -73,3 +82,71 @@ func TestTelegramRetry(t *testing.T) { require.Equal(t, expected, actual, fmt.Sprintf("error on status %d", statusCode)) } } + +func TestTelegramNotify(t *testing.T) { + for _, tc := range []struct { + name string + cfg config.TelegramConfig + expText string + }{ + { + name: "No escaping by default", + cfg: config.TelegramConfig{ + Message: "x < y", + HTTPConfig: &commoncfg.HTTPClientConfig{}, + }, + expText: "x < y", + }, + { + name: "Characters escaped in HTML mode", + cfg: config.TelegramConfig{ + ParseMode: "HTML", + Message: "x < y", + HTTPConfig: &commoncfg.HTTPClientConfig{}, + }, + expText: "x < y", + }, + } { + t.Run(tc.name, func(t *testing.T) { + var out []byte + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + var err error + out, err = io.ReadAll(r.Body) + require.NoError(t, err) + w.Write([]byte(`{"ok":true,"result":{"chat":{}}}`)) + })) + defer srv.Close() + u, _ := url.Parse(srv.URL) + + tc.cfg.APIUrl = &config.URL{URL: u} + + notifier, err := New(&tc.cfg, test.CreateTmpl(t), log.NewNopLogger()) + require.NoError(t, err) + + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + ctx = notify.WithGroupKey(ctx, "1") + + retry, err := notifier.Notify(ctx, []*types.Alert{ + { + Alert: model.Alert{ + Labels: model.LabelSet{ + "lbl1": "val1", + "lbl3": "val3", + }, + StartsAt: time.Now(), + EndsAt: time.Now().Add(time.Hour), + }, + }, + }...) + + require.False(t, retry) + require.NoError(t, err) + + req := map[string]string{} + err = json.Unmarshal(out, &req) + require.NoError(t, err) + require.Equal(t, tc.expText, req["text"]) + }) + } +}