1. 釘釘SDK
2. 代碼示例
package main
import (
"io/ioutil"
"github.com/CodyGuo/dingtalk"
"github.com/CodyGuo/dingtalk/pkg/robot"
"github.com/CodyGuo/glog"
)
func main() {
glog.SetFlags(glog.LglogFlags)
webHook := "https://oapi.dingtalk.com/robot/send?access_token=xxx"
secret := "xxx"
dt := dingtalk.New(webHook, dingtalk.WithSecret(secret))
textContent := "我就是我, 是不一樣的煙火@176xxxx8207"
atMobiles := robot.SendWithAtMobiles([]string{"176xxxxxx07", "178xxxxxx28"})
if err := dt.RobotSendText(textContent, atMobiles); err != nil {
glog.Fatal(err)
}
printResult(dt)
linkTitle := "時代的火車向前開"
linkText := `這個即將發佈的新版本,創始人xx稱它爲“紅樹林”。` +
`而在此之前,每當面臨重大升級,產品經理們都會取一個應景的代號,` +
`這一次,爲什麼是“紅樹林”?`
linkMessageURL := "https://www.dingtalk.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI"
linkPicURL := "https://cdn.pixabay.com/photo/2020/05/05/08/05/butterfly-5131967_960_720.jpg"
if err := dt.RobotSendLink(linkTitle, linkText, linkMessageURL, linkPicURL); err != nil {
glog.Fatal(err)
}
printResult(dt)
markdownTitle := "markdown"
markdownText := "#### 杭州天氣 @176XXXXXXXX\n" +
"> 9度,西北風1級,空氣良89,相對溫度73%\n" +
"> ![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png)\n" +
"> ###### 10點20分發布 [天氣](https://www.dingtalk.com)\n"
if err := dt.RobotSendMarkdown(markdownTitle, markdownText); err != nil {
glog.Fatal(err)
}
printResult(dt)
actionCardTitle := "喬布斯 20 年前想打造一間蘋果咖啡廳,而它正是 Apple Store 的前身"
actionCardText := "![screenshot](@lADOpwk3K80C0M0FoA)\n" +
"### 喬布斯 20 年前想打造的蘋果咖啡廳\n" +
"Apple Store 的設計正從原來滿滿的科技感走向生活化," +
"而其生活化的走向其實可以追溯到 20 年前蘋果一個建立咖啡館的計劃"
actionCardSingleTitle := "閱讀全文"
actionCardSingleURL := "https://www.dingtalk.com/"
actionCardBtnOrientation := "0"
if err := dt.RobotSendEntiretyActionCard(actionCardTitle,
actionCardText,
actionCardSingleTitle,
actionCardSingleURL,
actionCardBtnOrientation); err != nil {
glog.Fatal(err)
}
printResult(dt)
btns := map[string]string{
"內容不錯": actionCardSingleURL,
"不感興趣": actionCardSingleURL,
}
if err := dt.RobotSendIndependentActionCard(actionCardTitle,
actionCardText,
actionCardBtnOrientation,
btns); err != nil {
glog.Fatal(err)
}
printResult(dt)
link1 := robot.FeedCardLink{
Title: linkTitle,
MessageURL: linkMessageURL,
PicURL: linkPicURL,
}
link2 := robot.FeedCardLink{
Title: linkTitle + "2",
MessageURL: linkMessageURL,
PicURL: linkPicURL,
}
links := []robot.FeedCardLink{link1, link2}
if err := dt.RobotSendFeedCard(links); err != nil {
glog.Fatal(err)
}
printResult(dt)
}
func printResult(dt *dingtalk.DingTalk) {
response, err := dt.GetResponse()
if err != nil {
glog.Fatal(err)
}
reqBody, err := response.Request.GetBody()
if err != nil {
glog.Fatal(err)
}
reqData, err := ioutil.ReadAll(reqBody)
if err != nil {
glog.Fatal(err)
}
glog.Infof("發送消息成功, message: %s", reqData)
}
3. 消息類型
3.1 text類型
3.2 link類型
3.3 markdown類型
3.4 整體跳轉ActionCard類型
3.5 獨立跳轉ActionCard類型
3.6 FeedCard類型