用钉钉机器人,可以做一些通知,比如说程序的预警,风控啊。
官方描述
看官网描述,很强大,支持文本,链接,Markdown。
添加完机器人可以用curl工具随便发点什么试试:
curl ‘https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxx’
-H ‘Content-Type: application/json’
-d ‘{“msgtype”: “text”,“text”: {“content”:“我就是我, 是不一样的烟火”}}’
如果报错如下:
{"errcode":310000,"errmsg":"sign not match, more: [https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq]"}
那是因为勾选了 加密
加签之后需要将 url拼接上×tamp= XX&sign=XX
通过下面方法可将加密后的参数拼接到url即可;
private String sign(Long timestamp,String secret){
try {
String stringToSign = timestamp + "\n" + secret;
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
byte[] signData = mac.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8));
return URLEncoder.encode(new String(Base64.encodeBase64(signData)),"UTF-8");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
其他主题注意,必填参数缺失回报这个错。
{"errcode":400503,"errmsg":"miss param :XXXX"}
OapiRobotSendRequest.Actioncard actionCard = new OapiRobotSendRequest.Actioncard();
actionCard.setTitle("乔布斯 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身");
actionCard.setText("![screenshot](https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png) \n" +
" ### 乔布斯 20 年前想打造的苹果咖啡厅 \n" +
" Apple Store 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划");
actionCard.setBtnOrientation("0");
actionCard.setSingleTitle("阅读全文");
actionCard.setSingleURL("singleURL");
actionCard.setHideAvatar("12");
OapiRobotSendRequest.Btns btns = new OapiRobotSendRequest.Btns();
btns.setTitle("21212");
ArrayList<OapiRobotSendRequest.Btns> btns1 = new ArrayList<>();
actionCard.setBtns(btns1);
//将主题封装带要执行的request
request.setActionCard(actionCard);
//执行
OapiRobotSendResponse response = client.execute(request);