diff --git a/receivers/dinding/config.go b/receivers/dinding/config.go index b2229c82..7d73bd57 100644 --- a/receivers/dinding/config.go +++ b/receivers/dinding/config.go @@ -13,9 +13,11 @@ type Config struct { MessageType string `json:"msgType,omitempty" yaml:"msgType,omitempty"` Title string `json:"title,omitempty" yaml:"title,omitempty"` Message string `json:"message,omitempty" yaml:"message,omitempty"` + ToUser string `json:"touser,omitempty" yaml:"touser,omitempty"` } const defaultDingdingMsgType = "link" +const defaultDingdingToUser = "all" func NewConfig(jsonData json.RawMessage) (Config, error) { var settings Config @@ -35,5 +37,8 @@ func NewConfig(jsonData json.RawMessage) (Config, error) { if settings.Message == "" { settings.Message = templates.DefaultMessageEmbed } + if settings.ToUser == "" { + settings.ToUser = defaultDingdingToUser + } return settings, nil } diff --git a/receivers/dinding/config_test.go b/receivers/dinding/config_test.go index b02541c7..65192687 100644 --- a/receivers/dinding/config_test.go +++ b/receivers/dinding/config_test.go @@ -40,16 +40,18 @@ func TestNewConfig(t *testing.T) { MessageType: defaultDingdingMsgType, Title: templates.DefaultMessageTitleEmbed, Message: templates.DefaultMessageEmbed, + ToUser: defaultDingdingToUser, }, }, { name: "All empty fields = minimal valid configuration", - settings: `{"url": "http://localhost", "message": "", "title": "", "msgType": ""}`, + settings: `{"url": "http://localhost", "message": "", "title": "", "msgType": "", "toUser":""}`, expectedConfig: Config{ URL: "http://localhost", MessageType: defaultDingdingMsgType, Title: templates.DefaultMessageTitleEmbed, Message: templates.DefaultMessageEmbed, + ToUser: defaultDingdingToUser, }, }, { @@ -60,6 +62,7 @@ func TestNewConfig(t *testing.T) { MessageType: "actionCard", Title: "Alerts firing: {{ len .Alerts.Firing }}", Message: "{{ len .Alerts.Firing }} alerts are firing, {{ len .Alerts.Resolved }} are resolved", + ToUser: "all", }, }, } diff --git a/receivers/dinding/dingding.go b/receivers/dinding/dingding.go index f27b415e..808cafd4 100644 --- a/receivers/dinding/dingding.go +++ b/receivers/dinding/dingding.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "net/url" + "strings" "github.com/prometheus/alertmanager/types" @@ -13,6 +14,7 @@ import ( "github.com/grafana/alerting/templates" ) +// refer: https://open.dingtalk.com/document/orgapp/custom-robots-send-group-messages // Notifier is responsible for sending alert notifications to ding ding. type Notifier struct { *receivers.Base @@ -45,7 +47,9 @@ func (dd *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error title := tmpl(dd.settings.Title) msgType := tmpl(dd.settings.MessageType) - b, err := buildBody(dingDingURL, msgType, title, message) + toUser := dd.settings.ToUser + + b, err := buildBody(dingDingURL, msgType, title, message, toUser) if err != nil { return false, err } @@ -85,7 +89,13 @@ func buildDingDingURL(dd *Notifier) string { return "dingtalk://dingtalkclient/page/link?" + q.Encode() } -func buildBody(url string, msgType string, title string, msg string) (string, error) { +func buildBody(url string, msgType string, title string, msg string, toUser string) (string, error) { + var atUsers []string + isAtAll := toUser == "all" + + if toUser != "all" { + atUsers = strings.Split(toUser, ",") + } var bodyMsg map[string]interface{} if msgType == "actionCard" { bodyMsg = map[string]interface{}{ @@ -97,7 +107,7 @@ func buildBody(url string, msgType string, title string, msg string) (string, er "singleURL": url, }, } - } else { + } else if msgType == "link" { bodyMsg = map[string]interface{}{ "msgtype": "link", "link": map[string]string{ @@ -106,6 +116,29 @@ func buildBody(url string, msgType string, title string, msg string) (string, er "messageUrl": url, }, } + } else if msgType == "text" { + bodyMsg = map[string]interface{}{ + "msgtype": "text", + "text": map[string]string{ + "content": msg, + }, + "at": map[string]interface{}{ + "atMobiles": atUsers, + "isAtAll": isAtAll, + }, + } + } else if msgType == "markdown" { + bodyMsg = map[string]interface{}{ + "msgtype": "markdown", + "markdown": map[string]string{ + "text": msg, + "title": title, + }, + "at": map[string]interface{}{ + "atMobiles": atUsers, + "isAtAll": isAtAll, + }, + } } body, err := json.Marshal(bodyMsg) if err != nil { diff --git a/receivers/dinding/dingding_test.go b/receivers/dinding/dingding_test.go index 03aad121..b62e8671 100644 --- a/receivers/dinding/dingding_test.go +++ b/receivers/dinding/dingding_test.go @@ -37,6 +37,7 @@ func TestNotify(t *testing.T) { MessageType: defaultDingdingMsgType, Title: templates.DefaultMessageTitleEmbed, Message: templates.DefaultMessageEmbed, + ToUser: defaultDingdingToUser, }, alerts: []*types.Alert{ { @@ -62,6 +63,7 @@ func TestNotify(t *testing.T) { MessageType: "actionCard", Title: templates.DefaultMessageTitleEmbed, Message: "{{ len .Alerts.Firing }} alerts are firing, {{ len .Alerts.Resolved }} are resolved", + ToUser: defaultDingdingToUser, }, alerts: []*types.Alert{ { @@ -93,6 +95,7 @@ func TestNotify(t *testing.T) { MessageType: defaultDingdingMsgType, Title: "Alerts firing: {{ len .Alerts.Firing }}", Message: "customMessage", + ToUser: defaultDingdingToUser, }, alerts: []*types.Alert{ { @@ -118,6 +121,7 @@ func TestNotify(t *testing.T) { MessageType: "actionCard", Title: templates.DefaultMessageTitleEmbed, Message: "I'm a custom template {{ .NotAField }} bad template", + ToUser: defaultDingdingToUser, }, alerts: []*types.Alert{ { @@ -148,6 +152,7 @@ func TestNotify(t *testing.T) { MessageType: "actionCard", Title: templates.DefaultMessageTitleEmbed, Message: "I'm a custom template {{ {.NotAField }} bad template", + ToUser: defaultDingdingToUser, }, alerts: []*types.Alert{ { diff --git a/receivers/dinding/testing.go b/receivers/dinding/testing.go index 9238c39f..586c9cb8 100644 --- a/receivers/dinding/testing.go +++ b/receivers/dinding/testing.go @@ -5,5 +5,6 @@ const FullValidConfigForTesting = `{ "url": "http://localhost", "message": "{{ len .Alerts.Firing }} alerts are firing, {{ len .Alerts.Resolved }} are resolved", "title": "Alerts firing: {{ len .Alerts.Firing }}", - "msgType": "actionCard" + "msgType": "actionCard", + "toUser": "all" }`