[原創]Golang一行代碼給釘釘群推送消息
釘釘本來就是工具,只是boss把你變成了工具. — 麥·卡隆
今天朋友扔給我個某簽到腳本,讓我做推送功能.
我迅速從吃灰收藏夾里掏出Sever醬,結果...似乎它經歷了一些可怕的事情...
總之,每天推送最多5條.
(咱也不差這幾個money,買個會員支持一下唄)
為了照顧互聯網最大幫派白嫖黨,我決定再找一個新的解決方案.
需求:
- 主流平台推送
- 直接調用
- 兼容各種平台
甲方滿意
經過偉大的一番科學發現,終於讓我找到合適的解決方案.
最終選擇:釘釘群聊機器人
參考釘釘官方文檔:https://developers.dingtalk.com/document/robots/custom-robot-access
發現是純API調用,直接POST萬事大吉.
第一步 創建並獲取自定義機器人Webhook
(以下文字Copy官方文檔)
- 選擇需要添加機器人的群聊,然后依次單擊群設置 > 智能群助手。
-
在機器人管理頁面選擇自定義機器人,輸入機器人名字並選擇要發送消息的群,同時可以為機器人設置機器人頭像。
-
完成必要的安全設置,勾選我已閱讀並同意《自定義機器人服務及免責條款》,然后單擊完成。
(我還沒寫完密鑰方式發送的代碼,可以先用着別人的包) -
完成安全設置后,復制出機器人的Webhook地址,可用於向這個群發送消息,格式如下:
https://oapi.dingtalk.com/robot/send?access_token=XXXXXX
然后,我決定用Golang寫個史詩級懶人調用包
第二步 寫DingBot包
第一步,Goland
直接一鍵導入全部json
轉結構體:
點擊查看神必代碼
package dingbot
// Text 文本json
type dText struct {
At struct {
AtMobiles []string `json:"atMobiles"`
AtUserIds []string `json:"atUserIds"`
IsAtAll bool `json:"isAtAll"`
} `json:"at"`
Text struct {
Content string `json:"content"`
} `json:"text"`
Msgtype string `json:"msgtype"`
}
//Link Link型json
type dLink struct {
Msgtype string `json:"msgtype"`
Link struct {
Text string `json:"text"`
Title string `json:"title"`
PicUrl string `json:"picUrl"`
MessageUrl string `json:"messageUrl"`
} `json:"link"`
}
//MD Markdown型json
type dMD struct {
Msgtype string `json:"msgtype"`
Markdown struct {
Title string `json:"title"`
Text string `json:"text"`
} `json:"markdown"`
At struct {
AtMobiles []string `json:"atMobiles"`
AtUserIds []string `json:"atUserIds"`
IsAtAll bool `json:"isAtAll"`
} `json:"at"`
}
// AActionCard 整體跳轉ActionCard類型
type dAActionCard struct {
ActionCard struct {
Title string `json:"title"`
Text string `json:"text"`
BtnOrientation string `json:"btnOrientation"`
SingleTitle string `json:"singleTitle"`
SingleURL string `json:"singleURL"`
} `json:"actionCard"`
Msgtype string `json:"msgtype"`
}
// DActionCard 獨立跳轉ActionCard類型
type dDActionCard struct {
Msgtype string `json:"msgtype"`
ActionCard struct {
Title string `json:"title"`
Text string `json:"text"`
BtnOrientation string `json:"btnOrientation"`
Btns []struct {
Title string `json:"title"`
ActionURL string `json:"actionURL"`
} `json:"btns"`
} `json:"actionCard"`
}
// FeedCard FeedCard類型
type dFeedCard struct {
Msgtype string `json:"msgtype"`
FeedCard struct {
Links []struct {
Title string `json:"title"`
MessageURL string `json:"messageURL"`
PicURL string `json:"picURL"`
} `json:"links"`
} `json:"feedCard"`
}
// ErrorReport 返回的錯誤
type dErrorReport struct {
Errcode int `json:"errcode"`
Errmsg string `json:"errmsg"`
}
可以非常清楚的看到機器人一共有6種(准確說是5種)方式發送消息.
然后,用最蠢方式再Copy個Post代碼:
點擊查看更神必的代碼
package dingbot
// from https://github.com/CatchZeng/dingtalk
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
)
const httpTimoutSecond = time.Duration(30) * time.Second
func send(message []byte, pushURL string) (*dErrorReport, error) {
res := &dErrorReport{}
reqBytes := message
req, err := http.NewRequest(http.MethodPost, pushURL, bytes.NewReader(reqBytes))
if err != nil {
return res, err
}
req.Header.Add("Accept-Charset", "utf8")
req.Header.Add("Content-Type", "application/json")
client := new(http.Client)
client.Timeout = httpTimoutSecond
resp, err := client.Do(req)
if err != nil {
return res, err
}
defer resp.Body.Close()
resultByte, err := ioutil.ReadAll(resp.Body)
if err != nil {
return res, err
}
err = json.Unmarshal(resultByte, &res)
if err != nil {
return res, fmt.Errorf("unmarshal http response body from json error = %w", err)
}
if res.Errcode != 0 {
return res, fmt.Errorf("send message to dingtalk error = %s", res.Errmsg)
}
return res, nil
}
感謝前輩CatchZeng的Dingtalk包.
最后,閉眼寫個函數實現:
const URL = "https://oapi.dingtalk.com/robot/send?access_token="
// Text 推送文本
func Text(token string, content string, AtMobiles []string, AtUserIds []string, IsAtAll bool) error {
t := new(dText)
t.Text = struct {
Content string `json:"content"`
}(struct{ Content string }{Content: content})
t.At = struct {
AtMobiles []string `json:"atMobiles"`
AtUserIds []string `json:"atUserIds"`
IsAtAll bool `json:"isAtAll"`
}(struct {
AtMobiles []string
AtUserIds []string
IsAtAll bool
}{AtMobiles: AtMobiles, AtUserIds: AtUserIds, IsAtAll: IsAtAll})
t.Msgtype = "text"
if jsonByte, err := json.Marshal(t); err != nil {
return err
} else if _, err := send(jsonByte, URL+token); err != nil {
return err
}
return nil
大功告成!來試試一行代碼調用
//func Text(token string, content string, AtMobiles []string, AtUserIds []string, IsAtAll bool) error
dingbot.Text("123","hello",[]string{},[]string{},false)
效果還是很不錯的
因為我的釘釘里有一些不可描述的東西,這次就不截圖啦!
源碼地址:https://github.com/EldersJavas/EsDingTalkBot_Go
Go文檔:https://pkg.go.dev/github.com/EldersJavas/EsDingTalkBot_Go
作者:麥卡隆
轉載帶上鏈接哦!