huyuanming
5 years ago
10 changed files with 374 additions and 8 deletions
@ -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 EnterpriseWeChatUtils { |
||||
|
||||
public static final Logger logger = LoggerFactory.getLogger(EnterpriseWeChatUtils.class); |
||||
|
||||
private static final String enterpriseWeChatCorpId = getString(Constants.ENTERPRISE_WECHAT_CORP_ID); |
||||
|
||||
private static final String enterpriseWeChatSecret = getString(Constants.ENTERPRISE_WECHAT_SECRET); |
||||
|
||||
private static final String enterpriseWeChatTokenUrl = getString(Constants.ENTERPRISE_WECHAT_TOKEN_URL); |
||||
private String enterpriseWeChatTokenUrlReplace = enterpriseWeChatTokenUrl |
||||
.replaceAll("\\$corpId", enterpriseWeChatCorpId) |
||||
.replaceAll("\\$secret", enterpriseWeChatSecret); |
||||
|
||||
private static final String enterpriseWeChatPushUrl = getString(Constants.ENTERPRISE_WECHAT_PUSH_URL); |
||||
|
||||
private static final String enterpriseWeChatTeamSendMsg = getString(Constants.ENTERPRISE_WECHAT_TEAM_SEND_MSG); |
||||
|
||||
private static final String enterpriseWeChatUserSendMsg = getString(Constants.ENTERPRISE_WECHAT_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(enterpriseWeChatTokenUrlReplace); |
||||
CloseableHttpResponse response = httpClient.execute(httpGet); |
||||
try { |
||||
HttpEntity entity = response.getEntity(); |
||||
resp = EntityUtils.toString(entity, "utf-8"); |
||||
EntityUtils.consume(entity); |
||||
} finally { |
||||
response.close(); |
||||
} |
||||
|
||||
Map<String, Object> map = JSON.parseObject(resp, |
||||
new TypeToken<Map<String, Object>>() { |
||||
}.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 enterpriseWeChatTeamSendMsg.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<String> toParty, String agentId, String msg) { |
||||
String listParty = FuncUtils.mkString(toParty, "|"); |
||||
return enterpriseWeChatTeamSendMsg.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 enterpriseWeChatUserSendMsg.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<String> toUser, String agentId, String msg) { |
||||
String listUser = FuncUtils.mkString(toUser, "|"); |
||||
return enterpriseWeChatUserSendMsg.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 enterpriseWeChatPushUrlReplace = enterpriseWeChatPushUrl.replaceAll("\\$token", token); |
||||
|
||||
CloseableHttpClient httpclient = HttpClients.createDefault(); |
||||
HttpPost httpPost = new HttpPost(enterpriseWeChatPushUrlReplace); |
||||
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:{}", enterpriseWeChatPushUrl, data, resp); |
||||
return resp; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,34 @@
|
||||
/* |
||||
* 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; |
||||
|
||||
public class FuncUtils { |
||||
|
||||
static public String mkString(Iterable<String> 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(); |
||||
} |
||||
|
||||
} |
@ -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 |
||||
* enterprise.wechat.corp.id |
||||
* enterprise.wechat.secret |
||||
* enterprise.wechat.token.url |
||||
* enterprise.wechat.push.url |
||||
* enterprise.wechat.send.msg |
||||
*/ |
||||
public class EnterpriseWeChatUtilsTest { |
||||
|
||||
// Please change
|
||||
private String agentId = "1000002"; // app id
|
||||
private String partyId = "2"; |
||||
private Collection<String> listPartyId = Arrays.asList("2","4"); |
||||
private String userId = "test1"; |
||||
private Collection<String> listUserId = Arrays.asList("test1","test2"); |
||||
|
||||
@Test |
||||
public void testSendSingleTeamWeChat() { |
||||
EnterpriseWeChatUtils wx = new EnterpriseWeChatUtils(); |
||||
|
||||
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 testSendMultiTeamWeChat() { |
||||
EnterpriseWeChatUtils wx = new EnterpriseWeChatUtils(); |
||||
|
||||
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 testSendSingleUserWeChat() { |
||||
EnterpriseWeChatUtils wx = new EnterpriseWeChatUtils(); |
||||
|
||||
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 testSendMultiUserWeChat() { |
||||
EnterpriseWeChatUtils wx = new EnterpriseWeChatUtils(); |
||||
|
||||
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(); |
||||
} |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue