Golang接入钉钉通知
raoxiaoya 人气:0设置与接入
1、创建一个钉钉群
2、打开群设置,智能群助手,添加机器人,自定义Webhook接入
3、设置机器人名字,开启消息推送,复制Webhook地址
4、安全设置,选择自定义关键词,最多可添加十个
发送消息
采用post方式向上面复制的地址发送请求即可
消息协议:https://open.dingtalk.com/document/robots/message-types-and-data-format
/* -- @Time : 2022/6/8 11:24 -- @Author : raoxiaoya -- @Desc : */ package dingtalk import ( "encoding/json" "errors" "voteapi/pkg/curl" ) type Response struct { Errcode int Errmsg string } const KeywordMonitor = "Monitor" const MessageRobot = "https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxxxxxxxxx" // 发送简单文本消息 func SendDingTalkMessage(messageContent, messagePrefix string) (err error) { defer func() { if er := recover(); er != nil { err = errors.New("SendDingTalkMessage panic") } }() headers := map[string]string{ "Content-Type": "application/json", } text := map[string]string{ "content": messagePrefix + ": " + messageContent, } postData := map[string]interface{}{ "msgtype": "text", "text": text, } body, _ := json.Marshal(postData) resp, err := curl.HttpRequest(MessageRobot, "POST", headers, string(body)) if err != nil { return err } var re Response _ = json.Unmarshal([]byte(resp), &re) if re.Errcode > 0 { return errors.New(resp) } return nil }
调用
dingtalk.SendDingTalkMessage(fmt.Sprintf("service [%s] reboot success", v.Name), dingtalk.KeywordMonitor)
加载全部内容