前言
最近在优化持续集成,之前使用 Python 写过钉钉机器人,不过随着项目往 golang 发展,于是想去掉 Python 的依赖。另外,想着如果后台服务有些重要的事项,可以通知到钉钉,也想着做个 go module 供项目使用。本来想直接 github 上找一个,结果搜索了老半天没看到一个符合的,于是,手撸了一个。
DingTalk
DockerJenkinsfile命令行module加签链式语法文本、链接、Markdown、ActionCard、FeedCard
特性
支持Docker
支持module
支持加签
signText 消息
textLink 消息
linkMarkdown 消息
markdownActionCard 消息
ActionCard1ActionCard2FeedCard 消息
ActionCard1
安装
Docker 安装
docker pull catchzeng/dingtalk
二进制安装
到 releases 下载相应平台的二进制可执行文件,然后加入到 PATH 环境变量即可。
go get 安装
go get github.com/CatchZeng/dingtalk
使用方法
Docker
docker run catchzeng/dingtalk dingtalk text -t 1c53e149ba5de6597cxxxxxx0e901fdxxxxxx80b8ac141e4a75afdc44c85ca4f -s SECb90923e19e58b466481e9e7b7a5bxxxxxx4531axxxxxxad3967fb29f0eae5c68 -c "docker test"
Jenkinsfile
pipeline {
agent {
docker {
image 'catchzeng/dingtalk:latest'
}
}
environment {
DING_TOKEN = '1c53e149ba5de6597cxxxxxx0e901fdxxxxxx80b8ac141e4a75afdc44c85ca4f'
DING_SECRET = 'SECb90923e19e58b466481e9e7b7a5bxxxxxx4531axxxxxxad3967fb29f0eae5c68'
}
stages {
stage('notify') {
steps {
sh 'dingtalk link -t ${DING_TOKEN} -s ${DING_SECRET} -i "标题" -e "信息" -u "https://catchzeng.com/" -p "https://catchzeng.com/img/avatar-hux.jpg" -a'
}
}
}
}
作为 module
package main
import (
"log"
"github.com/CatchZeng/dingtalk/client"
"github.com/CatchZeng/dingtalk/message"
)
func main() {
dingTalk := client.DingTalk{
AccessToken: "1c53e149ba5de6597cxxxxxx0e901fdxxxxxx80b8ac141e4a75afdc44c85ca4f",
Secret: "SECb90923e19e58b466481e9e7b7a5bxxxxxx4531axxxxxxad3967fb29f0eae5c68",
}
msg := message.NewTextMessage().SetContent("测试文本&at 某个人").SetAt([]string{"177010xxx60"}, false)
dingTalk.Send(msg)
}
命令行工具
Demo
dingtalk text -t 1c53e149ba5de6597cxxxxxx0e901fdxxxxxx80b8ac141e4a75afdc44c85ca4f -s SECb90923e19e58b466481e9e7b7a5bxxxxxx4531axxxxxxad3967fb29f0eae5c68 -c "测试命令行 & at 某个人" -m "177010xxx60","177010xxx61"
Help
dingtalk
```shell
$ dingtalk -h
dingtalk is a command line tool for DingTalkUsage: