dingtalk
钉钉机器人消息封装,三行代码发送钉钉消息——Golang实现,简化日常通过钉钉群机器人发送提示、报警消息操作。
目前自定义机器人支持
- 文本(text)
- 链接(link)
- markdown
- ActionCard
- 整体跳转
- 独立跳转
- FeedCard
使用
创建钉钉群机器人
- 选择添加
自定义
机器人。 - 安全设置
共有关键词、加签、IP白名单三种设置,需要根据情况进行选择。
- 选择
自定义关键词
,这里设置的关键词在初始化机器人的时候会用到。
获取
-
go get github.com/blinkbean/dingtalk
初始化
-
// key 创建钉钉机器人需要设置的关键词,默认为`.` func InitDingTalk(tokens []string, key string) *dingTalk
-
import "github.com/blinkbean/dingtalk" func main() { // 单个机器人有单位时间内消息条数的限制,如果有需要可以初始化多个token,发消息时随机发给其中一个机器人。 var dingToken = []string{"7bd675b66646ba890046c2198257576470099e1bda0770bad7dd6684fb1e0415"} cli := dingtalk.InitDingTalk(dingToken, ".") cli.SendTextMessage("content") }
text类型
- 方法及可选参数
// 方法定义 SendTextMessage(content string, opt ...atOption) error // 可选参数 // @所有人 WithAtAll() // @指定群成员 WithAtMobiles(mobiles []string)
- 使用
// at所有人 cli.SendTextMessage("content", WithAtAll()) // at指定群成员 mobiles := []string{"131********"} cli.SendTextMessage("content", WithAtMobiles(mobiles))
link类型
- 方法
// 方法定义 SendLinkMessage(title, text, picUrl, msgUrl string) error
- 使用
cli.SendLinkMessage(title, text, picUrl, msgUrl)
markdown类型
- 方法及可选参数
// 方法定义 // text:markdown格式字符串 SendMarkDownMessage(title, text string, opts ...atOption) error // 可选参数 目前钉钉markdown格式消息不支持@(可能是钉钉的bug),所以以下可选参数暂时不生效。 // @所有人 WithAtAll() // @指定群成员 WithAtMobiles(mobiles []string)
- 使用
cli.SendMarkDownMessage(title, text)
整体跳转ActionCard类型
- 方法及可选参数
// 方法定义 SendActionCardMessage(title, text string, opts ...actionCardOption) error // 可选参数 // 标题 WithCardSingleTitle(title string) // 跳转地址 WithCardSingleURL(url string)
- 使用
cli.SendActionSingleMessage(title, text, WithCardSingleTitle(sTitle), WithCardSingleURL(url))
- [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XB6PGmk7-1593936340194)(https://i.loli.net/2020/07/05/kKELHAlomndiO9I.jpg)]
独立跳转ActionCard类型
- 方法及可选参数
// 方法定义 SendActionCardMessage(title, text string, opts ...actionCardOption) error // 可选参数 // 按钮排列方向,默认水平 WithCardBtnVertical() // 跳转按钮 WithCardBtns(btns []ActionCardMultiBtnModel) // ActionCardMultiBtnModel type ActionCardMultiBtnModel struct { Title string `json:"title,omitempty"` ActionURL string `json:"actionURL,omitempty"` }
- 使用
btns := []ActionCardMultiBtnModel{{ Title: "test1", ActionURL: testUrl, },{ Title: "test2", ActionURL: testUrl, }, } cli.SendActionSingleMessage(title, text, WithCardBtns(btns))
FeedCard类型
- 方法
// 方法定义 SendFeedCardMessage(feedCard []FeedCardLinkModel) error // FeedCardLinkModel type FeedCardLinkModel struct { Title string `json:"title,omitempty"` MessageURL string `json:"messageURL,omitempty"` PicURL string `json:"picURL,omitempty"` }
- 使用
links := []FeedCardLinkModel{ { Title: "FeedCard1.", MessageURL: testUrl, PicURL: testImg, }, { Title: "FeedCard2", MessageURL: testUrl, PicURL: testImg, }, { Title: "FeedCard3", MessageURL: testUrl, PicURL: testImg, }, } cli.SendFeedCardMessage(links)