Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/update alerting dingding.20231113 #140

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions receivers/dinding/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
5 changes: 4 additions & 1 deletion receivers/dinding/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
{
Expand All @@ -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",
},
},
}
Expand Down
39 changes: 36 additions & 3 deletions receivers/dinding/dingding.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"net/url"
"strings"

"github.com/prometheus/alertmanager/types"

Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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{}{
Expand All @@ -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{
Expand All @@ -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,
},
}
Comment on lines +119 to +141
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add unit-tests for two new parameter values

}
body, err := json.Marshal(bodyMsg)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions receivers/dinding/dingding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func TestNotify(t *testing.T) {
MessageType: defaultDingdingMsgType,
Title: templates.DefaultMessageTitleEmbed,
Message: templates.DefaultMessageEmbed,
ToUser: defaultDingdingToUser,
},
alerts: []*types.Alert{
{
Expand All @@ -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{
{
Expand Down Expand Up @@ -93,6 +95,7 @@ func TestNotify(t *testing.T) {
MessageType: defaultDingdingMsgType,
Title: "Alerts firing: {{ len .Alerts.Firing }}",
Message: "customMessage",
ToUser: defaultDingdingToUser,
},
alerts: []*types.Alert{
{
Expand All @@ -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{
{
Expand Down Expand Up @@ -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{
{
Expand Down
3 changes: 2 additions & 1 deletion receivers/dinding/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}`