feat: 增加钉钉发送

dev
huangfeng 1 year ago
parent a7d0730768
commit a9e92c5db0

@ -142,6 +142,12 @@
<artifactId>querydsl-apt</artifactId>
<version>${querydsl.version}</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibaba-dingtalk-service-sdk</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
<build>

@ -0,0 +1,53 @@
package com.xydl.cac.util;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;
/**
*
*/
@Slf4j
public class DingTalkPushUtil {
public static final String CUSTOM_ROBOT_TOKEN = "b0571a2261a96ac2346c3c913e25783e0d7994bf1bff7e64e5a5e171eeccf1f8";
public static final String SECRET = "SEC0757cffdf63deea00a57f718f4540e5d97ffe887509d199403c06d9d00c0a158";
public static final String KEY_WORD = "运维告警";
public static void pushText(String content) {
try {
Long timestamp = System.currentTimeMillis();
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");
//sign字段和timestamp字段必须拼接到请求URL上否则会出现 310000 的错误信息
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send?sign=" + sign + "&timestamp=" + timestamp);
OapiRobotSendRequest req = new OapiRobotSendRequest();
/**
*
*/
//定义文本内容
OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
text.setContent(KEY_WORD + content);
//设置消息类型
req.setMsgtype("text");
req.setText(text);
OapiRobotSendResponse rsp = client.execute(req, CUSTOM_ROBOT_TOKEN);
} catch (Exception e) {
log.error("钉钉发送失败", e);
}
}
}

@ -0,0 +1,87 @@
package com.xydl.cac.util;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
import java.io.File;
import java.io.IOException;
import java.util.Map;
public class JSONProcessor {
public Map<String, Object> fromJSON2Map(String json) throws JsonParseException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
@SuppressWarnings("unchecked")
Map<String, Object> productMap = mapper.readValue(json, Map.class);
return productMap;
}
public Map<String, Object> fromJSON2Map(Object obj) throws JsonParseException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
@SuppressWarnings("unchecked")
Map<String, Object> productMap = mapper.convertValue(obj, Map.class);
return productMap;
}
public String buildJSONFromJSONObject(Object obj) {
String jsonInString = null;
ObjectMapper mapper = new ObjectMapper();
try {
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
mapper.setSerializationInclusion(Include.NON_NULL);
jsonInString = mapper.writeValueAsString(obj);
} catch (Exception e) {
// LOG.error("JSON transform failed. ", e);
}
return jsonInString;
}
public String buildJSONFromJSONObject(Object obj, boolean prettyPrinter) {
String jsonInString = null;
ObjectMapper mapper = new ObjectMapper();
try {
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
mapper.setSerializationInclusion(Include.NON_NULL);
if (prettyPrinter) {
jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
} else {
jsonInString = mapper.writeValueAsString(obj);
}
} catch (Exception e) {
// LOG.error("JSON transform failed. ", e);
}
return jsonInString;
}
public <T> T buildJSONObjectFromJSON(String json, Class<T> clazz) throws Exception {
ObjectMapper mapper = new ObjectMapper();
try {
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return mapper.readValue(json, clazz);
} catch (UnrecognizedPropertyException e) {
throw new Exception(e.getPropertyName(), e);
} catch (Exception e) {
throw new Exception("JSON Object transform failed. ", e);
}
}
public <T> T buildJSONObjectFromJSON(File file, Class<T> clazz) throws Exception {
ObjectMapper mapper = new ObjectMapper();
try {
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return mapper.readValue(file, clazz);
} catch (UnrecognizedPropertyException e) {
throw new Exception(e.getPropertyName(), e);
} catch (Exception e) {
throw new Exception("JSON Object transform failed. ", e);
}
}
}

@ -0,0 +1,60 @@
package com.xydl.cac.util;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import java.io.File;
import java.io.IOException;
import java.util.Map;
public class JSONUtil {
private static JSONProcessor jSONProcessor = new JSONProcessor();
public static String object2Json(Object obj) {
return jSONProcessor.buildJSONFromJSONObject(obj);
}
public static String object2Json(Object obj, boolean prettyPrinter) {
return jSONProcessor.buildJSONFromJSONObject(obj, prettyPrinter);
}
public static <T> T json2Object(String json, Class<T> clazz) throws Exception {
return jSONProcessor.buildJSONObjectFromJSON(json, clazz);
}
public static <T> T file2Object(File file, Class<T> clazz) throws Exception {
return jSONProcessor.buildJSONObjectFromJSON(file, clazz);
}
public static Map<String, Object> object2Map(Object obj) {
try {
return jSONProcessor.fromJSON2Map(obj);
} catch (IOException e) {
return null;
}
}
public static String object2Xml(Object obj) throws JsonProcessingException {
XmlMapper xmlMapper = XmlMapper.builder()
.enable(SerializationFeature.INDENT_OUTPUT)
.build();
return xmlMapper.writeValueAsString(obj);
}
public static <T> T xml2Object(String xml, Class<T> clazz) throws JsonProcessingException {
XmlMapper xmlMapper = XmlMapper.builder()
.build();
return xmlMapper.readValue(xml, clazz);
}
public static JsonNode xml2JsonNode(String xml) throws JsonProcessingException {
XmlMapper xmlMapper = XmlMapper.builder()
.build();
return xmlMapper.readTree(xml);
}
}
Loading…
Cancel
Save