grafana/pkg/services/alerting/notifiers/dingding.go

159 lines
3.9 KiB
Go
Raw Normal View History

package notifiers
import (
"encoding/json"
"fmt"
"net/url"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
)
const defaultDingdingMsgType = "link"
2018-10-25 05:24:04 -05:00
func init() {
alerting.RegisterNotifier(&alerting.NotifierPlugin{
Type: "dingding",
Name: "DingDing",
Description: "Sends HTTP POST request to DingDing",
Heading: "DingDing settings",
Factory: newDingDingNotifier,
Options: []alerting.NotifierOption{
{
Label: "Url",
Element: alerting.ElementTypeInput,
InputType: alerting.InputTypeText,
Placeholder: "https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxx",
PropertyName: "url",
Required: true,
},
{
Label: "Message Type",
Element: alerting.ElementTypeSelect,
PropertyName: "msgType",
SelectOptions: []alerting.SelectOption{
{
Value: "link",
Label: "Link"},
{
Value: "actionCard",
Label: "ActionCard",
},
},
},
},
})
}
Encryption: Refactor securejsondata.SecureJsonData to stop relying on global functions (#38865) * Encryption: Add support to encrypt/decrypt sjd * Add datasources.Service as a proxy to datasources db operations * Encrypt ds.SecureJsonData before calling SQLStore * Move ds cache code into ds service * Fix tlsmanager tests * Fix pluginproxy tests * Remove some securejsondata.GetEncryptedJsonData usages * Add pluginsettings.Service as a proxy for plugin settings db operations * Add AlertNotificationService as a proxy for alert notification db operations * Remove some securejsondata.GetEncryptedJsonData usages * Remove more securejsondata.GetEncryptedJsonData usages * Fix lint errors * Minor fixes * Remove encryption global functions usages from ngalert * Fix lint errors * Minor fixes * Minor fixes * Remove securejsondata.DecryptedValue usage * Refactor the refactor * Remove securejsondata.DecryptedValue usage * Move securejsondata to migrations package * Move securejsondata to migrations package * Minor fix * Fix integration test * Fix integration tests * Undo undesired changes * Fix tests * Add context.Context into encryption methods * Fix tests * Fix tests * Fix tests * Trigger CI * Fix test * Add names to params of encryption service interface * Remove bus from CacheServiceImpl * Add logging * Add keys to logger Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com> * Add missing key to logger Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com> * Undo changes in markdown files * Fix formatting * Add context to secrets service * Rename decryptSecureJsonData to decryptSecureJsonDataFn * Name args in GetDecryptedValueFn * Add template back to NewAlertmanagerNotifier * Copy GetDecryptedValueFn to ngalert * Add logging to pluginsettings * Fix pluginsettings test Co-authored-by: Tania B <yalyna.ts@gmail.com> Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com>
2021-10-07 09:33:50 -05:00
func newDingDingNotifier(model *models.AlertNotification, _ alerting.GetDecryptedValueFn) (alerting.Notifier, error) {
url := model.Settings.Get("url").MustString()
if url == "" {
return nil, alerting.ValidationError{Reason: "Could not find url property in settings"}
}
msgType := model.Settings.Get("msgType").MustString(defaultDingdingMsgType)
return &DingDingNotifier{
NotifierBase: NewNotifierBase(model),
MsgType: msgType,
URL: url,
log: log.New("alerting.notifier.dingding"),
}, nil
}
// DingDingNotifier is responsible for sending alert notifications to ding ding.
type DingDingNotifier struct {
NotifierBase
MsgType string
URL string
log log.Logger
}
// Notify sends the alert notification to dingding.
func (dd *DingDingNotifier) Notify(evalContext *alerting.EvalContext) error {
dd.log.Info("Sending dingding")
messageURL, err := evalContext.GetRuleURL()
if err != nil {
dd.log.Error("Failed to get messageUrl", "error", err, "dingding", dd.Name)
messageURL = ""
}
body, err := dd.genBody(evalContext, messageURL)
if err != nil {
return err
}
cmd := &models.SendWebhookSync{
Url: dd.URL,
Body: string(body),
}
if err := bus.Dispatch(evalContext.Ctx, cmd); err != nil {
dd.log.Error("Failed to send DingDing", "error", err, "dingding", dd.Name)
return err
}
return nil
}
func (dd *DingDingNotifier) genBody(evalContext *alerting.EvalContext, messageURL string) ([]byte, error) {
q := url.Values{
"pc_slide": {"false"},
"url": {messageURL},
}
// 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()
dd.log.Info("messageUrl:" + messageURL)
message := evalContext.Rule.Message
picURL := evalContext.ImagePublicURL
title := evalContext.GetNotificationTitle()
2018-10-19 04:17:38 -05:00
if message == "" {
message = title
}
for i, match := range evalContext.EvalMatches {
message += fmt.Sprintf("\n%2d. %s: %s", i+1, match.Metric, match.Value)
}
var bodyMsg map[string]interface{}
if dd.MsgType == "actionCard" {
2018-11-11 21:18:53 -06:00
// Embed the pic into the markdown directly because actionCard doesn't have a picUrl field
if dd.NeedsImage() && picURL != "" {
message = "![](" + picURL + ")\n\n" + message
2018-11-11 21:18:53 -06:00
}
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,
}
if dd.NeedsImage() {
link["picUrl"] = picURL
}
bodyMsg = map[string]interface{}{
"msgtype": "link",
"link": link,
}
}
return json.Marshal(bodyMsg)
}