背景
最近在用Go實(shí)現(xiàn)業(yè)務(wù)日志實(shí)時(shí)監(jiān)控,當(dāng)捕捉到error級(jí)別的日志時(shí),想要推送釘釘消息到群里進(jìn)行通知,下面代碼實(shí)現(xiàn)了簡(jiǎn)單消息及markdown消息推送到釘釘機(jī)器人webhook的操作,供大家參考。
代碼實(shí)現(xiàn)
package main
import (
"fmt"
// 先下載這個(gè)包,go get github.com/mikemintang/go-curl
"github.com/mikemintang/go-curl"
)
func main() {
// 釘釘機(jī)器人webhook
url := "https://oapi.dingtalk.com/robot/send?access_token=your_access_token"
// 添加頭信息
headers := map[string]string{
"Content-Type": "application/json",
}
/*
發(fā)送簡(jiǎn)單文本消息
content := make(map[string]string)
content["content"] = "hello"
postData := map[string]interface{}{
"msgtype": "text",
"text": content ,
}
*/
// 發(fā)送markdown消息
content := make(map[string]string)
content["content"] = "hello"
postData := map[string]interface{}{
"msgtype": "markdown" ,
"markdown": map[string]string{
"title": "ERROR",
"text": "## Please check now\n " +
"> 1. first\n" +
"> 2. second\n",
},
"at": map[string]interface{}{
"atMobiles":[]string{"18888888888","18866666666"},
"isAtAll": false,
},
}
// 鏈?zhǔn)讲僮? req := curl.NewRequest()
resp, err := req.
SetUrl(url).
SetHeaders(headers).
SetPostData(postData).
Post()
// 返回處理
if err != nil {
fmt.Println(err)
} else {
if resp.IsOk() {
fmt.Println(resp.Body)
} else {
fmt.Println(resp.Raw)
}
}
}