Files
grafana/pkg/services/ngalert/notifier/channels/dingding.go
T
gotjosh 74fb491b6a Alerting: Validate contact point configuration during migration to Unified Alerting (#40717)
* Alerting: Validate contact point configuration during the migration

This minimises the chances of generating broken configuration as part of the migration. Originally, we wanted to generate it and not produce a hard stop in Grafana but this strategy has the chance to avoid delivering notifications for our users.

We now think it's better to hard stop the migration and let the user take care of resolving the configuration manually.
2021-10-22 10:11:06 +01:00

128 lines
3.2 KiB
Go

package channels
import (
"context"
"encoding/json"
"fmt"
"net/url"
"github.com/prometheus/alertmanager/template"
"github.com/prometheus/alertmanager/types"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
)
const defaultDingdingMsgType = "link"
// NewDingDingNotifier is the constructor for the Dingding notifier
func NewDingDingNotifier(model *NotificationChannelConfig, t *template.Template) (*DingDingNotifier, error) {
if model.Settings == nil {
return nil, receiverInitError{Reason: "no settings supplied", Cfg: *model}
}
url := model.Settings.Get("url").MustString()
if url == "" {
return nil, receiverInitError{Reason: "could not find url property in settings", Cfg: *model}
}
msgType := model.Settings.Get("msgType").MustString(defaultDingdingMsgType)
return &DingDingNotifier{
Base: NewBase(&models.AlertNotification{
Uid: model.UID,
Name: model.Name,
Type: model.Type,
DisableResolveMessage: model.DisableResolveMessage,
Settings: model.Settings,
}),
MsgType: msgType,
URL: url,
Message: model.Settings.Get("message").MustString(`{{ template "default.message" .}}`),
log: log.New("alerting.notifier.dingding"),
tmpl: t,
}, nil
}
// DingDingNotifier is responsible for sending alert notifications to ding ding.
type DingDingNotifier struct {
*Base
MsgType string
URL string
Message string
tmpl *template.Template
log log.Logger
}
// Notify sends the alert notification to dingding.
func (dd *DingDingNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
dd.log.Info("Sending dingding")
ruleURL := joinUrlPath(dd.tmpl.ExternalURL.String(), "/alerting/list", dd.log)
q := url.Values{
"pc_slide": {"false"},
"url": {ruleURL},
}
// Use special link to auto open the message url outside of Dingding
// Refer: https://open-doc.dingtalk.com/docs/doc.htm?treeId=385&articleId=104972&docType=1#s9
messageURL := "dingtalk://dingtalkclient/page/link?" + q.Encode()
var tmplErr error
tmpl, _ := TmplText(ctx, dd.tmpl, as, dd.log, &tmplErr)
message := tmpl(dd.Message)
title := tmpl(`{{ template "default.title" . }}`)
var bodyMsg map[string]interface{}
if tmpl(dd.MsgType) == "actionCard" {
bodyMsg = map[string]interface{}{
"msgtype": "actionCard",
"actionCard": map[string]string{
"text": message,
"title": title,
"singleTitle": "More",
"singleURL": messageURL,
},
}
} else {
link := map[string]string{
"text": message,
"title": title,
"messageUrl": messageURL,
}
bodyMsg = map[string]interface{}{
"msgtype": "link",
"link": link,
}
}
u := tmpl(dd.URL)
if tmplErr != nil {
dd.log.Debug("failed to template DingDing message", "err", tmplErr.Error())
}
body, err := json.Marshal(bodyMsg)
if err != nil {
return false, err
}
cmd := &models.SendWebhookSync{
Url: u,
Body: string(body),
}
if err := bus.DispatchCtx(ctx, cmd); err != nil {
return false, fmt.Errorf("send notification to dingding: %w", err)
}
return true, nil
}
func (dd *DingDingNotifier) SendResolved() bool {
return !dd.GetDisableResolveMessage()
}