Skip to content

Commit

Permalink
Alerting: Add custom templated title to Wecom notifier (#51529)
Browse files Browse the repository at this point in the history
* add custom title in wecom channel

* add wecom test case and setting config in ui

* Update pkg/services/ngalert/notifier/channels/wecom_test.go

Co-authored-by: Matthew Jacobson <JacobsonMT@gmail.com>

* change version in comment

* Update pkg/services/ngalert/notifier/available_channels.go

Co-authored-by: Matthew Jacobson <JacobsonMT@gmail.com>

* format

Co-authored-by: Matthew Jacobson <JacobsonMT@gmail.com>
  • Loading branch information
2 people authored and gotjosh committed Jul 14, 2022
1 parent e8319b5 commit b63f072
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
8 changes: 8 additions & 0 deletions alerting/available_channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" . }}`,
},
},
},
{
Expand Down
6 changes: 5 additions & 1 deletion alerting/channels/wecom.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type WeComConfig struct {
*NotificationChannelConfig
URL string
Message string
Title string
}

func WeComFactory(fc FactoryConfig) (NotificationChannel, error) {
Expand All @@ -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
}

Expand All @@ -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,
Expand All @@ -66,6 +69,7 @@ type WeComNotifier struct {
*Base
URL string
Message string
Title string
tmpl *template.Template
log log.Logger
ns notifications.WebhookSender
Expand All @@ -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),
)

Expand Down
27 changes: 27 additions & 0 deletions alerting/channels/wecom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: `{}`,
Expand Down

0 comments on commit b63f072

Please sign in to comment.