java
/**
* @Remark 企业机器人
* {"errcode":400102,"errmsg":"错误描述:机器人已经停用或者未启用;解决方案:请让企业管理员前往开放平台后台启用对应机器人 :https://open-dev.dingtalk.com/#/"}
* @Author wang qiang
* @Date 2025/12/13 14:48
**/
@GetMapping("business")
public void sendMessage() throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send?access_token=");
OapiRobotSendRequest request = new OapiRobotSendRequest();
request.setMsgtype("text");
OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
text.setContent("测试文本消息");
request.setText(text);
OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
at.setAtUserIds(Arrays.asList("15r_6bgckasnts"));
at.setIsAtAll(false);
request.setAt(at);
OapiRobotSendResponse response = client.execute(request);
System.out.println(response.getBody());
}
/**
* @Remark 自定义机器人
* @Author wang qiang
* @Date 2025/12/13 14:48
**/
@GetMapping("webHook")
public OapiRobotSendResponse sendMessageWebhook(@RequestParam("msg") String msg) throws Exception {
return sendMsg(msg);
}
/**
* @Remark 自定义机器人--加签生成sign
* @Author wang qiang
* @Date 2025/12/13 15:40
**/
private String getSign(Long timestamp) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
// 钉钉机器人加签SECRET
String SECRET="";
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"));
return URLEncoder.encode(new String(Base64.encodeBase64(signData,false)),"UTF-8");
}
/**
* @Remark 自定义机器人--钉钉发送消息
* @param msg 新消息内容
* @Author wang qiang
* @Date 2025/12/13 16:06
**/
public OapiRobotSendResponse sendMsg(String msg) throws Exception {
//钉钉机器人中webhook中的token
String TOKEN="";
//钉钉群主的id-可以为空
String USER_ID="";
Long timestamp = System.currentTimeMillis();
//sign字段和timestamp字段必须拼接到请求URL上,否则会出现 310000 的错误信息
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send?sign="+getSign(timestamp)+"×tamp="+timestamp);
OapiRobotSendRequest req = new OapiRobotSendRequest();
//定义文本内容
OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
text.setContent(msg);
//定义 @ 对象
OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
at.setAtUserIds(Arrays.asList(USER_ID));
//设置消息类型
req.setMsgtype("text");
req.setText(text);
req.setAt(at);
return client.execute(req, TOKEN);
}
java
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibaba-dingtalk-service-sdk</artifactId>
<version>2.0.0</version>
</dependency>
自定义机器人

企业机器人
