|
@@ -52,7 +52,7 @@ public class DingTalkUtil {
|
|
/*
|
|
/*
|
|
* 上传文件,返回mediaid
|
|
* 上传文件,返回mediaid
|
|
*/
|
|
*/
|
|
- public String upload(String filePath) throws DingTalkApiException {
|
|
|
|
|
|
+ public String upload(String filePath, String type) throws DingTalkApiException {
|
|
String url = "https://oapi.dingtalk.com/media/upload?access_token={0}";
|
|
String url = "https://oapi.dingtalk.com/media/upload?access_token={0}";
|
|
HttpHeaders httpHeaders = new HttpHeaders();
|
|
HttpHeaders httpHeaders = new HttpHeaders();
|
|
httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
|
|
httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
|
|
@@ -60,7 +60,7 @@ public class DingTalkUtil {
|
|
MultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<>();
|
|
MultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<>();
|
|
FileSystemResource fileSystemResource = new FileSystemResource(filePath);
|
|
FileSystemResource fileSystemResource = new FileSystemResource(filePath);
|
|
multiValueMap.add("media", fileSystemResource);
|
|
multiValueMap.add("media", fileSystemResource);
|
|
- multiValueMap.add("type", "image");
|
|
|
|
|
|
+ multiValueMap.add("type", type);
|
|
|
|
|
|
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(multiValueMap, httpHeaders);
|
|
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(multiValueMap, httpHeaders);
|
|
|
|
|
|
@@ -84,15 +84,20 @@ public class DingTalkUtil {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- /*
|
|
|
|
- * 使用钉钉机器人发送消息
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 使用钉钉机器人发送图片消息
|
|
|
|
+ *
|
|
|
|
+ * @param msgParam
|
|
|
|
+ * @throws DingTalkApiException
|
|
*/
|
|
*/
|
|
- public void sendMsgWithRobot(Object msgParam) throws DingTalkApiException {
|
|
|
|
|
|
+ public void sendImageMsgWithRobot(String mediaId) throws DingTalkApiException {
|
|
String url = "https://api.dingtalk.com/v1.0/robot/groupMessages/send";
|
|
String url = "https://api.dingtalk.com/v1.0/robot/groupMessages/send";
|
|
|
|
|
|
HttpHeaders httpHeaders = new HttpHeaders();
|
|
HttpHeaders httpHeaders = new HttpHeaders();
|
|
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
|
|
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
|
|
httpHeaders.set("x-acs-dingtalk-access-token", getApiToken());
|
|
httpHeaders.set("x-acs-dingtalk-access-token", getApiToken());
|
|
|
|
+ Map<String, Object> msgParam = new HashMap<>();
|
|
|
|
+ msgParam.put("photoURL", mediaId);
|
|
|
|
|
|
Map<String, Object> request = new HashMap<>();
|
|
Map<String, Object> request = new HashMap<>();
|
|
try {
|
|
try {
|
|
@@ -114,7 +119,90 @@ public class DingTalkUtil {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} catch (JsonProcessingException e) {
|
|
} catch (JsonProcessingException e) {
|
|
- throw new DingTalkApiException("入参格式错误--" + msgParam.toString());
|
|
|
|
|
|
+ throw new DingTalkApiException("JSON转化失败--" + msgParam);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 使用钉钉机器人发送File消息
|
|
|
|
+ *
|
|
|
|
+ * @param mediaId
|
|
|
|
+ * @param fileName
|
|
|
|
+ * @throws DingTalkApiException
|
|
|
|
+ */
|
|
|
|
+ public void sendFileMsgWithRobot(String mediaId, String fileName) throws DingTalkApiException {
|
|
|
|
+ String url = "https://api.dingtalk.com/v1.0/robot/groupMessages/send";
|
|
|
|
+
|
|
|
|
+ HttpHeaders httpHeaders = new HttpHeaders();
|
|
|
|
+ httpHeaders.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
+ httpHeaders.set("x-acs-dingtalk-access-token", getApiToken());
|
|
|
|
+
|
|
|
|
+ Map<String, Object> msgParam = new HashMap<>();
|
|
|
|
+ msgParam.put("mediaId", mediaId);
|
|
|
|
+ msgParam.put("fileName", fileName);
|
|
|
|
+ msgParam.put("fileType", fileName.substring(fileName.lastIndexOf('.') + 1));
|
|
|
|
+
|
|
|
|
+ Map<String, Object> request = new HashMap<>();
|
|
|
|
+ try {
|
|
|
|
+ request.put("msgParam", objectMapper.writeValueAsString(msgParam));
|
|
|
|
+ request.put("msgKey", "sampleFile");
|
|
|
|
+ request.put("openConversationId", conversationId);
|
|
|
|
+ request.put("robotCode", appKey);
|
|
|
|
+
|
|
|
|
+ HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(request, httpHeaders);
|
|
|
|
+
|
|
|
|
+ ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
|
|
|
|
+
|
|
|
|
+ if (responseEntity.getStatusCode().is2xxSuccessful()) {
|
|
|
|
+ JsonNode node = objectMapper.readTree(responseEntity.getBody());
|
|
|
|
+ if (node.get("processQueryKey") != null) {
|
|
|
|
+ return;
|
|
|
|
+ } else {
|
|
|
|
+ throw new DingTalkApiException("api返回格式有误--" + responseEntity.getBody());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } catch (JsonProcessingException e) {
|
|
|
|
+ throw new DingTalkApiException("入参格式错误--" + msgParam);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 使用钉钉机器人发送文字消息
|
|
|
|
+ *
|
|
|
|
+ * @param msg
|
|
|
|
+ * @throws DingTalkApiException
|
|
|
|
+ */
|
|
|
|
+ public void sendTextMsgWithRobot(String msg) throws DingTalkApiException {
|
|
|
|
+ String url = "https://api.dingtalk.com/v1.0/robot/groupMessages/send";
|
|
|
|
+
|
|
|
|
+ HttpHeaders httpHeaders = new HttpHeaders();
|
|
|
|
+ httpHeaders.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
+ httpHeaders.set("x-acs-dingtalk-access-token", getApiToken());
|
|
|
|
+
|
|
|
|
+ Map<String, Object> msgParam = new HashMap<>();
|
|
|
|
+ msgParam.put("content", msg);
|
|
|
|
+
|
|
|
|
+ Map<String, Object> request = new HashMap<>();
|
|
|
|
+ try {
|
|
|
|
+ request.put("msgParam", objectMapper.writeValueAsString(msgParam));
|
|
|
|
+ request.put("msgKey", "sampleText");
|
|
|
|
+ request.put("openConversationId", conversationId);
|
|
|
|
+ request.put("robotCode", appKey);
|
|
|
|
+
|
|
|
|
+ HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(request, httpHeaders);
|
|
|
|
+
|
|
|
|
+ ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
|
|
|
|
+
|
|
|
|
+ if (responseEntity.getStatusCode().is2xxSuccessful()) {
|
|
|
|
+ JsonNode node = objectMapper.readTree(responseEntity.getBody());
|
|
|
|
+ if (node.get("processQueryKey") != null) {
|
|
|
|
+ return;
|
|
|
|
+ } else {
|
|
|
|
+ throw new DingTalkApiException("api返回格式有误--" + responseEntity.getBody());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } catch (JsonProcessingException e) {
|
|
|
|
+ throw new DingTalkApiException("入参格式错误--" + msgParam);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|