未验证 提交 03a0c9f5 编写于 作者: T Tq 提交者: GitHub

[Bug] [ALERT-9224] fix wechat alert functions: app and group chat (#9225)

* fix wechat alert with app and group chat

* add apache header and fix imports

* delete wechat alert 'userSendMsg' property code

* delete wechat alert 'userSendMsg' property code in Test

* delete wechat alert 'userSendMsg' property code in factory Test

* change `getSubWorkFlowParam` from private to protected
上级 0d0674df
......@@ -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
......
......@@ -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");
}
......
......@@ -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";
......
......@@ -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<String, String> 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<String> 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<String> 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<String,String> 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();
......
/*
* 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<String,String> 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<String, String> getText() {
return text;
}
public void setText(Map<String, String> 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<String, String> text, Integer safe) {
this.chatid = chatid;
this.msgtype = msgtype;
this.text = text;
this.safe = safe;
}
}
/*
* 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<String,String> 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<String, String> getText() {
return text;
}
public void setText(Map<String, String> 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<String, String> 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;
}
}
......@@ -36,7 +36,7 @@ public class WeChatAlertChannelFactoryTest {
WeChatAlertChannelFactory weChatAlertChannelFactory = new WeChatAlertChannelFactory();
List<PluginParams> params = weChatAlertChannelFactory.params();
JSONUtils.toJsonString(params);
Assert.assertEquals(7, params.size());
Assert.assertEquals(6, params.size());
}
@Test
......
......@@ -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());
......
......@@ -1307,7 +1307,7 @@ public class ProcessService {
/**
* complement data needs transform parent parameter to child.
*/
private String getSubWorkFlowParam(ProcessInstanceMap instanceMap, ProcessInstance parentProcessInstance, Map<String, String> fatherParams) {
protected String getSubWorkFlowParam(ProcessInstanceMap instanceMap, ProcessInstance parentProcessInstance, Map<String, String> fatherParams) {
// set sub work process command
String processMapStr = JSONUtils.toJsonString(instanceMap);
Map<String, String> cmdParam = JSONUtils.toMap(processMapStr);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册