Skip to content

Commit

Permalink
make sure dingtalk notify always produce valid json (#469)
Browse files Browse the repository at this point in the history
  • Loading branch information
suchen-sci authored Jan 10, 2024
1 parent 8c0a606 commit dcd5dd5
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions notify/dingtalk/dingtalk.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ type NotifyConfig struct {
SignSecret string `yaml:"secret,omitempty" json:"secret,omitempty" jsonschema:"format=string,title=Secret,description=The Dingtalk Robot Secret"`
}

// DingTalk is the dingtalk notification struct
type DingTalk struct {
MsgType string `json:"msgtype"`
Markdown Markdown `json:"markdown"`
}

// Markdown is the markdown struct of dingtalk notification
type Markdown struct {
Title string `json:"title"`
Text string `json:"text"`
}

// Config configures the dingtalk notification
func (c *NotifyConfig) Config(gConf global.NotifySettings) error {
c.NotifyKind = "dingtalk"
Expand All @@ -56,18 +68,21 @@ func (c *NotifyConfig) Config(gConf global.NotifySettings) error {
// SendDingtalkNotification will post to an 'Robot Webhook' url in Dingtalk Apps. It accepts
// some text and the Dingtalk robot will send it in group.
func (c *NotifyConfig) SendDingtalkNotification(title, msg string) error {

title = "**" + title + "**"
// It will be better to escape the msg.
msgContent := fmt.Sprintf(`
{
"msgtype": "markdown",
"markdown": {
"title": "%s",
"text": "%s"
}
dingtalk := DingTalk{
MsgType: "markdown",
Markdown: Markdown{
Title: title,
Text: msg,
},
}
msgContent, err := json.Marshal(dingtalk)
if err != nil {
log.Errorf("[%s / %s ] - %v, err - %s", c.Kind(), c.Name(), dingtalk, err)
return fmt.Errorf("[%s / %s] - Error from json marshal [%s] - [%s]",
c.Kind(), c.Name(), dingtalk, err)
}
`, report.JSONEscape(title), report.JSONEscape(msg))

req, err := http.NewRequest(http.MethodPost, c.addSign(c.WebhookURL, c.SignSecret), bytes.NewBuffer([]byte(msgContent)))
if err != nil {
return err
Expand Down

0 comments on commit dcd5dd5

Please sign in to comment.