From 098a8b4e160cfc9f61bb716b5159ccf585efb5b9 Mon Sep 17 00:00:00 2001 From: guozijn Date: Thu, 29 Aug 2019 21:32:32 +0800 Subject: [PATCH] Make dingtalk support markdown Signed-off-by: guozijn --- cmd/nuntius/config.go | 3 ++- provider/dingtalk/dingtalk.go | 7 +++++++ samples/notifications.tmpl | 13 +++++++++++++ samples/nuntius.yml | 6 ++++-- 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/cmd/nuntius/config.go b/cmd/nuntius/config.go index 0bbcce1..afa55cf 100644 --- a/cmd/nuntius/config.go +++ b/cmd/nuntius/config.go @@ -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 { diff --git a/provider/dingtalk/dingtalk.go b/provider/dingtalk/dingtalk.go index f471dbd..521a709 100644 --- a/provider/dingtalk/dingtalk.go +++ b/provider/dingtalk/dingtalk.go @@ -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"` } @@ -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"` @@ -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 diff --git a/samples/notifications.tmpl b/samples/notifications.tmpl index 9631ba3..e59f9fc 100644 --- a/samples/notifications.tmpl +++ b/samples/notifications.tmpl @@ -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 }} diff --git a/samples/nuntius.yml b/samples/nuntius.yml index e82fdc9..a89f4cb 100644 --- a/samples/nuntius.yml +++ b/samples/nuntius.yml @@ -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: @@ -25,4 +27,4 @@ receivers: - name: 'dingtalk' provider: dingtalk - text: '{{ template "common_message" . }}' + text: '{{ template "dingtalk_markdown_message" . }}'