Golang接入釘釘通知的示例代碼
設(shè)置與接入
1、創(chuàng)建一個釘釘群
2、打開群設(shè)置,智能群助手,添加機器人,自定義Webhook接入
3、設(shè)置機器人名字,開啟消息推送,復(fù)制Webhook地址
4、安全設(shè)置,選擇自定義關(guān)鍵詞,最多可添加十個
發(fā)送消息
采用post方式向上面復(fù)制的地址發(fā)送請求即可
消息協(xié)議: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"
// 發(fā)送簡單文本消息
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
}調(diào)用
dingtalk.SendDingTalkMessage(fmt.Sprintf("service [%s] reboot success", v.Name), dingtalk.KeywordMonitor)
到此這篇關(guān)于Golang接入釘釘通知的示例代碼的文章就介紹到這了,更多相關(guān)Golang接入釘釘通知內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Go?json自定義Unmarshal避免判斷nil示例詳解
這篇文章主要為大家介紹了Go?json自定義Unmarshal避免判斷nil示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06
GoLang中panic與recover函數(shù)以及defer語句超詳細講解
這篇文章主要介紹了GoLang的panic、recover函數(shù),以及defer語句,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01
Go語言實現(xiàn)類似c++中的多態(tài)功能實例
Go本身不具有多態(tài)的特性,不能夠像Java、C++那樣編寫多態(tài)類、多態(tài)方法。但是,使用Go可以編寫具有多態(tài)功能的類綁定的方法。下面來一起看看吧2016-09-09

