Skip to content

Commit

Permalink
use http instead of alibaba-dingtalk-service-sdk(#844) (#996)
Browse files Browse the repository at this point in the history
* use http instead of alibaba-dingtalk-service-sdk

* rm alibaba-dingtalk-service-sdk dependency

* optimize DingMsg result
  • Loading branch information
baymax55 authored Nov 22, 2022
1 parent 1a9bc6d commit 1910ddc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 29 deletions.
12 changes: 0 additions & 12 deletions hippo4j-message/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,31 @@
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;

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
Expand Down Expand Up @@ -86,20 +89,35 @@ protected void execute(RobotMessageExecuteDTO robotMessageExecuteDTO) {
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;
}
}
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 1910ddc

Please sign in to comment.