mirror of
				https://github.com/grafana/grafana.git
				synced 2025-02-25 18:55:37 -06:00 
			
		
		
		
	Alerting: Support for configuring content field for Discord alert notifier (#17017)
This commit is contained in:
		
				
					committed by
					
						
						Carl Bergquist
					
				
			
			
				
	
			
			
			
						parent
						
							a9c94ec93b
						
					
				
				
					commit
					ca6151e23f
				
			@@ -24,15 +24,27 @@ func init() {
 | 
			
		||||
		Factory:     NewDiscordNotifier,
 | 
			
		||||
		OptionsTemplate: `
 | 
			
		||||
      <h3 class="page-heading">Discord settings</h3>
 | 
			
		||||
      <div class="gf-form">
 | 
			
		||||
        <span class="gf-form-label width-14">Webhook URL</span>
 | 
			
		||||
        <input type="text" required class="gf-form-input max-width-22" ng-model="ctrl.model.settings.url" placeholder="Discord webhook URL"></input>
 | 
			
		||||
      <div class="gf-form max-width-30">
 | 
			
		||||
        <span class="gf-form-label width-10">Message Content</span>
 | 
			
		||||
        <input type="text"
 | 
			
		||||
          class="gf-form-input max-width-30"
 | 
			
		||||
          ng-model="ctrl.model.settings.content"
 | 
			
		||||
          data-placement="right">
 | 
			
		||||
        </input>
 | 
			
		||||
        <info-popover mode="right-absolute">
 | 
			
		||||
          Mention a group using @ or a user using <@ID> when notifying in a channel
 | 
			
		||||
        </info-popover>
 | 
			
		||||
      </div>
 | 
			
		||||
      <div class="gf-form  max-width-30">
 | 
			
		||||
        <span class="gf-form-label width-10">Webhook URL</span>
 | 
			
		||||
        <input type="text" required class="gf-form-input max-width-30" ng-model="ctrl.model.settings.url" placeholder="Discord webhook URL"></input>
 | 
			
		||||
      </div>
 | 
			
		||||
    `,
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewDiscordNotifier(model *models.AlertNotification) (alerting.Notifier, error) {
 | 
			
		||||
	content := model.Settings.Get("content").MustString()
 | 
			
		||||
	url := model.Settings.Get("url").MustString()
 | 
			
		||||
	if url == "" {
 | 
			
		||||
		return nil, alerting.ValidationError{Reason: "Could not find webhook url property in settings"}
 | 
			
		||||
@@ -40,6 +52,7 @@ func NewDiscordNotifier(model *models.AlertNotification) (alerting.Notifier, err
 | 
			
		||||
 | 
			
		||||
	return &DiscordNotifier{
 | 
			
		||||
		NotifierBase: NewNotifierBase(model),
 | 
			
		||||
		Content:      content,
 | 
			
		||||
		WebhookURL:   url,
 | 
			
		||||
		log:          log.New("alerting.notifier.discord"),
 | 
			
		||||
	}, nil
 | 
			
		||||
@@ -47,6 +60,7 @@ func NewDiscordNotifier(model *models.AlertNotification) (alerting.Notifier, err
 | 
			
		||||
 | 
			
		||||
type DiscordNotifier struct {
 | 
			
		||||
	NotifierBase
 | 
			
		||||
	Content    string
 | 
			
		||||
	WebhookURL string
 | 
			
		||||
	log        log.Logger
 | 
			
		||||
}
 | 
			
		||||
@@ -63,6 +77,10 @@ func (this *DiscordNotifier) Notify(evalContext *alerting.EvalContext) error {
 | 
			
		||||
	bodyJSON := simplejson.New()
 | 
			
		||||
	bodyJSON.Set("username", "Grafana")
 | 
			
		||||
 | 
			
		||||
	if this.Content != "" {
 | 
			
		||||
		bodyJSON.Set("content", this.Content)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fields := make([]map[string]interface{}, 0)
 | 
			
		||||
 | 
			
		||||
	for _, evt := range evalContext.EvalMatches {
 | 
			
		||||
 
 | 
			
		||||
@@ -29,7 +29,8 @@ func TestDiscordNotifier(t *testing.T) {
 | 
			
		||||
			Convey("settings should trigger incident", func() {
 | 
			
		||||
				json := `
 | 
			
		||||
				{
 | 
			
		||||
          "url": "https://web.hook/"
 | 
			
		||||
					"content": "@everyone Please check this notification",
 | 
			
		||||
					"url": "https://web.hook/"
 | 
			
		||||
				}`
 | 
			
		||||
 | 
			
		||||
				settingsJSON, _ := simplejson.NewJson([]byte(json))
 | 
			
		||||
@@ -45,6 +46,7 @@ func TestDiscordNotifier(t *testing.T) {
 | 
			
		||||
				So(err, ShouldBeNil)
 | 
			
		||||
				So(discordNotifier.Name, ShouldEqual, "discord_testing")
 | 
			
		||||
				So(discordNotifier.Type, ShouldEqual, "discord")
 | 
			
		||||
				So(discordNotifier.Content, ShouldEqual, "@everyone Please check this notification")
 | 
			
		||||
				So(discordNotifier.WebhookURL, ShouldEqual, "https://web.hook/")
 | 
			
		||||
			})
 | 
			
		||||
		})
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user