钉钉群添加自定义机器人
群内添加自定义机器人,从自定义配置中获取到secret以及accessToken
代码调用
Long timestamp = System.currentTimeMillis();
String secret = "secret";
String accessToken = "accessToken";
String stringToSign = timestamp + "\n" + secret;
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)),"UTF-8");
StringBuffer stringBuffer=new StringBuffer();
stringBuffer.append("https://oapi.dingtalk.com/robot/send?access_token=").append(accessToken);
//stringBuffer.append("×tamp=").append(timestamp);
// stringBuffer.append("&sign=").append(sign);
System.out.println(stringBuffer.toString());
//sign字段和timestamp字段必须拼接到请求URL上,否则会出现 310000 的错误信息
DingTalkClient client = new DefaultDingTalkClient(stringBuffer.toString());
//参数 类型 必选 说明
//msgtype string true 此消息类型为固定actionCard
//title string true 首屏会话透出的展示内容
//text string true markdown格式的消息
//btns array true 按钮的信息:title-按钮方案,actionURL-点击按钮触发的URL
//btnOrientation string false 0-按钮竖直排列,1-按钮横向排列
//hideAvatar string false 0-正常发消息者头像,1-隐藏发消息者头像
//支持多种request方式
OapiRobotSendRequest.Actioncard actionCard = new OapiRobotSendRequest.Actioncard();
actionCard.setTitle("杭州天气");
actionCard.setText("#### 杭州天气 @156xxxx8827\n" +
"> 9度,西北风1级,空气良89,相对温度73%\n\n" +
"> ![screenshot](https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png)\n" +
"> ###### 10点20分发布 [天气](http://www.thinkpage.cn/) \n");
// 此处默认为0
actionCard.setBtnOrientation("0");
// 此处默认为0
actionCard.setHideAvatar("0");
OapiRobotSendRequest request = new OapiRobotSendRequest();
request.setMsgtype("actionCard");
request.setActionCard(actionCard);
OapiRobotSendResponse rsp = client.execute(request);
System.out.println(rsp.getBody());
标签:string,stringBuffer,actionCard,机器人,发送,消息,timestamp,new,append
From: https://www.cnblogs.com/BitX/p/18304735