diff --git a/alerting/available_channels.go b/alerting/available_channels.go index 948a7010..48c82c7b 100644 --- a/alerting/available_channels.go +++ b/alerting/available_channels.go @@ -696,6 +696,14 @@ func GetAvailableNotifiers() []*alerting.NotifierPlugin { Placeholder: `{{ template "default.message" . }}`, PropertyName: "message", }, + { // New in 9.1. + Label: "Title", + Element: alerting.ElementTypeInput, + InputType: alerting.InputTypeText, + Description: "Templated title of the message", + PropertyName: "title", + Placeholder: `{{ template "default.title" . }}`, + }, }, }, { diff --git a/alerting/channels/wecom.go b/alerting/channels/wecom.go index 12fb42ce..4d25c1b8 100644 --- a/alerting/channels/wecom.go +++ b/alerting/channels/wecom.go @@ -18,6 +18,7 @@ type WeComConfig struct { *NotificationChannelConfig URL string Message string + Title string } func WeComFactory(fc FactoryConfig) (NotificationChannel, error) { @@ -40,6 +41,7 @@ func NewWeComConfig(config *NotificationChannelConfig, decryptFunc GetDecryptedV NotificationChannelConfig: config, URL: url, Message: config.Settings.Get("message").MustString(`{{ template "default.message" .}}`), + Title: config.Settings.Get("title").MustString(DefaultMessageTitleEmbed), }, nil } @@ -55,6 +57,7 @@ func NewWeComNotifier(config *WeComConfig, ns notifications.WebhookSender, t *te }), URL: config.URL, Message: config.Message, + Title: config.Title, log: log.New("alerting.notifier.wecom"), ns: ns, tmpl: t, @@ -66,6 +69,7 @@ type WeComNotifier struct { *Base URL string Message string + Title string tmpl *template.Template log log.Logger ns notifications.WebhookSender @@ -82,7 +86,7 @@ func (w *WeComNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool, e "msgtype": "markdown", } content := fmt.Sprintf("# %s\n%s\n", - tmpl(DefaultMessageTitleEmbed), + tmpl(w.Title), tmpl(w.Message), ) diff --git a/alerting/channels/wecom_test.go b/alerting/channels/wecom_test.go index 4c7bfd2b..d5343f36 100644 --- a/alerting/channels/wecom_test.go +++ b/alerting/channels/wecom_test.go @@ -76,6 +76,33 @@ func TestWeComNotifier(t *testing.T) { "msgtype": "markdown", }, expMsgError: nil, + }, { + name: "Custom title and message with multiple alerts", + settings: `{ + "url": "http://localhost", + "message": "{{ len .Alerts.Firing }} alerts are firing, {{ len .Alerts.Resolved }} are resolved", + "title": "This notification is {{ .Status }}!" + }`, + alerts: []*types.Alert{ + { + Alert: model.Alert{ + Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val1"}, + Annotations: model.LabelSet{"ann1": "annv1"}, + }, + }, { + Alert: model.Alert{ + Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val2"}, + Annotations: model.LabelSet{"ann1": "annv2"}, + }, + }, + }, + expMsg: map[string]interface{}{ + "markdown": map[string]interface{}{ + "content": "# This notification is firing!\n2 alerts are firing, 0 are resolved\n", + }, + "msgtype": "markdown", + }, + expMsgError: nil, }, { name: "Error in initing", settings: `{}`,