This is an automated email from the ASF dual-hosted git repository.

leonbao pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/dolphinscheduler.git


The following commit(s) were added to refs/heads/dev by this push:
     new c294979e2f [Bug-9235][Alert]Fix wechat markdown message and change 
wechat form structure (#9367)
c294979e2f is described below

commit c294979e2fc96e876f5699bc3164accbfa5c1023
Author: Tq <[email protected]>
AuthorDate: Wed Apr 6 20:29:30 2022 +0800

    [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 <[email protected]>
---
 .../dolphinscheduler/alert/api/ShowType.java       |  4 +-
 .../alert/wechat/WeChatAlertChannelFactory.java    | 10 +--
 .../alert/wechat/WeChatAlertParamsConstants.java   |  5 +-
 .../plugin/alert/wechat/WeChatSender.java          | 80 +++++-----------------
 .../plugin/alert/wechat/WechatAppChatMessage.java  | 19 ++++-
 .../plugin/alert/wechat/WechatAppMessage.java      | 25 +++++--
 .../src/locales/modules/en_US.ts                   |  2 +-
 .../src/locales/modules/zh_CN.ts                   |  2 +-
 8 files changed, 67 insertions(+), 80 deletions(-)

diff --git 
a/dolphinscheduler-alert/dolphinscheduler-alert-api/src/main/java/org/apache/dolphinscheduler/alert/api/ShowType.java
 
b/dolphinscheduler-alert/dolphinscheduler-alert-api/src/main/java/org/apache/dolphinscheduler/alert/api/ShowType.java
index bb1b1b2afb..2f1b4f2b4b 100644
--- 
a/dolphinscheduler-alert/dolphinscheduler-alert-api/src/main/java/org/apache/dolphinscheduler/alert/api/ShowType.java
+++ 
b/dolphinscheduler-alert/dolphinscheduler-alert-api/src/main/java/org/apache/dolphinscheduler/alert/api/ShowType.java
@@ -25,11 +25,13 @@ public enum ShowType {
      * 1 TEXT;
      * 2 attachment;
      * 3 TABLE+attachment;
+     * 4 MARKDOWN;
      */
     TABLE(0, "table"),
     TEXT(1, "text"),
     ATTACHMENT(2, "attachment"),
-    TABLE_ATTACHMENT(3, "table attachment");
+    TABLE_ATTACHMENT(3, "table attachment"),
+    MARKDOWN(4, "markdown"),;
 
     private final int code;
     private final String descp;
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 a77daf7e45..39a4a8159f 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
@@ -56,14 +56,14 @@ public final class WeChatAlertChannelFactory implements 
AlertChannelFactory {
                                            .build();
 
         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()
-                                                               
.setRequired(true)
+                                                               
.setRequired(false)
                                                                .build())
                                           .build();
 
         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()
                                                                  
.setRequired(true)
                                                                  .build())
@@ -77,9 +77,9 @@ public final class WeChatAlertChannelFactory implements 
AlertChannelFactory {
                 .build();
 
         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))
-                                        .setValue(ShowType.TABLE.getDescp())
+                                        .setValue(ShowType.MARKDOWN.getDescp())
                                         
.addValidate(Validate.newBuilder().setRequired(true).build())
                                         .build();
 
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 db9813dab8..d8085b901f 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,9 +24,8 @@ 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_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_AGENT_ID = "$t('agentId/chatId')";
+    static final String NAME_ENTERPRISE_WE_CHAT_AGENT_ID = "agentId/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 8dfdf50986..b8ed73d587 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
@@ -17,15 +17,10 @@
 
 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;
-import org.apache.dolphinscheduler.alert.api.ShowType;
 import org.apache.dolphinscheduler.spi.utils.JSONUtils;
 import org.apache.dolphinscheduler.spi.utils.StringUtils;
-
 import org.apache.http.HttpEntity;
 import org.apache.http.client.methods.CloseableHttpResponse;
 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.HttpClients;
 import org.apache.http.util.EntityUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.io.IOException;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 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 {
     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 SECRET_REGEX = "{secret}";
     private static final String TOKEN_REGEX = "{token}";
-    private final String weChatAgentId;
-    private final String weChatChatId;
+    private final String weChatAgentIdChatId;
     private final String weChatUsers;
     private final String weChatTokenUrlReplace;
     private final String weChatToken;
@@ -66,8 +62,7 @@ public final class WeChatSender {
     private final String showType;
 
     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);
+        weChatAgentIdChatId = 
config.get(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_AGENT_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);
@@ -76,8 +71,8 @@ public final class WeChatSender {
         showType = config.get(AlertConstants.NAME_SHOW_TYPE);
         requireNonNull(showType, AlertConstants.NAME_SHOW_TYPE + 
MUST_NOT_NULL);
         weChatTokenUrlReplace = weChatTokenUrl
-            .replace(CORP_ID_REGEX, weChatCorpId)
-            .replace(SECRET_REGEX, weChatSecret);
+                .replace(CORP_ID_REGEX, weChatCorpId)
+                .replace(SECRET_REGEX, weChatSecret);
         weChatToken = getToken();
     }
 
@@ -100,42 +95,10 @@ 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
      *
-     * @param title the title
+     * @param title   the title
      * @param content the content
      * @return markdown text
      */
@@ -242,17 +205,17 @@ public final class WeChatSender {
             return alertResult;
         }
         String enterpriseWeChatPushUrlReplace = "";
-        Map<String,String> contentMap=new HashMap<>();
-        contentMap.put(WeChatAlertConstants.WE_CHAT_CONTENT_KEY,data);
-        String msgJson="";
+        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);
+            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);
         } 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);
+            WechatAppChatMessage wechatAppChatMessage = new 
WechatAppChatMessage(weChatAgentIdChatId, showType, contentMap, 
WE_CHAT_MESSAGE_SAFE_PUBLICITY);
+            msgJson = JSONUtils.toJsonString(wechatAppChatMessage);
         }
 
         try {
@@ -272,14 +235,7 @@ public final class WeChatSender {
      * @return the markdown alert table/text
      */
     private String markdownByAlert(String title, String content) {
-        String result = "";
-        if (showType.equals(ShowType.TABLE.getDescp())) {
-            result = markdownTable(title, content);
-        } else if (showType.equals(ShowType.TEXT.getDescp())) {
-            result = markdownText(title, content);
-        }
-        return result;
-
+        return markdownText(title, content);
     }
 
     private String getToken() {
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
index 5294972b3c..e04f7335b8 100644
--- 
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
@@ -17,6 +17,8 @@
 
 package org.apache.dolphinscheduler.plugin.alert.wechat;
 
+import org.apache.dolphinscheduler.alert.api.ShowType;
+
 import java.util.Map;
 
 public class WechatAppChatMessage {
@@ -24,6 +26,7 @@ public class WechatAppChatMessage {
     private String chatid;
     private String msgtype;
     private Map<String,String> text;
+    private Map<String,String> markdown;
     private Integer safe;
 
     public String getChatid() {
@@ -58,13 +61,25 @@ public class WechatAppChatMessage {
         this.safe = safe;
     }
 
+    public Map<String, String> getMarkdown() {
+        return markdown;
+    }
+
+    public void setMarkdown(Map<String, String> markdown) {
+        this.markdown = markdown;
+    }
+
     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.msgtype = msgtype;
-        this.text = text;
+        if (msgtype.equals(ShowType.MARKDOWN.getDescp())) {
+            this.markdown = contentMap;
+        } else {
+            this.text = contentMap;
+        }
         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
index 4032e4856f..dbb74a6a93 100644
--- 
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
@@ -17,6 +17,8 @@
 
 package org.apache.dolphinscheduler.plugin.alert.wechat;
 
+import org.apache.dolphinscheduler.alert.api.ShowType;
+
 import java.util.Map;
 
 public class WechatAppMessage {
@@ -24,7 +26,8 @@ public class WechatAppMessage {
     private String touser;
     private String msgtype;
     private Integer agentid;
-    private Map<String,String> text;
+    private Map<String, String> text;
+    private Map<String, String> markdown;
     private Integer safe;
     private Integer enable_id_trans;
     private Integer enable_duplicate_check;
@@ -85,16 +88,28 @@ public class WechatAppMessage {
         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(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.msgtype = msgtype;
         this.agentid = agentid;
-        this.text = text;
+        if (msgtype.equals(ShowType.MARKDOWN.getDescp())) {
+            this.markdown = contentMap;
+        } else {
+            this.text = contentMap;
+        }
         this.safe = safe;
-        this.enable_id_trans = enable_id_trans;
-        this.enable_duplicate_check = enable_duplicate_check;
+        this.enable_id_trans = enableIdTrans;
+        this.enable_duplicate_check = enableDuplicateCheck;
     }
 }
diff --git a/dolphinscheduler-ui-next/src/locales/modules/en_US.ts 
b/dolphinscheduler-ui-next/src/locales/modules/en_US.ts
index a76690effd..8faf9f9031 100644
--- a/dolphinscheduler-ui-next/src/locales/modules/en_US.ts
+++ b/dolphinscheduler-ui-next/src/locales/modules/en_US.ts
@@ -1112,7 +1112,7 @@ const security = {
     Secret: 'Secret',
     users: 'Users',
     userSendMsg: 'UserSendMsg',
-    agentId: 'AgentId',
+    'agentId/chatId': 'AgentId or ChatId',
     showType: 'Show Type',
     receivers: 'Receivers',
     receiverCcs: 'ReceiverCcs',
diff --git a/dolphinscheduler-ui-next/src/locales/modules/zh_CN.ts 
b/dolphinscheduler-ui-next/src/locales/modules/zh_CN.ts
index a435f59371..8e81fab7e7 100644
--- a/dolphinscheduler-ui-next/src/locales/modules/zh_CN.ts
+++ b/dolphinscheduler-ui-next/src/locales/modules/zh_CN.ts
@@ -1097,7 +1097,7 @@ const security = {
     Secret: '密钥',
     users: '群员',
     userSendMsg: '群员信息',
-    agentId: '应用ID',
+    'agentId/chatId': '应用ID或群聊ID',
     showType: '内容展示类型',
     receivers: '收件人',
     receiverCcs: '抄送人',

Reply via email to