From e4db2d6b8715d5b22a27e715d1c01dfdad3a5ea7 Mon Sep 17 00:00:00 2001 From: saowu <786021717@qq.com> Date: Mon, 9 Sep 2024 21:35:09 +0800 Subject: [PATCH] [Fix-16569][alert] Feishu send alert message failed (#16588) --- .../alert/feishu/FeiShuAlertChannel.java | 2 +- .../plugin/alert/feishu/FeiShuSender.java | 117 ++++-------------- .../plugin/alert/feishu/FeiShuSenderTest.java | 5 +- 3 files changed, 29 insertions(+), 95 deletions(-) diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuAlertChannel.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuAlertChannel.java index 29c78a9d1b..20c7828412 100644 --- a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuAlertChannel.java +++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuAlertChannel.java @@ -31,7 +31,7 @@ public final class FeiShuAlertChannel implements AlertChannel { AlertData alertData = alertInfo.getAlertData(); Map paramsMap = alertInfo.getAlertParams(); if (null == paramsMap) { - return new AlertResult(false, "fei shu params is null"); + return new AlertResult(false, "feishu params is null"); } return new FeiShuSender(paramsMap).sendFeiShuMsg(alertData); } diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuSender.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuSender.java index 05a34ccecc..07ba19ef39 100644 --- a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuSender.java +++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuSender.java @@ -37,6 +37,10 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; import com.fasterxml.jackson.annotation.JsonProperty; @@ -83,25 +87,24 @@ public final class FeiShuSender { alertResult.setSuccess(false); if (org.apache.commons.lang3.StringUtils.isBlank(result)) { - alertResult.setMessage("send fei shu msg error"); - log.info("send fei shu msg error,fei shu server resp is null"); + alertResult.setMessage("send feishu msg error: feishu server resp is blank."); + log.info("send feishu msg error: feishu server resp is blank."); return alertResult; } FeiShuSendMsgResponse sendMsgResponse = JSONUtils.parseObject(result, FeiShuSendMsgResponse.class); if (null == sendMsgResponse) { - alertResult.setMessage("send fei shu msg fail"); - log.info("send fei shu msg error,resp error"); + alertResult.setMessage("send feishu msg error: feishu server resp parse error is null."); + log.info("send feishu msg error: feishu server resp parse error is null."); return alertResult; } - if (sendMsgResponse.statusCode == 0) { + if (sendMsgResponse.code == 0) { alertResult.setSuccess(true); - alertResult.setMessage("send fei shu msg success"); + alertResult.setMessage("send feishu msg success."); return alertResult; } - alertResult.setMessage(String.format("alert send fei shu msg error : %s", sendMsgResponse.getStatusMessage())); - log.info("alert send fei shu msg error : {} ,Extra : {} ", sendMsgResponse.getStatusMessage(), - sendMsgResponse.getExtra()); + alertResult.setMessage(String.format("alert send feishu msg error: %s", sendMsgResponse.getMsg())); + log.info("alert send feishu msg error: {}", sendMsgResponse); return alertResult; } @@ -134,10 +137,10 @@ public final class FeiShuSender { String resp = sendMsg(alertData); return checkSendFeiShuSendMsgResult(resp); } catch (Exception e) { - log.error("send fei shu alert failed:", e); + log.error("send feishu alert failed:", e); alertResult = new AlertResult(); alertResult.setSuccess(false); - alertResult.setMessage("send fei shu alert fail."); + alertResult.setMessage("send feishu alert fail."); } return alertResult; } @@ -167,95 +170,25 @@ public final class FeiShuSender { } finally { response.close(); } - log.info("Fei Shu send title :{} ,content :{}, resp: {}", alertData.getTitle(), alertData.getContent(), + log.info("feishu send title: {}, content: {}, resp: {}", alertData.getTitle(), alertData.getContent(), resp); return resp; } finally { httpClient.close(); } } - + @AllArgsConstructor + @Builder + @Data + @NoArgsConstructor static final class FeiShuSendMsgResponse { - @JsonProperty("Extra") - private String extra; - @JsonProperty("StatusCode") - private Integer statusCode; - @JsonProperty("StatusMessage") - private String statusMessage; - - public FeiShuSendMsgResponse() { - } - - public String getExtra() { - return this.extra; - } - - @JsonProperty("Extra") - public void setExtra(String extra) { - this.extra = extra; - } - - public Integer getStatusCode() { - return this.statusCode; - } - - @JsonProperty("StatusCode") - public void setStatusCode(Integer statusCode) { - this.statusCode = statusCode; - } - - public String getStatusMessage() { - return this.statusMessage; - } + @JsonProperty("data") + private Object data; + @JsonProperty("code") + private Integer code; + @JsonProperty("msg") + private String msg; - @JsonProperty("StatusMessage") - public void setStatusMessage(String statusMessage) { - this.statusMessage = statusMessage; - } - - public boolean equals(final Object o) { - if (o == this) { - return true; - } - if (!(o instanceof FeiShuSendMsgResponse)) { - return false; - } - final FeiShuSendMsgResponse other = (FeiShuSendMsgResponse) o; - final Object this$extra = this.getExtra(); - final Object other$extra = other.getExtra(); - if (this$extra == null ? other$extra != null : !this$extra.equals(other$extra)) { - return false; - } - final Object this$statusCode = this.getStatusCode(); - final Object other$statusCode = other.getStatusCode(); - if (this$statusCode == null ? other$statusCode != null : !this$statusCode.equals(other$statusCode)) { - return false; - } - final Object this$statusMessage = this.getStatusMessage(); - final Object other$statusMessage = other.getStatusMessage(); - if (this$statusMessage == null ? other$statusMessage != null - : !this$statusMessage.equals(other$statusMessage)) { - return false; - } - return true; - } - - public int hashCode() { - final int PRIME = 59; - int result = 1; - final Object $extra = this.getExtra(); - result = result * PRIME + ($extra == null ? 43 : $extra.hashCode()); - final Object $statusCode = this.getStatusCode(); - result = result * PRIME + ($statusCode == null ? 43 : $statusCode.hashCode()); - final Object $statusMessage = this.getStatusMessage(); - result = result * PRIME + ($statusMessage == null ? 43 : $statusMessage.hashCode()); - return result; - } - - public String toString() { - return "FeiShuSender.FeiShuSendMsgResponse(extra=" + this.getExtra() + ", statusCode=" - + this.getStatusCode() + ", statusMessage=" + this.getStatusMessage() + ")"; - } } } diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/src/test/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuSenderTest.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/src/test/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuSenderTest.java index 829b02dea6..20800cd724 100644 --- a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/src/test/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuSenderTest.java +++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/src/test/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuSenderTest.java @@ -89,9 +89,10 @@ public class FeiShuSenderTest { AlertResult alertResult = feiShuSender.checkSendFeiShuSendMsgResult(""); Assertions.assertFalse(alertResult.isSuccess()); AlertResult alertResult2 = feiShuSender.checkSendFeiShuSendMsgResult("123"); - Assertions.assertEquals("send fei shu msg fail", alertResult2.getMessage()); + Assertions.assertEquals("send feishu msg error: feishu server resp parse error is null.", + alertResult2.getMessage()); - String response = "{\"StatusCode\":\"0\",\"extra\":\"extra\",\"StatusMessage\":\"StatusMessage\"}"; + String response = "{\"code\":\"0\",\"data\":{},\"msg\":\"success\"}"; AlertResult alertResult3 = feiShuSender.checkSendFeiShuSendMsgResult(response); Assertions.assertTrue(alertResult3.isSuccess()); }