|
|
|
@ -0,0 +1,94 @@ |
|
|
|
package com.qniao.dam.infrastructure.utils; |
|
|
|
|
|
|
|
import cn.hutool.http.HttpUtil; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.dingtalk.api.DefaultDingTalkClient; |
|
|
|
import com.dingtalk.api.DingTalkClient; |
|
|
|
import com.dingtalk.api.request.OapiRobotSendRequest; |
|
|
|
import com.taobao.api.ApiException; |
|
|
|
import com.taobao.api.TaobaoResponse; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.lang.StringUtils; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
@Component |
|
|
|
@Slf4j |
|
|
|
public class DingTalkWarningUtil { |
|
|
|
|
|
|
|
private static Integer contextSize = 600; |
|
|
|
|
|
|
|
private final static String dingTalkUrl = "https://oapi.dingtalk.com/robot/send?access_token="; |
|
|
|
|
|
|
|
/** |
|
|
|
* 发送钉钉报错信息 |
|
|
|
* |
|
|
|
* @param data 数据 |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public static boolean send(String accessToken, Map<String, String> data) { |
|
|
|
OapiRobotSendRequest request = new OapiRobotSendRequest(); |
|
|
|
if (data.containsKey("Msgtype")) { |
|
|
|
request.setMsgtype(data.get("Msgtype")); |
|
|
|
} else { |
|
|
|
request.setMsgtype("text"); |
|
|
|
|
|
|
|
} |
|
|
|
OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text(); |
|
|
|
String content = ""; |
|
|
|
|
|
|
|
if (data.containsKey("time")) { |
|
|
|
content = content + data.get("time"); |
|
|
|
} |
|
|
|
if (data.containsKey("traceId")) { |
|
|
|
content = content + "[" + data.get("traceId") + "]"; |
|
|
|
} |
|
|
|
if (data.containsKey("requestUri")) { |
|
|
|
content = content + "[" + data.get("requestUri") + "]"; |
|
|
|
} |
|
|
|
|
|
|
|
if (data.containsKey("content")) { |
|
|
|
content = content + data.get("content"); |
|
|
|
} |
|
|
|
Integer csize = contextSize; |
|
|
|
if (data.containsKey("contextSize")) { |
|
|
|
csize = Integer.parseInt(data.get("contextSize")); |
|
|
|
} |
|
|
|
if (content.length() > csize) { |
|
|
|
content = content.substring(0, csize); |
|
|
|
} |
|
|
|
log.info(content); |
|
|
|
if (data.containsKey("Msgtype") && data.get("Msgtype").equals("markdown")) { |
|
|
|
OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown(); |
|
|
|
markdown.setTitle(data.get("toastType")); |
|
|
|
if (data.containsKey("RemindUser")) { |
|
|
|
content = "@" + data.get("RemindUser") + " <br>" + content; |
|
|
|
} |
|
|
|
markdown.setText(content); |
|
|
|
request.setMarkdown(markdown); |
|
|
|
} else { |
|
|
|
if (data.containsKey("toastType")) { |
|
|
|
content = data.get("toastType") + content; |
|
|
|
} |
|
|
|
text.setContent(content); |
|
|
|
request.setText(text); |
|
|
|
} |
|
|
|
try { |
|
|
|
TaobaoResponse response = getTalkClient(accessToken).execute(request); |
|
|
|
if (response.getErrorCode() != null && !response.getErrorCode().equals("0")) { |
|
|
|
log.error("DingTalk接口异常", response); |
|
|
|
return false; |
|
|
|
} |
|
|
|
} catch (ApiException e) { |
|
|
|
log.error("DingTalk接口异常", e); |
|
|
|
return false; |
|
|
|
} |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
private static DingTalkClient getTalkClient(String accessToken) { |
|
|
|
return StringUtils.isNotBlank(accessToken) ? new DefaultDingTalkClient(dingTalkUrl + accessToken) : null; |
|
|
|
} |
|
|
|
|
|
|
|
} |