|
|
|
@ -21,16 +21,16 @@ import cn.hippo4j.common.toolkit.CollectionUtil;
|
|
|
|
|
import cn.hippo4j.common.toolkit.FileUtil;
|
|
|
|
|
import cn.hippo4j.common.toolkit.Singleton;
|
|
|
|
|
import cn.hippo4j.common.toolkit.StringUtil;
|
|
|
|
|
import cn.hippo4j.common.toolkit.JSONUtil;
|
|
|
|
|
import cn.hippo4j.common.toolkit.Assert;
|
|
|
|
|
import cn.hippo4j.common.toolkit.http.HttpUtil;
|
|
|
|
|
import cn.hippo4j.message.dto.NotifyConfigDTO;
|
|
|
|
|
import cn.hippo4j.message.enums.NotifyPlatformEnum;
|
|
|
|
|
import cn.hippo4j.message.platform.base.AbstractRobotSendMessageHandler;
|
|
|
|
|
import cn.hippo4j.message.platform.base.RobotMessageActualContent;
|
|
|
|
|
import cn.hippo4j.message.platform.base.RobotMessageExecuteDTO;
|
|
|
|
|
import cn.hippo4j.message.platform.constant.DingAlarmConstants;
|
|
|
|
|
import com.dingtalk.api.DefaultDingTalkClient;
|
|
|
|
|
import com.dingtalk.api.DingTalkClient;
|
|
|
|
|
import com.dingtalk.api.request.OapiRobotSendRequest;
|
|
|
|
|
import com.taobao.api.ApiException;
|
|
|
|
|
import lombok.Data;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.apache.commons.codec.binary.Base64;
|
|
|
|
|
|
|
|
|
@ -38,11 +38,14 @@ import javax.crypto.Mac;
|
|
|
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
|
|
|
|
|
import static cn.hippo4j.message.platform.constant.DingAlarmConstants.*;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* doc:<a href="https://open.dingtalk.com/document/robots/custom-robot-access">自定义机器人接入</a>
|
|
|
|
|
* Send ding notification message.
|
|
|
|
|
*/
|
|
|
|
|
@Slf4j
|
|
|
|
@ -86,20 +89,35 @@ public class DingSendMessageHandler extends AbstractRobotSendMessageHandler {
|
|
|
|
|
log.error("Failed to sign the message sent by nailing.", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
DingTalkClient dingTalkClient = new DefaultDingTalkClient(serverUrl);
|
|
|
|
|
OapiRobotSendRequest request = new OapiRobotSendRequest();
|
|
|
|
|
request.setMsgtype("markdown");
|
|
|
|
|
OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
|
|
|
|
|
markdown.setTitle(Objects.equals(notifyConfig.getType(), "CONFIG") ? DING_NOTICE_TITLE : DING_ALARM_TITLE);
|
|
|
|
|
markdown.setText(robotMessageExecuteDTO.getText());
|
|
|
|
|
OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
|
|
|
|
|
at.setAtMobiles(CollectionUtil.newArrayList(notifyConfig.getReceives().split(",")));
|
|
|
|
|
request.setAt(at);
|
|
|
|
|
request.setMarkdown(markdown);
|
|
|
|
|
String title = Objects.equals(notifyConfig.getType(), "CONFIG") ? DING_NOTICE_TITLE : DING_ALARM_TITLE;
|
|
|
|
|
String text = robotMessageExecuteDTO.getText();
|
|
|
|
|
ArrayList<String> atMobiles = CollectionUtil.newArrayList(notifyConfig.getReceives().split(","));
|
|
|
|
|
|
|
|
|
|
HashMap<String, Object> markdown = new HashMap<>();
|
|
|
|
|
markdown.put("title", title);
|
|
|
|
|
markdown.put("text", text);
|
|
|
|
|
HashMap<String, Object> at = new HashMap<>();
|
|
|
|
|
at.put("atMobiles", atMobiles);
|
|
|
|
|
|
|
|
|
|
HashMap<String, Object> markdownJson = new HashMap<>();
|
|
|
|
|
markdownJson.put("msgtype", "markdown");
|
|
|
|
|
markdownJson.put("markdown", markdown);
|
|
|
|
|
markdownJson.put("at", at);
|
|
|
|
|
try {
|
|
|
|
|
dingTalkClient.execute(request);
|
|
|
|
|
} catch (ApiException ex) {
|
|
|
|
|
String responseBody = HttpUtil.post(serverUrl, markdownJson);
|
|
|
|
|
DingRobotResponse response = JSONUtil.parseObject(responseBody, DingRobotResponse.class);
|
|
|
|
|
Assert.isTrue(response != null, "response is null");
|
|
|
|
|
if (response.getErrcode() != 0) {
|
|
|
|
|
log.error("Ding failed to send message,reason : {}", response.errmsg);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
log.error("Ding failed to send message", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Data
|
|
|
|
|
static class DingRobotResponse {
|
|
|
|
|
private Long errcode;
|
|
|
|
|
private String errmsg;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|