diff --git a/escheduler-alert/pom.xml b/escheduler-alert/pom.xml index f066bc4b727247b9519dc8d9cbc34143a6bb6e83..cb7d2db4bc578a99fb9dbff65c79d3229658dac5 100644 --- a/escheduler-alert/pom.xml +++ b/escheduler-alert/pom.xml @@ -102,6 +102,17 @@ escheduler-dao + + org.apache.httpcomponents + httpcore + 4.4.6 + + + org.apache.httpcomponents + httpclient + 4.5.5 + + diff --git a/escheduler-alert/src/main/java/cn/escheduler/alert/utils/Constants.java b/escheduler-alert/src/main/java/cn/escheduler/alert/utils/Constants.java index 07d1866a5df5a33b7797977e4889534760a842d7..cc2530826f1b8a9274080a808bc661462ba26b4d 100644 --- a/escheduler-alert/src/main/java/cn/escheduler/alert/utils/Constants.java +++ b/escheduler-alert/src/main/java/cn/escheduler/alert/utils/Constants.java @@ -126,4 +126,16 @@ public class Constants { public static final String TH_END = ""; public static final int ALERT_SCAN_INTERVEL = 5000; + + public static final String QIYE_WEIXIN_CORP_ID = "qiye.weixin.corp.id"; + + public static final String QIYE_WEIXIN_SECRET = "qiye.weixin.secret"; + + public static final String QIYE_WEIXIN_TOKEN_URL = "qiye.weixin.token.url"; + + public static final String QIYE_WEIXIN_PUSH_URL = "qiye.weixin.push.url"; + + public static final String QIYE_WEIXIN_TEAM_SEND_MSG = "qiye.weixin.team.send.msg"; + + public static final String QIYE_WEIXIN_USER_SEND_MSG = "qiye.weixin.user.send.msg"; } diff --git a/escheduler-alert/src/main/java/cn/escheduler/alert/utils/FuncUtils.java b/escheduler-alert/src/main/java/cn/escheduler/alert/utils/FuncUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..c973e1de9bb29eabc5c9106095b10d757eaca485 --- /dev/null +++ b/escheduler-alert/src/main/java/cn/escheduler/alert/utils/FuncUtils.java @@ -0,0 +1,18 @@ +package cn.escheduler.alert.utils; + +public class FuncUtils { + + static public String mkString(Iterable list, String split) { + StringBuilder sb = new StringBuilder(); + boolean first = true; + for (String item : list) { + if (first) + first = false; + else + sb.append(split); + sb.append(item); + } + return sb.toString(); + } + +} diff --git a/escheduler-alert/src/main/java/cn/escheduler/alert/utils/QiyeWeixinUtils.java b/escheduler-alert/src/main/java/cn/escheduler/alert/utils/QiyeWeixinUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..5af6baa4fcb82312a9bdb9c677b201f7e8f4c214 --- /dev/null +++ b/escheduler-alert/src/main/java/cn/escheduler/alert/utils/QiyeWeixinUtils.java @@ -0,0 +1,167 @@ +/* + * 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 cn.escheduler.alert.utils; + +import com.alibaba.fastjson.JSON; + +import com.google.common.reflect.TypeToken; +import org.apache.http.HttpEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +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.Collection; +import java.util.Map; + +import static cn.escheduler.alert.utils.PropertyUtils.getString; + +/** + * qiye weixin utils + */ +public class QiyeWeixinUtils { + + public static final Logger logger = LoggerFactory.getLogger(QiyeWeixinUtils.class); + + private static final String qiyeWeixinCorpId = getString(Constants.QIYE_WEIXIN_CORP_ID); + + private static final String qiyeWeixinSecret = getString(Constants.QIYE_WEIXIN_SECRET); + + private static final String qiyeWeixinTokenUrl = getString(Constants.QIYE_WEIXIN_TOKEN_URL); + private String qiyeWeixinTokenUrlReplace = qiyeWeixinTokenUrl + .replaceAll("\\$weixinCorpId", qiyeWeixinCorpId) + .replaceAll("\\$weixinSecret", qiyeWeixinSecret); + + private static final String qiyeWeixinPushUrl = getString(Constants.QIYE_WEIXIN_PUSH_URL); + + private static final String qiyeWeixinTeamSendMsg = getString(Constants.QIYE_WEIXIN_TEAM_SEND_MSG); + + private static final String qiyeWeixinUserSendMsg = getString(Constants.QIYE_WEIXIN_USER_SEND_MSG); + + /** + * get winxin token info + * @return token string info + * @throws IOException + */ + public String getToken() throws IOException { + String resp; + + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpGet httpGet = new HttpGet(qiyeWeixinTokenUrlReplace); + CloseableHttpResponse response = httpClient.execute(httpGet); + try { + HttpEntity entity = response.getEntity(); + resp = EntityUtils.toString(entity, "utf-8"); + EntityUtils.consume(entity); + } finally { + response.close(); + } + + Map map = JSON.parseObject(resp, + new TypeToken>() { + }.getType()); + return map.get("access_token").toString(); + } + + /** + * make team single weixin message + * @param toParty + * @param agentId + * @param msg + * @return weixin send message + */ + public String makeTeamSendMsg(String toParty, String agentId, String msg) { + return qiyeWeixinTeamSendMsg.replaceAll("\\$toParty", toParty) + .replaceAll("\\$agentId", agentId) + .replaceAll("\\$msg", msg); + } + + /** + * make team multi weixin message + * @param toParty + * @param agentId + * @param msg + * @return weixin send message + */ + public String makeTeamSendMsg(Collection toParty, String agentId, String msg) { + String listParty = FuncUtils.mkString(toParty, "|"); + return qiyeWeixinTeamSendMsg.replaceAll("\\$toParty", listParty) + .replaceAll("\\$agentId", agentId) + .replaceAll("\\$msg", msg); + } + + /** + * make team single user message + * @param toUser + * @param agentId + * @param msg + * @return weixin send message + */ + public String makeUserSendMsg(String toUser, String agentId, String msg) { + return qiyeWeixinUserSendMsg.replaceAll("\\$toUser", toUser) + .replaceAll("\\$agentId", agentId) + .replaceAll("\\$msg", msg); + } + + /** + * make team multi user message + * @param toUser + * @param agentId + * @param msg + * @return weixin send message + */ + public String makeUserSendMsg(Collection toUser, String agentId, String msg) { + String listUser = FuncUtils.mkString(toUser, "|"); + return qiyeWeixinUserSendMsg.replaceAll("\\$toUser", listUser) + .replaceAll("\\$agentId", agentId) + .replaceAll("\\$msg", msg); + } + + /** + * send weixin + * @param charset + * @param data + * @param token + * @return weixin resp, demo: {"errcode":0,"errmsg":"ok","invaliduser":""} + * @throws IOException + */ + public String sendQiyeWeixin(String charset, String data, String token) throws IOException { + String qiyeWeixinPushUrlReplace = qiyeWeixinPushUrl.replaceAll("\\$weixinToken", token); + + CloseableHttpClient httpclient = HttpClients.createDefault(); + HttpPost httpPost = new HttpPost(qiyeWeixinPushUrlReplace); + httpPost.setEntity(new StringEntity(data, charset)); + CloseableHttpResponse response = httpclient.execute(httpPost); + String resp; + try { + HttpEntity entity = response.getEntity(); + resp = EntityUtils.toString(entity, charset); + EntityUtils.consume(entity); + } finally { + response.close(); + } + logger.info("qiye weixin send [{}], param:{}, resp:{}", qiyeWeixinPushUrl, data, resp); + return resp; + } + +} diff --git a/escheduler-alert/src/main/resources/alert.properties b/escheduler-alert/src/main/resources/alert.properties index e2cba1160deaf9a9470cac1e3437d6bb19b18017..31265c2200c2ca80add04cc35a810bbc2c82209a 100644 --- a/escheduler-alert/src/main/resources/alert.properties +++ b/escheduler-alert/src/main/resources/alert.properties @@ -11,6 +11,13 @@ mail.passwd=xxxxxxx #xls file path,need create if not exist xls.file.path=/opt/xls +# qiye weixin configuration +qiye.weixin.corp.id=xxxxxxx +qiye.weixin.secret=xxxxxxx +qiye.weixin.token.url=https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$weixinCorpId&corpsecret=$weixinSecret +qiye.weixin.push.url=https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$weixinToken +qiye.weixin.team.send.msg={\"toparty\":\"$toParty\",\"agentid\":\"$agentId\",\"msgtype\":\"text\",\"text\":{\"content\":\"$msg\"},\"safe\":\"0\"} +qiye.weixin.user.send.msg={\"touser\":\"$toUser\",\"agentid\":\"$agentId\",\"msgtype\":\"text\",\"text\":{\"content\":\"$msg\"},\"safe\":\"0\"} diff --git a/escheduler-alert/src/test/java/cn/escheduler/alert/utils/QiyeWeixinUtilsTest.java b/escheduler-alert/src/test/java/cn/escheduler/alert/utils/QiyeWeixinUtilsTest.java new file mode 100644 index 0000000000000000000000000000000000000000..051f3bccd1ff091db697065373c74ad0675dfd44 --- /dev/null +++ b/escheduler-alert/src/test/java/cn/escheduler/alert/utils/QiyeWeixinUtilsTest.java @@ -0,0 +1,110 @@ +/* + * 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 cn.escheduler.alert.utils; + +import org.junit.Assert; +import org.junit.Test; + +import com.alibaba.fastjson.JSON; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Collection; + +/** + * Please manually modify the configuration file before testing. + * file: alert.properties + * qiye.weixin.corp.id + * qiye.weixin.secret + * qiye.weixin.token.url + * qiye.weixin.push.url + * qiye.weixin.send.msg + */ +public class QiyeWeixinUtilsTest { + + // Please change + private String agentId = "1000002"; // app id + private String partyId = "2"; + private Collection listPartyId = Arrays.asList("2","4"); + private String userId = "test1"; + private Collection listUserId = Arrays.asList("test1","test2"); + + @Test + public void testSendSingleTeamWeixin() { + QiyeWeixinUtils wx = new QiyeWeixinUtils(); + + try { + String token = wx.getToken(); + String msg = wx.makeTeamSendMsg(partyId, agentId, "hello world"); + String resp = wx.sendQiyeWeixin("utf-8", msg, token); + + String errmsg = JSON.parseObject(resp).getString("errmsg"); + Assert.assertEquals(errmsg, "ok"); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void testSendMultiTeamWeixin() { + QiyeWeixinUtils wx = new QiyeWeixinUtils(); + + try { + String token = wx.getToken(); + String msg = wx.makeTeamSendMsg(listPartyId, agentId, "hello world"); + String resp = wx.sendQiyeWeixin("utf-8", msg, token); + + String errmsg = JSON.parseObject(resp).getString("errmsg"); + Assert.assertEquals(errmsg, "ok"); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void testSendSingleUserWeixin() { + QiyeWeixinUtils wx = new QiyeWeixinUtils(); + + try { + String token = wx.getToken(); + String msg = wx.makeUserSendMsg(userId, agentId, "hello world"); + String resp = wx.sendQiyeWeixin("utf-8", msg, token); + + String errmsg = JSON.parseObject(resp).getString("errmsg"); + Assert.assertEquals(errmsg, "ok"); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void testSendMultiUserWeixin() { + QiyeWeixinUtils wx = new QiyeWeixinUtils(); + + try { + String token = wx.getToken(); + String msg = wx.makeUserSendMsg(listUserId, agentId, "hello world"); + String resp = wx.sendQiyeWeixin("utf-8", msg, token); + + String errmsg = JSON.parseObject(resp).getString("errmsg"); + Assert.assertEquals(errmsg, "ok"); + } catch (IOException e) { + e.printStackTrace(); + } + } + +}