notify/telegram: Update to use default telegram config

Signed-off-by: Matthias Loibl <mail@matthiasloibl.com>
This commit is contained in:
Matthias Loibl 2022-06-03 12:04:19 +02:00
parent 34e60d2c80
commit 3ccff53692
No known key found for this signature in database
GPG Key ID: 78A796CA74CA38BA
3 changed files with 26 additions and 19 deletions

View File

@ -143,7 +143,7 @@ var (
},
DisableNotifications: false,
Message: `{{ template "telegram.default.message" . }}`,
ParseMode: "MarkdownV2",
ParseMode: "HTML",
}
)
@ -661,9 +661,6 @@ func (c *TelegramConfig) UnmarshalYAML(unmarshal func(interface{}) error) error
if c.ChatID == 0 {
return fmt.Errorf("missing chat_id on telegram_config")
}
if c.APIUrl == nil {
return fmt.Errorf("missing api_url on telegram_config")
}
if c.ParseMode != "" &&
c.ParseMode != "Markdown" &&
c.ParseMode != "MarkdownV2" &&

View File

@ -16,7 +16,6 @@ package telegram
import (
"context"
"net/http"
"net/url"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
@ -45,19 +44,6 @@ func New(conf *config.TelegramConfig, t *template.Template, l log.Logger, httpOp
return nil, err
}
if conf.APIUrl == nil {
apiURL, err := url.Parse("https://api.telegram.org")
if err != nil {
return nil, err
}
conf.APIUrl = &config.URL{URL: apiURL}
}
if conf.ParseMode == "" {
// The default Telegram template is in HTML,
// so we need to set the parse mode to HTML for Telegram not to return a parse error.
conf.ParseMode = "HTML"
}
client, err := createTelegramClient(conf.BotToken, conf.APIUrl.String(), conf.ParseMode, httpclient)
if err != nil {
return nil, err

View File

@ -21,13 +21,37 @@ import (
"github.com/go-kit/log"
commoncfg "github.com/prometheus/common/config"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
"github.com/prometheus/alertmanager/config"
"github.com/prometheus/alertmanager/notify/test"
)
func TestTelegramUnmarshal(t *testing.T) {
in := `
route:
receiver: test
receivers:
- name: test
telegram_configs:
- chat_id: 1234
bot_token: secret
`
var c config.Config
err := yaml.Unmarshal([]byte(in), &c)
require.NoError(t, err)
require.Len(t, c.Receivers, 1)
require.Len(t, c.Receivers[0].TelegramConfigs, 1)
require.Equal(t, "https://api.telegram.org", c.Receivers[0].TelegramConfigs[0].APIUrl.String())
require.Equal(t, config.Secret("secret"), c.Receivers[0].TelegramConfigs[0].BotToken)
require.Equal(t, int64(1234), c.Receivers[0].TelegramConfigs[0].ChatID)
require.Equal(t, "MarkdownV2", c.Receivers[0].TelegramConfigs[0].ParseMode)
}
func TestTelegramRetry(t *testing.T) {
// Fake url for testing purpouses
// Fake url for testing purposes
fakeURL := config.URL{
URL: &url.URL{
Scheme: "https",