first commit
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
package com.xiang.common.utils.dingTalk;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.dingtalk.api.DefaultDingTalkClient;
|
||||
import com.dingtalk.api.DingTalkClient;
|
||||
import com.dingtalk.api.request.OapiRobotSendRequest;
|
||||
import com.dingtalk.api.response.OapiRobotSendResponse;
|
||||
import com.xiang.common.config.RobotConfig;
|
||||
import com.xiang.common.exception.BusinessException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 钉钉消息工具类
|
||||
* @Author: xiang
|
||||
* @Date: 2026-01-04 15:16
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class DingTalkSender {
|
||||
|
||||
private static final String MSG_TYPE_TEXT = "text";
|
||||
|
||||
/**
|
||||
* 发送机器人消息到指定的群
|
||||
* @param robotConfig 机器人配置文件
|
||||
* @param msg 消息内容
|
||||
* @return
|
||||
*/
|
||||
public String sendRobotMessage(RobotConfig robotConfig, String msg) {
|
||||
try {
|
||||
return doSendMsg(robotConfig, msg);
|
||||
} catch (Exception e) {
|
||||
log.info("钉钉机器人消息发送失败,业务线===>{}", robotConfig.getName());
|
||||
throw new BusinessException("钉钉消息发送失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 消息发送
|
||||
* @param robotConfig 机器人配置信息
|
||||
* @param msg 发送的消息
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
private String doSendMsg(RobotConfig robotConfig, String msg) throws Exception {
|
||||
String robotSecret = robotConfig.getSecret().trim();
|
||||
String robotToken = robotConfig.getToken().trim();
|
||||
List<String> userIds = robotConfig.getUsers();
|
||||
Long timestamp = System.currentTimeMillis();
|
||||
String stringToSign = timestamp + "\n" + robotSecret;
|
||||
Mac mac = Mac.getInstance("HmacSHA256");
|
||||
mac.init(new SecretKeySpec(robotSecret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
|
||||
byte[] signData = mac.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8));
|
||||
String sign = URLEncoder.encode(Base64.getEncoder().encodeToString(signData), StandardCharsets.UTF_8);
|
||||
|
||||
//sign字段和timestamp字段必须拼接到请求URL上,否则会出现 310000 的错误信息
|
||||
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send?sign=" + sign + "×tamp=" + timestamp);
|
||||
OapiRobotSendRequest req = new OapiRobotSendRequest();
|
||||
|
||||
//定义文本内容
|
||||
OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
|
||||
text.setContent(msg);
|
||||
//定义 @ 对象
|
||||
OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
|
||||
at.setAtUserIds(userIds);
|
||||
//设置消息类型
|
||||
req.setMsgtype(MSG_TYPE_TEXT);
|
||||
req.setText(text);
|
||||
req.setAt(at);
|
||||
OapiRobotSendResponse rsp = client.execute(req, robotToken);
|
||||
return rsp.getBody();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user