Browse Source

[Bug-9235][Alert]Fix wechat markdown message and change wechat form structure (#9367)

* fix wechat issues:
1. change table msg type to markdown.
2. change userId to not required and enrich hints
3. change 'app id' to 'app id and chat id'

* fix wechat issues:
1. revert table showtype and add markdown showtype.
2. enrich hints.
3. delete 'chatid', rename agentid to weChatAgentIdChatId.
4. modify code to send markdown message.

* fix wechat issues: Change the language pack of agentId to agentId/chatId.

* fix format

* fix param name

Co-authored-by: Amy <amywang0104@163.com>
3.0.0/version-upgrade
Tq 3 years ago committed by GitHub
parent
commit
c294979e2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      dolphinscheduler-alert/dolphinscheduler-alert-api/src/main/java/org/apache/dolphinscheduler/alert/api/ShowType.java
  2. 10
      dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertChannelFactory.java
  3. 5
      dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertParamsConstants.java
  4. 74
      dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatSender.java
  5. 19
      dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WechatAppChatMessage.java
  6. 25
      dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WechatAppMessage.java
  7. 2
      dolphinscheduler-ui-next/src/locales/modules/en_US.ts
  8. 2
      dolphinscheduler-ui-next/src/locales/modules/zh_CN.ts

4
dolphinscheduler-alert/dolphinscheduler-alert-api/src/main/java/org/apache/dolphinscheduler/alert/api/ShowType.java

@ -25,11 +25,13 @@ public enum ShowType {
* 1 TEXT; * 1 TEXT;
* 2 attachment; * 2 attachment;
* 3 TABLE+attachment; * 3 TABLE+attachment;
* 4 MARKDOWN;
*/ */
TABLE(0, "table"), TABLE(0, "table"),
TEXT(1, "text"), TEXT(1, "text"),
ATTACHMENT(2, "attachment"), ATTACHMENT(2, "attachment"),
TABLE_ATTACHMENT(3, "table attachment"); TABLE_ATTACHMENT(3, "table attachment"),
MARKDOWN(4, "markdown"),;
private final int code; private final int code;
private final String descp; private final String descp;

10
dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertChannelFactory.java

@ -56,14 +56,14 @@ public final class WeChatAlertChannelFactory implements AlertChannelFactory {
.build(); .build();
InputParam usersParam = InputParam.newBuilder(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_USERS, WeChatAlertParamsConstants.ENTERPRISE_WE_CHAT_USERS) InputParam usersParam = InputParam.newBuilder(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_USERS, WeChatAlertParamsConstants.ENTERPRISE_WE_CHAT_USERS)
.setPlaceholder("please input users ") .setPlaceholder("use `|` to separate userIds and `@all` to everyone ")
.addValidate(Validate.newBuilder() .addValidate(Validate.newBuilder()
.setRequired(true) .setRequired(false)
.build()) .build())
.build(); .build();
InputParam agentIdParam = InputParam.newBuilder(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_AGENT_ID, WeChatAlertParamsConstants.ENTERPRISE_WE_CHAT_AGENT_ID) InputParam agentIdParam = InputParam.newBuilder(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_AGENT_ID, WeChatAlertParamsConstants.ENTERPRISE_WE_CHAT_AGENT_ID)
.setPlaceholder("please input agent id ") .setPlaceholder("please input agent id or chat id ")
.addValidate(Validate.newBuilder() .addValidate(Validate.newBuilder()
.setRequired(true) .setRequired(true)
.build()) .build())
@ -77,9 +77,9 @@ public final class WeChatAlertChannelFactory implements AlertChannelFactory {
.build(); .build();
RadioParam showType = RadioParam.newBuilder(AlertConstants.NAME_SHOW_TYPE, AlertConstants.SHOW_TYPE) RadioParam showType = RadioParam.newBuilder(AlertConstants.NAME_SHOW_TYPE, AlertConstants.SHOW_TYPE)
.addParamsOptions(new ParamsOptions(ShowType.TABLE.getDescp(), ShowType.TABLE.getDescp(), false)) .addParamsOptions(new ParamsOptions(ShowType.MARKDOWN.getDescp(), ShowType.MARKDOWN.getDescp(), false))
.addParamsOptions(new ParamsOptions(ShowType.TEXT.getDescp(), ShowType.TEXT.getDescp(), false)) .addParamsOptions(new ParamsOptions(ShowType.TEXT.getDescp(), ShowType.TEXT.getDescp(), false))
.setValue(ShowType.TABLE.getDescp()) .setValue(ShowType.MARKDOWN.getDescp())
.addValidate(Validate.newBuilder().setRequired(true).build()) .addValidate(Validate.newBuilder().setRequired(true).build())
.build(); .build();

5
dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertParamsConstants.java

@ -24,9 +24,8 @@ public final class WeChatAlertParamsConstants {
static final String NAME_ENTERPRISE_WE_CHAT_SECRET = "secret"; static final String NAME_ENTERPRISE_WE_CHAT_SECRET = "secret";
static final String ENTERPRISE_WE_CHAT_TEAM_SEND_MSG = "$t('teamSendMsg')"; 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 NAME_ENTERPRISE_WE_CHAT_TEAM_SEND_MSG = "teamSendMsg";
static final String ENTERPRISE_WE_CHAT_AGENT_ID = "$t('agentId')"; static final String ENTERPRISE_WE_CHAT_AGENT_ID = "$t('agentId/chatId')";
static final String NAME_ENTERPRISE_WE_CHAT_AGENT_ID = "agentId"; static final String NAME_ENTERPRISE_WE_CHAT_AGENT_ID = "agentId/chatId";
static final String NAME_ENTERPRISE_WE_CHAT_CHAT_ID = "chatId";
static final String ENTERPRISE_WE_CHAT_USERS = "$t('users')"; static final String ENTERPRISE_WE_CHAT_USERS = "$t('users')";
static final String NAME_ENTERPRISE_WE_CHAT_USERS = "users"; static final String NAME_ENTERPRISE_WE_CHAT_USERS = "users";

74
dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatSender.java

@ -17,15 +17,10 @@
package org.apache.dolphinscheduler.plugin.alert.wechat; 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.AlertConstants;
import org.apache.dolphinscheduler.alert.api.AlertResult; import org.apache.dolphinscheduler.alert.api.AlertResult;
import org.apache.dolphinscheduler.alert.api.ShowType;
import org.apache.dolphinscheduler.spi.utils.JSONUtils; import org.apache.dolphinscheduler.spi.utils.JSONUtils;
import org.apache.dolphinscheduler.spi.utils.StringUtils; import org.apache.dolphinscheduler.spi.utils.StringUtils;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
@ -34,18 +29,20 @@ import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import static java.util.Objects.requireNonNull;
import static org.apache.dolphinscheduler.plugin.alert.wechat.WeChatAlertConstants.*;
public final class WeChatSender { public final class WeChatSender {
private static final Logger logger = LoggerFactory.getLogger(WeChatSender.class); private static final Logger logger = LoggerFactory.getLogger(WeChatSender.class);
@ -57,8 +54,7 @@ public final class WeChatSender {
private static final String CORP_ID_REGEX = "{corpId}"; private static final String CORP_ID_REGEX = "{corpId}";
private static final String SECRET_REGEX = "{secret}"; private static final String SECRET_REGEX = "{secret}";
private static final String TOKEN_REGEX = "{token}"; private static final String TOKEN_REGEX = "{token}";
private final String weChatAgentId; private final String weChatAgentIdChatId;
private final String weChatChatId;
private final String weChatUsers; private final String weChatUsers;
private final String weChatTokenUrlReplace; private final String weChatTokenUrlReplace;
private final String weChatToken; private final String weChatToken;
@ -66,8 +62,7 @@ public final class WeChatSender {
private final String showType; private final String showType;
WeChatSender(Map<String, String> config) { WeChatSender(Map<String, String> config) {
weChatAgentId = config.get(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_AGENT_ID); weChatAgentIdChatId = 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); weChatUsers = config.get(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_USERS);
String weChatCorpId = config.get(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_CORP_ID); String weChatCorpId = config.get(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_CORP_ID);
String weChatSecret = config.get(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_SECRET); String weChatSecret = config.get(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_SECRET);
@ -100,38 +95,6 @@ public final class WeChatSender {
} }
} }
/**
* convert table to markdown style
*
* @param title the title
* @param content the content
* @return markdown table content
*/
private static String markdownTable(String title, String content) {
List<LinkedHashMap> mapItemsList = JSONUtils.toList(content, LinkedHashMap.class);
if (null == mapItemsList || mapItemsList.isEmpty()) {
logger.error("itemsList is null");
throw new RuntimeException("itemsList is null");
}
StringBuilder contents = new StringBuilder(200);
for (LinkedHashMap mapItems : mapItemsList) {
Set<Entry<String, Object>> entries = mapItems.entrySet();
Iterator<Entry<String, Object>> iterator = entries.iterator();
StringBuilder t = new StringBuilder(String.format("`%s`%s", title, WeChatAlertConstants.MARKDOWN_ENTER));
while (iterator.hasNext()) {
Map.Entry<String, Object> entry = iterator.next();
t.append(WeChatAlertConstants.MARKDOWN_QUOTE);
t.append(entry.getKey()).append(":").append(entry.getValue());
t.append(WeChatAlertConstants.MARKDOWN_ENTER);
}
contents.append(t);
}
return contents.toString();
}
/** /**
* convert text to markdown style * convert text to markdown style
* *
@ -242,17 +205,17 @@ public final class WeChatSender {
return alertResult; return alertResult;
} }
String enterpriseWeChatPushUrlReplace = ""; String enterpriseWeChatPushUrlReplace = "";
Map<String,String> contentMap=new HashMap<>(); Map<String, String> contentMap = new HashMap<>();
contentMap.put(WeChatAlertConstants.WE_CHAT_CONTENT_KEY,data); contentMap.put(WeChatAlertConstants.WE_CHAT_CONTENT_KEY, data);
String msgJson=""; String msgJson = "";
if (sendType.equals(WeChatType.APP.getDescp())) { if (sendType.equals(WeChatType.APP.getDescp())) {
enterpriseWeChatPushUrlReplace = WeChatAlertConstants.WE_CHAT_PUSH_URL.replace(TOKEN_REGEX, weChatToken); 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); WechatAppMessage wechatAppMessage = new WechatAppMessage(weChatUsers, showType, Integer.valueOf(weChatAgentIdChatId), contentMap, WE_CHAT_MESSAGE_SAFE_PUBLICITY, WE_CHAT_ENABLE_ID_TRANS, WE_CHAT_DUPLICATE_CHECK_INTERVAL_ZERO);
msgJson=JSONUtils.toJsonString(wechatAppMessage); msgJson = JSONUtils.toJsonString(wechatAppMessage);
} else if (sendType.equals(WeChatType.APPCHAT.getDescp())) { } else if (sendType.equals(WeChatType.APPCHAT.getDescp())) {
enterpriseWeChatPushUrlReplace = WeChatAlertConstants.WE_CHAT_APP_CHAT_PUSH_URL.replace(TOKEN_REGEX, weChatToken); 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); WechatAppChatMessage wechatAppChatMessage = new WechatAppChatMessage(weChatAgentIdChatId, showType, contentMap, WE_CHAT_MESSAGE_SAFE_PUBLICITY);
msgJson=JSONUtils.toJsonString(wechatAppChatMessage); msgJson = JSONUtils.toJsonString(wechatAppChatMessage);
} }
try { try {
@ -272,14 +235,7 @@ public final class WeChatSender {
* @return the markdown alert table/text * @return the markdown alert table/text
*/ */
private String markdownByAlert(String title, String content) { private String markdownByAlert(String title, String content) {
String result = ""; return markdownText(title, content);
if (showType.equals(ShowType.TABLE.getDescp())) {
result = markdownTable(title, content);
} else if (showType.equals(ShowType.TEXT.getDescp())) {
result = markdownText(title, content);
}
return result;
} }
private String getToken() { private String getToken() {

19
dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WechatAppChatMessage.java

@ -17,6 +17,8 @@
package org.apache.dolphinscheduler.plugin.alert.wechat; package org.apache.dolphinscheduler.plugin.alert.wechat;
import org.apache.dolphinscheduler.alert.api.ShowType;
import java.util.Map; import java.util.Map;
public class WechatAppChatMessage { public class WechatAppChatMessage {
@ -24,6 +26,7 @@ public class WechatAppChatMessage {
private String chatid; private String chatid;
private String msgtype; private String msgtype;
private Map<String,String> text; private Map<String,String> text;
private Map<String,String> markdown;
private Integer safe; private Integer safe;
public String getChatid() { public String getChatid() {
@ -58,13 +61,25 @@ public class WechatAppChatMessage {
this.safe = safe; this.safe = safe;
} }
public Map<String, String> getMarkdown() {
return markdown;
}
public void setMarkdown(Map<String, String> markdown) {
this.markdown = markdown;
}
public WechatAppChatMessage() { public WechatAppChatMessage() {
} }
public WechatAppChatMessage(String chatid, String msgtype, Map<String, String> text, Integer safe) { public WechatAppChatMessage(String chatid, String msgtype, Map<String, String> contentMap, Integer safe) {
this.chatid = chatid; this.chatid = chatid;
this.msgtype = msgtype; this.msgtype = msgtype;
this.text = text; if (msgtype.equals(ShowType.MARKDOWN.getDescp())) {
this.markdown = contentMap;
} else {
this.text = contentMap;
}
this.safe = safe; this.safe = safe;
} }
} }

25
dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WechatAppMessage.java

@ -17,6 +17,8 @@
package org.apache.dolphinscheduler.plugin.alert.wechat; package org.apache.dolphinscheduler.plugin.alert.wechat;
import org.apache.dolphinscheduler.alert.api.ShowType;
import java.util.Map; import java.util.Map;
public class WechatAppMessage { public class WechatAppMessage {
@ -24,7 +26,8 @@ public class WechatAppMessage {
private String touser; private String touser;
private String msgtype; private String msgtype;
private Integer agentid; private Integer agentid;
private Map<String,String> text; private Map<String, String> text;
private Map<String, String> markdown;
private Integer safe; private Integer safe;
private Integer enable_id_trans; private Integer enable_id_trans;
private Integer enable_duplicate_check; private Integer enable_duplicate_check;
@ -85,16 +88,28 @@ public class WechatAppMessage {
this.enable_duplicate_check = enable_duplicate_check; this.enable_duplicate_check = enable_duplicate_check;
} }
public Map<String, String> getMarkdown() {
return markdown;
}
public void setMarkdown(Map<String, String> markdown) {
this.markdown = markdown;
}
public WechatAppMessage() { public WechatAppMessage() {
} }
public WechatAppMessage(String touser, String msgtype, Integer agentid, Map<String, String> text, Integer safe, Integer enable_id_trans, Integer enable_duplicate_check) { public WechatAppMessage(String touser, String msgtype, Integer agentid, Map<String, String> contentMap, Integer safe, Integer enableIdTrans, Integer enableDuplicateCheck) {
this.touser = touser; this.touser = touser;
this.msgtype = msgtype; this.msgtype = msgtype;
this.agentid = agentid; this.agentid = agentid;
this.text = text; if (msgtype.equals(ShowType.MARKDOWN.getDescp())) {
this.markdown = contentMap;
} else {
this.text = contentMap;
}
this.safe = safe; this.safe = safe;
this.enable_id_trans = enable_id_trans; this.enable_id_trans = enableIdTrans;
this.enable_duplicate_check = enable_duplicate_check; this.enable_duplicate_check = enableDuplicateCheck;
} }
} }

2
dolphinscheduler-ui-next/src/locales/modules/en_US.ts

@ -1112,7 +1112,7 @@ const security = {
Secret: 'Secret', Secret: 'Secret',
users: 'Users', users: 'Users',
userSendMsg: 'UserSendMsg', userSendMsg: 'UserSendMsg',
agentId: 'AgentId', 'agentId/chatId': 'AgentId or ChatId',
showType: 'Show Type', showType: 'Show Type',
receivers: 'Receivers', receivers: 'Receivers',
receiverCcs: 'ReceiverCcs', receiverCcs: 'ReceiverCcs',

2
dolphinscheduler-ui-next/src/locales/modules/zh_CN.ts

@ -1097,7 +1097,7 @@ const security = {
Secret: '密钥', Secret: '密钥',
users: '群员', users: '群员',
userSendMsg: '群员信息', userSendMsg: '群员信息',
agentId: '应用ID', 'agentId/chatId': '应用ID或群聊ID',
showType: '内容展示类型', showType: '内容展示类型',
receivers: '收件人', receivers: '收件人',
receiverCcs: '抄送人', receiverCcs: '抄送人',

Loading…
Cancel
Save