diff --git a/receivers/telegram/config.go b/receivers/telegram/config.go index fc94a041..1e47f4d6 100644 --- a/receivers/telegram/config.go +++ b/receivers/telegram/config.go @@ -26,6 +26,7 @@ type Config struct { DisableWebPagePreview bool `json:"disable_web_page_preview,omitempty" yaml:"disable_web_page_preview,omitempty"` ProtectContent bool `json:"protect_content,omitempty" yaml:"protect_content,omitempty"` DisableNotifications bool `json:"disable_notifications,omitempty" yaml:"disable_notifications,omitempty"` + IncludeScreenshotURL bool `json:"include_screenshot_url,omitempty" yaml:"include_screenshot_url,omitempty"` } func NewConfig(jsonData json.RawMessage, decryptFn receivers.DecryptFunc) (Config, error) { diff --git a/receivers/telegram/config_test.go b/receivers/telegram/config_test.go index cd8fe70c..411b8640 100644 --- a/receivers/telegram/config_test.go +++ b/receivers/telegram/config_test.go @@ -50,6 +50,7 @@ func TestNewConfig(t *testing.T) { DisableWebPagePreview: false, ProtectContent: false, DisableNotifications: false, + IncludeScreenshotURL: false, }, }, { @@ -66,6 +67,7 @@ func TestNewConfig(t *testing.T) { DisableWebPagePreview: false, ProtectContent: false, DisableNotifications: false, + IncludeScreenshotURL: false, }, }, { @@ -82,6 +84,7 @@ func TestNewConfig(t *testing.T) { DisableWebPagePreview: false, ProtectContent: false, DisableNotifications: false, + IncludeScreenshotURL: false, }, }, { @@ -103,6 +106,7 @@ func TestNewConfig(t *testing.T) { DisableWebPagePreview: false, ProtectContent: false, DisableNotifications: false, + IncludeScreenshotURL: false, }, }, { @@ -117,6 +121,7 @@ func TestNewConfig(t *testing.T) { DisableWebPagePreview: true, ProtectContent: true, DisableNotifications: true, + IncludeScreenshotURL: true, }, }, { @@ -132,6 +137,7 @@ func TestNewConfig(t *testing.T) { DisableWebPagePreview: true, ProtectContent: true, DisableNotifications: true, + IncludeScreenshotURL: true, }, }, { @@ -156,6 +162,7 @@ func TestNewConfig(t *testing.T) { DisableWebPagePreview: false, ProtectContent: false, DisableNotifications: false, + IncludeScreenshotURL: false, }, }, { @@ -172,6 +179,7 @@ func TestNewConfig(t *testing.T) { DisableWebPagePreview: false, ProtectContent: false, DisableNotifications: false, + IncludeScreenshotURL: false, }, }, { @@ -188,6 +196,7 @@ func TestNewConfig(t *testing.T) { DisableWebPagePreview: false, ProtectContent: false, DisableNotifications: false, + IncludeScreenshotURL: false, }, }, { diff --git a/receivers/telegram/telegram.go b/receivers/telegram/telegram.go index db688cff..5a869216 100644 --- a/receivers/telegram/telegram.go +++ b/receivers/telegram/telegram.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "mime/multipart" + "net/url" "os" "github.com/prometheus/alertmanager/notify" @@ -73,7 +74,13 @@ func (tn *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error return false, fmt.Errorf("failed to send telegram message: %w", err) } + // IncludeScreenshotURL set to true, no need to upload images. + if tn.settings.IncludeScreenshotURL { + return true, nil + } + // Create the cmd to upload each image + // Works only if IncludeScreenshotURL is set to false. _ = images.WithStoredImages(ctx, tn.log, tn.images, func(_ int, image images.Image) error { cmd, err = tn.newWebhookSyncCmd("sendPhoto", func(w *multipart.Writer) error { f, err := os.Open(image.Path) @@ -116,7 +123,18 @@ func (tn *Notifier) buildTelegramMessage(ctx context.Context, as []*types.Alert) tmpl, _ := templates.TmplText(ctx, tn.tmpl, as, tn.log, &tmplErr) // Telegram supports 4096 chars max - messageText, truncated := receivers.TruncateInRunes(tmpl(tn.settings.Message), telegramMaxMessageLenRunes) + rawMessage := tmpl(tn.settings.Message) + if tn.settings.IncludeScreenshotURL { + _ = images.WithStoredImages(ctx, tn.log, tn.images, func(_ int, image images.Image) error { + // Change to add the template ImageURL + imageURL, err := url.Parse(image.URL) + if err == nil { + tn.tmpl.ExternalURL = imageURL + } + return nil + }, as...) + } + messageText, truncated := receivers.TruncateInRunes(rawMessage, telegramMaxMessageLenRunes) if truncated { key, err := notify.ExtractGroupKey(ctx) if err != nil { diff --git a/receivers/telegram/telegram_test.go b/receivers/telegram/telegram_test.go index 34effd1b..436c4e1f 100644 --- a/receivers/telegram/telegram_test.go +++ b/receivers/telegram/telegram_test.go @@ -50,6 +50,7 @@ func TestNotify(t *testing.T) { DisableWebPagePreview: true, ProtectContent: true, DisableNotifications: true, + IncludeScreenshotURL: false, }, alerts: []*types.Alert{ { @@ -131,6 +132,38 @@ func TestNotify(t *testing.T) { "text": strings.Repeat("1", 4096-1) + "…", }}, expMsgError: nil, + }, { + name: "A single alert with default template and inline image URL", + settings: Config{ + BotToken: "abcdefgh0123456789", + ChatID: "someid", + MessageThreadID: "threadid", + Message: templates.DefaultMessageEmbed, + ParseMode: "Markdown", + DisableWebPagePreview: true, + ProtectContent: true, + DisableNotifications: true, + IncludeScreenshotURL: true, + }, + alerts: []*types.Alert{ + { + Alert: model.Alert{ + Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val1"}, + Annotations: model.LabelSet{"ann1": "annv1", "__dashboardUid__": "abcd", "__panelId__": "efgh", "__alertImageToken__": "test-image-1"}, + GeneratorURL: "a URL", + }, + }, + }, + expMsg: []map[string]string{{ + "chat_id": "someid", + "message_thread_id": "threadid", + "parse_mode": "Markdown", + "text": "**Firing**\n\nValue: [no value]\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: a URL\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matcher=alertname%3Dalert1&matcher=lbl1%3Dval1\nDashboard: http://localhost/d/abcd\nPanel: http://localhost/d/abcd?viewPanel=efgh\n https://www.example.com/test-image-1", + "disable_web_page_preview": "true", + "protect_content": "true", + "disable_notification": "true", + }}, + expMsgError: nil, }, } diff --git a/receivers/telegram/testing.go b/receivers/telegram/testing.go index 55714627..325e4249 100644 --- a/receivers/telegram/testing.go +++ b/receivers/telegram/testing.go @@ -9,7 +9,8 @@ const FullValidConfigForTesting = `{ "parse_mode" :"html", "disable_web_page_preview" :true, "protect_content" :true, - "disable_notifications" :true + "disable_notifications" :true, + "include_screenshot_url" :true }` // FullValidSecretsForTesting is a string representation of JSON object that contains all fields that can be overridden from secrets