浏览代码

Fixes #13993 - adds more options for Slack notifications

Andrey Kaipov 7 年之前
父节点
当前提交
63fdffe8c0
共有 2 个文件被更改,包括 61 次插入1 次删除
  1. 51 0
      pkg/services/alerting/notifiers/slack.go
  2. 10 1
      pkg/services/alerting/notifiers/slack_test.go

+ 51 - 0
pkg/services/alerting/notifiers/slack.go

@@ -39,6 +39,39 @@ func init() {
           Override default channel or user, use #channel-name or @username
         </info-popover>
       </div>
+      <div class="gf-form max-width-30">
+        <span class="gf-form-label width-6">Username</span>
+        <input type="text"
+          class="gf-form-input max-width-30"
+          ng-model="ctrl.model.settings.username"
+          data-placement="right">
+        </input>
+        <info-popover mode="right-absolute">
+          Set the username for the bot's message
+        </info-popover>
+      </div>
+      <div class="gf-form max-width-30">
+        <span class="gf-form-label width-6">Icon emoji</span>
+        <input type="text"
+          class="gf-form-input max-width-30"
+          ng-model="ctrl.model.settings.icon_emoji"
+          data-placement="right">
+        </input>
+        <info-popover mode="right-absolute">
+          Provide an emoji to use as the icon for the bot's message. Overrides the icon URL
+        </info-popover>
+      </div>
+      <div class="gf-form max-width-30">
+        <span class="gf-form-label width-6">Icon URL</span>
+        <input type="text"
+          class="gf-form-input max-width-30"
+          ng-model="ctrl.model.settings.icon_url"
+          data-placement="right">
+        </input>
+        <info-popover mode="right-absolute">
+          Provide a URL to an image to use as the icon for the bot's message
+        </info-popover>
+      </div>
       <div class="gf-form max-width-30">
         <span class="gf-form-label width-6">Mention</span>
         <input type="text"
@@ -73,6 +106,9 @@ func NewSlackNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
 	}
 
 	recipient := model.Settings.Get("recipient").MustString()
+	username := model.Settings.Get("username").MustString()
+	iconEmoji := model.Settings.Get("icon_emoji").MustString()
+	iconUrl := model.Settings.Get("icon_url").MustString()
 	mention := model.Settings.Get("mention").MustString()
 	token := model.Settings.Get("token").MustString()
 	uploadImage := model.Settings.Get("uploadImage").MustBool(true)
@@ -81,6 +117,9 @@ func NewSlackNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
 		NotifierBase: NewNotifierBase(model),
 		Url:          url,
 		Recipient:    recipient,
+		Username:     username,
+		IconEmoji:    iconEmoji,
+		IconUrl:      iconUrl,
 		Mention:      mention,
 		Token:        token,
 		Upload:       uploadImage,
@@ -92,6 +131,9 @@ type SlackNotifier struct {
 	NotifierBase
 	Url       string
 	Recipient string
+	Username  string
+	IconEmoji string
+	IconUrl   string
 	Mention   string
 	Token     string
 	Upload    bool
@@ -160,6 +202,15 @@ func (this *SlackNotifier) Notify(evalContext *alerting.EvalContext) error {
 	if this.Recipient != "" {
 		body["channel"] = this.Recipient
 	}
+	if this.Username != "" {
+		body["username"] = this.Username
+	}
+	if this.IconEmoji != "" {
+		body["icon_emoji"] = this.IconEmoji
+	}
+	if this.IconUrl != "" {
+		body["icon_url"] = this.IconUrl
+	}
 	data, _ := json.Marshal(&body)
 	cmd := &m.SendWebhookSync{Url: this.Url, Body: string(data)}
 	if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {

+ 10 - 1
pkg/services/alerting/notifiers/slack_test.go

@@ -47,15 +47,21 @@ func TestSlackNotifier(t *testing.T) {
 				So(slackNotifier.Type, ShouldEqual, "slack")
 				So(slackNotifier.Url, ShouldEqual, "http://google.com")
 				So(slackNotifier.Recipient, ShouldEqual, "")
+				So(slackNotifier.Username, ShouldEqual, "")
+				So(slackNotifier.IconEmoji, ShouldEqual, "")
+				So(slackNotifier.IconUrl, ShouldEqual, "")
 				So(slackNotifier.Mention, ShouldEqual, "")
 				So(slackNotifier.Token, ShouldEqual, "")
 			})
 
-			Convey("from settings with Recipient, Mention, and Token", func() {
+			Convey("from settings with Recipient, Username, IconEmoji, IconUrl, Mention, and Token", func() {
 				json := `
 				{
           "url": "http://google.com",
           "recipient": "#ds-opentsdb",
+          "username": "Grafana Alerts",
+          "icon_emoji": ":smile:",
+          "icon_url": "https://grafana.com/img/fav32.png",
           "mention": "@carl",
           "token": "xoxb-XXXXXXXX-XXXXXXXX-XXXXXXXXXX"
 				}`
@@ -75,6 +81,9 @@ func TestSlackNotifier(t *testing.T) {
 				So(slackNotifier.Type, ShouldEqual, "slack")
 				So(slackNotifier.Url, ShouldEqual, "http://google.com")
 				So(slackNotifier.Recipient, ShouldEqual, "#ds-opentsdb")
+				So(slackNotifier.Username, ShouldEqual, "Grafana Alerts")
+				So(slackNotifier.IconEmoji, ShouldEqual, ":smile:")
+				So(slackNotifier.IconUrl, ShouldEqual, "https://grafana.com/img/fav32.png")
 				So(slackNotifier.Mention, ShouldEqual, "@carl")
 				So(slackNotifier.Token, ShouldEqual, "xoxb-XXXXXXXX-XXXXXXXX-XXXXXXXXXX")
 			})