diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertChannelFactory.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertChannelFactory.java index ef5fc6f4cb..a77daf7e45 100644 --- a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertChannelFactory.java +++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertChannelFactory.java @@ -62,13 +62,6 @@ public final class WeChatAlertChannelFactory implements AlertChannelFactory { .build()) .build(); - InputParam userSendMsgParam = InputParam.newBuilder(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_USER_SEND_MSG, WeChatAlertParamsConstants.ENTERPRISE_WE_CHAT_USER_SEND_MSG) - .setPlaceholder("please input corp id ") - .addValidate(Validate.newBuilder() - .setRequired(true) - .build()) - .build(); - InputParam agentIdParam = InputParam.newBuilder(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_AGENT_ID, WeChatAlertParamsConstants.ENTERPRISE_WE_CHAT_AGENT_ID) .setPlaceholder("please input agent id ") .addValidate(Validate.newBuilder() @@ -90,7 +83,7 @@ public final class WeChatAlertChannelFactory implements AlertChannelFactory { .addValidate(Validate.newBuilder().setRequired(true).build()) .build(); - return Arrays.asList(corpIdParam, secretParam, usersParam, userSendMsgParam, agentIdParam, sendType, showType); + return Arrays.asList(corpIdParam, secretParam, usersParam, agentIdParam, sendType, showType); } @Override diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertConstants.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertConstants.java index 9c90d63e28..36ebc1c8d4 100644 --- a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertConstants.java +++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertConstants.java @@ -31,6 +31,18 @@ public final class WeChatAlertConstants { static final String WE_CHAT_TOKEN_URL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpId}&corpsecret={secret}"; + static final String WE_CHAT_CONTENT_KEY = "content"; + + static final String WE_CHAT_MESSAGE_TYPE_TEXT = "text"; + + static final Integer WE_CHAT_MESSAGE_SAFE_PUBLICITY = 0; + + static final Integer WE_CHAT_MESSAGE_SAFE_PRIVACY = 1; + + static final Integer WE_CHAT_ENABLE_ID_TRANS = 0; + + static final Integer WE_CHAT_DUPLICATE_CHECK_INTERVAL_ZERO = 0; + private WeChatAlertConstants() { throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); } diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertParamsConstants.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertParamsConstants.java index 5cf9e40e03..db9813dab8 100644 --- a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertParamsConstants.java +++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertParamsConstants.java @@ -24,10 +24,9 @@ public final class WeChatAlertParamsConstants { static final String NAME_ENTERPRISE_WE_CHAT_SECRET = "secret"; static final String ENTERPRISE_WE_CHAT_TEAM_SEND_MSG = "$t('teamSendMsg')"; static final String NAME_ENTERPRISE_WE_CHAT_TEAM_SEND_MSG = "teamSendMsg"; - static final String ENTERPRISE_WE_CHAT_USER_SEND_MSG = "$t('userSendMsg')"; - static final String NAME_ENTERPRISE_WE_CHAT_USER_SEND_MSG = "userSendMsg"; static final String ENTERPRISE_WE_CHAT_AGENT_ID = "$t('agentId')"; static final String NAME_ENTERPRISE_WE_CHAT_AGENT_ID = "agentId"; + static final String NAME_ENTERPRISE_WE_CHAT_CHAT_ID = "chatId"; static final String ENTERPRISE_WE_CHAT_USERS = "$t('users')"; static final String NAME_ENTERPRISE_WE_CHAT_USERS = "users"; diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatSender.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatSender.java index badd6f2525..8dfdf50986 100644 --- a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatSender.java +++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatSender.java @@ -18,6 +18,7 @@ package org.apache.dolphinscheduler.plugin.alert.wechat; import static java.util.Objects.requireNonNull; +import static org.apache.dolphinscheduler.plugin.alert.wechat.WeChatAlertConstants.*; import org.apache.dolphinscheduler.alert.api.AlertConstants; import org.apache.dolphinscheduler.alert.api.AlertResult; @@ -35,8 +36,6 @@ import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import java.io.IOException; -import java.util.Arrays; -import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; @@ -59,8 +58,8 @@ public final class WeChatSender { private static final String SECRET_REGEX = "{secret}"; private static final String TOKEN_REGEX = "{token}"; private final String weChatAgentId; + private final String weChatChatId; private final String weChatUsers; - private final String weChatUserSendMsg; private final String weChatTokenUrlReplace; private final String weChatToken; private final String sendType; @@ -68,11 +67,11 @@ public final class WeChatSender { WeChatSender(Map config) { weChatAgentId = config.get(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_AGENT_ID); + weChatChatId = config.get(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_CHAT_ID); weChatUsers = config.get(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_USERS); String weChatCorpId = config.get(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_CORP_ID); String weChatSecret = config.get(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_SECRET); String weChatTokenUrl = WeChatAlertConstants.WE_CHAT_TOKEN_URL; - weChatUserSendMsg = config.get(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_USER_SEND_MSG); sendType = config.get(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_SEND_TYPE); showType = config.get(AlertConstants.NAME_SHOW_TYPE); requireNonNull(showType, AlertConstants.NAME_SHOW_TYPE + MUST_NOT_NULL); @@ -96,7 +95,7 @@ public final class WeChatSender { response.close(); } logger.info("Enterprise WeChat send [{}], param:{}, resp:{}", - url, data, resp); + url, data, resp); return resp; } } @@ -228,21 +227,6 @@ public final class WeChatSender { return alertResult; } - /** - * make user multi user message - * - * @param toUser the toUser - * @param agentId the agentId - * @param msg the msg - * @return Enterprise WeChat send message - */ - private String makeUserSendMsg(Collection toUser, String agentId, String msg) { - String listUser = mkString(toUser); - return weChatUserSendMsg.replace(USER_REG_EXP, listUser) - .replace(AGENT_ID_REG_EXP, agentId) - .replace(MSG_REG_EXP, msg); - } - /** * send Enterprise WeChat * @@ -250,9 +234,7 @@ public final class WeChatSender { */ public AlertResult sendEnterpriseWeChat(String title, String content) { AlertResult alertResult; - List userList = Arrays.asList(weChatUsers.split(",")); String data = markdownByAlert(title, content); - String msg = makeUserSendMsg(userList, weChatAgentId, data); if (null == weChatToken) { alertResult = new AlertResult(); alertResult.setMessage("send we chat alert fail,get weChat token error"); @@ -260,14 +242,21 @@ public final class WeChatSender { return alertResult; } String enterpriseWeChatPushUrlReplace = ""; + Map contentMap=new HashMap<>(); + contentMap.put(WeChatAlertConstants.WE_CHAT_CONTENT_KEY,data); + String msgJson=""; if (sendType.equals(WeChatType.APP.getDescp())) { enterpriseWeChatPushUrlReplace = WeChatAlertConstants.WE_CHAT_PUSH_URL.replace(TOKEN_REGEX, weChatToken); + WechatAppMessage wechatAppMessage=new WechatAppMessage(weChatUsers, WE_CHAT_MESSAGE_TYPE_TEXT, Integer.valueOf(weChatAgentId),contentMap, WE_CHAT_MESSAGE_SAFE_PUBLICITY, WE_CHAT_ENABLE_ID_TRANS, WE_CHAT_DUPLICATE_CHECK_INTERVAL_ZERO); + msgJson=JSONUtils.toJsonString(wechatAppMessage); } else if (sendType.equals(WeChatType.APPCHAT.getDescp())) { enterpriseWeChatPushUrlReplace = WeChatAlertConstants.WE_CHAT_APP_CHAT_PUSH_URL.replace(TOKEN_REGEX, weChatToken); + WechatAppChatMessage wechatAppChatMessage=new WechatAppChatMessage(weChatChatId, WE_CHAT_MESSAGE_TYPE_TEXT, contentMap, WE_CHAT_MESSAGE_SAFE_PUBLICITY); + msgJson=JSONUtils.toJsonString(wechatAppChatMessage); } try { - return checkWeChatSendMsgResult(post(enterpriseWeChatPushUrlReplace, msg)); + return checkWeChatSendMsgResult(post(enterpriseWeChatPushUrlReplace, msgJson)); } catch (Exception e) { logger.info("send we chat alert msg exception : {}", e.getMessage()); alertResult = new AlertResult(); diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WechatAppChatMessage.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WechatAppChatMessage.java new file mode 100644 index 0000000000..5294972b3c --- /dev/null +++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WechatAppChatMessage.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.dolphinscheduler.plugin.alert.wechat; + +import java.util.Map; + +public class WechatAppChatMessage { + + private String chatid; + private String msgtype; + private Map text; + private Integer safe; + + public String getChatid() { + return chatid; + } + + public void setChatid(String chatid) { + this.chatid = chatid; + } + + public String getMsgtype() { + return msgtype; + } + + public void setMsgtype(String msgtype) { + this.msgtype = msgtype; + } + + public Map getText() { + return text; + } + + public void setText(Map text) { + this.text = text; + } + + public Integer getSafe() { + return safe; + } + + public void setSafe(Integer safe) { + this.safe = safe; + } + + public WechatAppChatMessage() { + } + + public WechatAppChatMessage(String chatid, String msgtype, Map text, Integer safe) { + this.chatid = chatid; + this.msgtype = msgtype; + this.text = text; + this.safe = safe; + } +} diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WechatAppMessage.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WechatAppMessage.java new file mode 100644 index 0000000000..4032e4856f --- /dev/null +++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WechatAppMessage.java @@ -0,0 +1,100 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.dolphinscheduler.plugin.alert.wechat; + +import java.util.Map; + +public class WechatAppMessage { + + private String touser; + private String msgtype; + private Integer agentid; + private Map text; + private Integer safe; + private Integer enable_id_trans; + private Integer enable_duplicate_check; + + public String getTouser() { + return touser; + } + + public void setTouser(String touser) { + this.touser = touser; + } + + public String getMsgtype() { + return msgtype; + } + + public void setMsgtype(String msgtype) { + this.msgtype = msgtype; + } + + public Integer getAgentid() { + return agentid; + } + + public void setAgentid(Integer agentid) { + this.agentid = agentid; + } + + public Map getText() { + return text; + } + + public void setText(Map text) { + this.text = text; + } + + public Integer getSafe() { + return safe; + } + + public void setSafe(Integer safe) { + this.safe = safe; + } + + public Integer getEnable_id_trans() { + return enable_id_trans; + } + + public void setEnable_id_trans(Integer enable_id_trans) { + this.enable_id_trans = enable_id_trans; + } + + public Integer getEnable_duplicate_check() { + return enable_duplicate_check; + } + + public void setEnable_duplicate_check(Integer enable_duplicate_check) { + this.enable_duplicate_check = enable_duplicate_check; + } + + public WechatAppMessage() { + } + + public WechatAppMessage(String touser, String msgtype, Integer agentid, Map text, Integer safe, Integer enable_id_trans, Integer enable_duplicate_check) { + this.touser = touser; + this.msgtype = msgtype; + this.agentid = agentid; + this.text = text; + this.safe = safe; + this.enable_id_trans = enable_id_trans; + this.enable_duplicate_check = enable_duplicate_check; + } +} diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/test/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertChannelFactoryTest.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/test/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertChannelFactoryTest.java index 648c206a33..04ecc36482 100644 --- a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/test/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertChannelFactoryTest.java +++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/test/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertChannelFactoryTest.java @@ -36,7 +36,7 @@ public class WeChatAlertChannelFactoryTest { WeChatAlertChannelFactory weChatAlertChannelFactory = new WeChatAlertChannelFactory(); List params = weChatAlertChannelFactory.params(); JSONUtils.toJsonString(params); - Assert.assertEquals(7, params.size()); + Assert.assertEquals(6, params.size()); } @Test diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/test/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatSenderTest.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/test/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatSenderTest.java index 73d889b652..77ee0c2dbe 100644 --- a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/test/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatSenderTest.java +++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/test/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatSenderTest.java @@ -61,10 +61,6 @@ public class WeChatSenderTest { weChatConfig.put(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_AGENT_ID, "100000"); weChatConfig.put(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_CORP_ID, "NAME_ENTERPRISE_WE_CHAT_CORP_ID"); weChatConfig.put(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_SECRET, "NAME_ENTERPRISE_WE_CHAT_SECRET"); - weChatConfig.put(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_USER_SEND_MSG, "{\"touser\":\"{toUser}\",\"agentid\":{agentId}" - + - ",\"msgtype\":\"markdown\",\"markdown\":{\"content\":\"{msg}\"}}" - ); weChatConfig.put(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_USERS, "Kris"); weChatConfig.put(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_TEAM_SEND_MSG, "msg"); weChatConfig.put(AlertConstants.NAME_SHOW_TYPE, ShowType.TABLE.getDescp()); diff --git a/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/process/ProcessService.java b/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/process/ProcessService.java index cf12b05960..8a9427f5f8 100644 --- a/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/process/ProcessService.java +++ b/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/process/ProcessService.java @@ -1307,7 +1307,7 @@ public class ProcessService { /** * complement data needs transform parent parameter to child. */ - private String getSubWorkFlowParam(ProcessInstanceMap instanceMap, ProcessInstance parentProcessInstance, Map fatherParams) { + protected String getSubWorkFlowParam(ProcessInstanceMap instanceMap, ProcessInstance parentProcessInstance, Map fatherParams) { // set sub work process command String processMapStr = JSONUtils.toJsonString(instanceMap); Map cmdParam = JSONUtils.toMap(processMapStr);