Skip to content

Commit

Permalink
Make dingtalk support markdown
Browse files Browse the repository at this point in the history
Signed-off-by: guozijn <[email protected]>
  • Loading branch information
guozijn committed Aug 29, 2019
1 parent d98b3bd commit 098a8b4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
3 changes: 2 additions & 1 deletion cmd/nuntius/config.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package main

import (
"io/ioutil"

"github.com/guozijn/nuntius/provider/dingtalk"
"github.com/guozijn/nuntius/provider/telegram"
"github.com/prometheus/alertmanager/template"
"gopkg.in/yaml.v2"
"io/ioutil"
)

type ReceiverConf struct {
Expand Down
7 changes: 7 additions & 0 deletions provider/dingtalk/dingtalk.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Config struct {
Token string `yaml:"token"`
URL string `yaml:"url"`
MsgType string `yaml:"msgtype"`
Title string `yaml:"title"`
AtMobiles []string `yaml:"atmobiles"`
IsAtAll bool `yaml:"isatall"`
}
Expand All @@ -29,6 +30,10 @@ type DingTalkPayload struct {
Text struct {
Content string `json:"content"`
} `json:"text"`
Markdown struct {
Title string `json:"title"`
Text string `json:"text"`
} `json:"markdown"`
At struct {
AtMobiles []string `json:"atMobiles"`
IsAtAll bool `json:"isAtAll"`
Expand All @@ -50,6 +55,8 @@ func (c *DingTalk) Send(message nuntius.Message) error {
payload := &DingTalkPayload{}
payload.Msgtype = c.MsgType
payload.Text.Content = message.Text
payload.Markdown.Title = c.Title
payload.Markdown.Text = message.Text
payload.At.AtMobiles = c.AtMobiles
payload.At.IsAtAll = c.IsAtAll

Expand Down
13 changes: 13 additions & 0 deletions samples/notifications.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,16 @@

{{ define "common_text" }}{{ template "common_title" .}}
{{ template "common_message" . }}{{ end }}

{{ define "dingtalk_markdown_message" }}
{{ if gt (len .Alerts.Firing) 0 }}
# Alerts Firing:
{{ range .Alerts.Firing }}> - {{ .Labels.instance }}: {{ .Annotations.description }}
{{ end }}{{ end }}
{{ if gt (len .Alerts.Resolved) 0 }}
# Alerts Resolved:
{{ range .Alerts.Resolved }}> - {{ .Labels.instance }}: {{ .Annotations.description }}
{{ end }}{{ end }}{{ end }}

{{ define "dingtalk_markdown_text" }}{{ template "dingtalk_markdown_title" .}}
{{ template "dingtalk_markdown_message" . }}{{ end }}
6 changes: 4 additions & 2 deletions samples/nuntius.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ providers:
dingtalk:
# Robot access token
token: "62150a3275e0e8398a07a311695bdaeb5500c8b9810fdfafb6d813a590136c4f"
msgtype: "text"
# Valid options: ['markdown', 'text']
msgtype: "markdown"
# The phone number of the person you want @
title: "告警信息"
atmobiles:
- "13666666666"
isatall:
Expand All @@ -25,4 +27,4 @@ receivers:

- name: 'dingtalk'
provider: dingtalk
text: '{{ template "common_message" . }}'
text: '{{ template "dingtalk_markdown_message" . }}'

0 comments on commit 098a8b4

Please sign in to comment.