use http instead of alibaba-dingtalk-service-sdk(#844) (#996)

* use http instead of alibaba-dingtalk-service-sdk

* rm alibaba-dingtalk-service-sdk dependency

* optimize DingMsg result
pull/1006/head
baymax55 2 years ago committed by GitHub
parent 1a9bc6de38
commit 1910ddc8a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -15,18 +15,6 @@
<artifactId>hippo4j-core</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibaba-dingtalk-service-sdk</artifactId>
<version>${dingtalk-sdk.version}</version>
<!-- User feedback that javax.jms cannot be downloaded, log4j is not found useful, so it is excluded for the time being. -->
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>

@ -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;
}
}

@ -51,7 +51,6 @@
<hibernate-validator.version>6.1.5.Final</hibernate-validator.version>
<jjwt.version>0.9.0</jjwt.version>
<jackson-bom.version>2.11.1</jackson-bom.version>
<dingtalk-sdk.version>2.0.0</dingtalk-sdk.version>
<h2.version>2.1.214</h2.version>
<!-- Framework -->
<dubbo.version>3.0.5</dubbo.version>

Loading…
Cancel
Save