|
@@ -1,15 +1,19 @@
|
|
|
package cn.snowheart.dingtalk.robot.starter.client;
|
|
package cn.snowheart.dingtalk.robot.starter.client;
|
|
|
|
|
|
|
|
-import cn.snowheart.dingtalk.robot.starter.entity.*;
|
|
|
|
|
|
|
+import cn.snowheart.dingtalk.robot.starter.entity.ActionCardButton;
|
|
|
|
|
+import cn.snowheart.dingtalk.robot.starter.entity.ActionCardMessage;
|
|
|
|
|
+import cn.snowheart.dingtalk.robot.starter.entity.BaseMessage;
|
|
|
|
|
+import cn.snowheart.dingtalk.robot.starter.entity.DingTalkResponse;
|
|
|
|
|
+import cn.snowheart.dingtalk.robot.starter.entity.FeedCardMessage;
|
|
|
|
|
+import cn.snowheart.dingtalk.robot.starter.entity.FeedCardMessageItem;
|
|
|
|
|
+import cn.snowheart.dingtalk.robot.starter.entity.LinkMessage;
|
|
|
|
|
+import cn.snowheart.dingtalk.robot.starter.entity.MarkdownMessage;
|
|
|
|
|
+import cn.snowheart.dingtalk.robot.starter.entity.TextMessage;
|
|
|
import cn.snowheart.dingtalk.robot.starter.exception.DingTalkException;
|
|
import cn.snowheart.dingtalk.robot.starter.exception.DingTalkException;
|
|
|
import cn.snowheart.dingtalk.robot.starter.type.HideAvatarType;
|
|
import cn.snowheart.dingtalk.robot.starter.type.HideAvatarType;
|
|
|
import cn.snowheart.dingtalk.robot.starter.type.ResponseCodeType;
|
|
import cn.snowheart.dingtalk.robot.starter.type.ResponseCodeType;
|
|
|
-import org.apache.tomcat.util.codec.binary.Base64;
|
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
-import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.http.HttpEntity;
|
|
import org.springframework.http.HttpEntity;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.http.MediaType;
|
|
import org.springframework.http.MediaType;
|
|
@@ -20,6 +24,8 @@ import javax.crypto.spec.SecretKeySpec;
|
|
|
import java.net.URI;
|
|
import java.net.URI;
|
|
|
import java.net.URISyntaxException;
|
|
import java.net.URISyntaxException;
|
|
|
import java.net.URLEncoder;
|
|
import java.net.URLEncoder;
|
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
|
|
+import java.util.Base64;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
|
|
@@ -33,31 +39,25 @@ import java.util.Map;
|
|
|
public class DingTalkRobotClient {
|
|
public class DingTalkRobotClient {
|
|
|
private final static Logger log = LoggerFactory.getLogger(DingTalkRobotClient.class);
|
|
private final static Logger log = LoggerFactory.getLogger(DingTalkRobotClient.class);
|
|
|
|
|
|
|
|
- @Autowired
|
|
|
|
|
- @Qualifier("dingTalkRobotRestTemplate")
|
|
|
|
|
private RestTemplate restTemplate;
|
|
private RestTemplate restTemplate;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 钉钉机器人WebHook地址的默认前缀
|
|
* 钉钉机器人WebHook地址的默认前缀
|
|
|
*/
|
|
*/
|
|
|
- @Value("${dingtalk.robot.prefix:https://oapi.dingtalk.com/robot/send}")
|
|
|
|
|
private String urlPrefix;
|
|
private String urlPrefix;
|
|
|
/**
|
|
/**
|
|
|
* 钉钉机器人WebHook地址的access_token
|
|
* 钉钉机器人WebHook地址的access_token
|
|
|
*/
|
|
*/
|
|
|
- @Value("${dingtalk.robot.access-token}")
|
|
|
|
|
private String accessToken;
|
|
private String accessToken;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 钉钉机器人是否启用加签,默认false,启用加签需设置secret_token
|
|
* 钉钉机器人是否启用加签,默认false,启用加签需设置secret_token
|
|
|
*/
|
|
*/
|
|
|
- @Value("${dingtalk.robot.secret.secret-enabled:false}")
|
|
|
|
|
private boolean secretEnable;
|
|
private boolean secretEnable;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 钉钉机器人WebHook地址的secret_token,群机器人加签用
|
|
* 钉钉机器人WebHook地址的secret_token,群机器人加签用
|
|
|
*/
|
|
*/
|
|
|
- @Value("${dingtalk.robot.secret.secret-token:null}")
|
|
|
|
|
private String secretToken;
|
|
private String secretToken;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -93,9 +93,9 @@ public class DingTalkRobotClient {
|
|
|
try {
|
|
try {
|
|
|
String stringToSign = timestamp + "\n" + secret;
|
|
String stringToSign = timestamp + "\n" + secret;
|
|
|
Mac mac = Mac.getInstance("HmacSHA256");
|
|
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");
|
|
|
|
|
|
|
+ mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
|
|
|
|
|
+ byte[] signData = mac.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8));
|
|
|
|
|
+ String sign = URLEncoder.encode(new String(Base64.getEncoder().encode(signData)), "UTF-8");
|
|
|
log.debug("【发送钉钉群消息】获取到签名sign = {}", sign);
|
|
log.debug("【发送钉钉群消息】获取到签名sign = {}", sign);
|
|
|
return sign;
|
|
return sign;
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
@@ -405,4 +405,24 @@ public class DingTalkRobotClient {
|
|
|
public String getWebhook(String accessToken, String secretToken) {
|
|
public String getWebhook(String accessToken, String secretToken) {
|
|
|
return getWebhook(accessToken, secretToken, true);
|
|
return getWebhook(accessToken, secretToken, true);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ public void setRestTemplate(RestTemplate restTemplate) {
|
|
|
|
|
+ this.restTemplate = restTemplate;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void setUrlPrefix(String urlPrefix) {
|
|
|
|
|
+ this.urlPrefix = urlPrefix;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void setAccessToken(String accessToken) {
|
|
|
|
|
+ this.accessToken = accessToken;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void setSecretEnable(boolean secretEnable) {
|
|
|
|
|
+ this.secretEnable = secretEnable;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void setSecretToken(String secretToken) {
|
|
|
|
|
+ this.secretToken = secretToken;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|