上個月比較忙,等不忙了繼續寫點基礎教程(五一還在高鐵上寫項目在)。因為公司的原因,自己學習了點JavaWeb的知識,重新寫了一個簡單的后台管理,用於記錄用戶注冊信息的。其中有這樣的一個要求,就是在用戶注冊完成之后,能發送一個提示信息,當時我第一個想法是用qq做消息提醒,但是網上找了半天,發現企鵝把相關的接口給關了,然后繼續搜索發現了可以用企業微信,但是網上的一些教程不算很詳細,自己還是琢磨了半天,然后今天整理一下發給大家。
首先是准備工作,幾個jar包:
數據庫和servlet看個人所需。沒有的話網上搜索一下。幾個相關的java文件和對應的代碼
public class SendWX { //發送消息的執行方法 public void send(String tel, String sec) { WeChatMsgSend swx = new WeChatMsgSend(); try {
//這里的token獲取待會會說從哪兒具體得到 String token = swx.getToken("wqd51b29a3fb154c92", "KWSGMIpqSmJ_wY8ettuAWafhfAdfTUKN3OParcIfaaY"); String postdata = swx.createpostdata("ErShiYi", "text", 1000002, "content", "手機號:" + tel + "\n內容:" + sec); String resp = swx.post("utf-8", WeChatMsgSend.CONTENT_TYPE, (new WeChatUrlData()).getSendMessage_Url(), postdata, token); System.out.println("獲取到的token======>" + token); System.out.println("請求數據======>" + postdata); System.out.println("發送微信的響應數據======>" + resp); } catch (Exception e) { e.getStackTrace(); } } }
/** * 微信消息發送實體類 * @author PC-MXF * */ public class WeChatData { //發送微信消息的URLString sendMsgUrl="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="; /** * 成員賬號 */ private String touser; /** * 消息類型 */ private String msgtype; /** * 企業用用的agentid */ private String agentid; /** * 十幾接收map類型數據 */ private Object text; 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 String getAgentid() { return agentid; } public void setAgentid(String agentid) { this.agentid = agentid; } public Object getText() { return text; } public void setText(Object text) { this.text = text; } }
/** * 微信發送消息 * * @author PC-MXF * */ public class WeChatMsgSend { private CloseableHttpClient httpClient; /** * 用於提交登錄數據 */ private HttpPost httpPost; /** * 用於獲得登陸后頁面 */ private HttpGet httpGet; public static final String CONTENT_TYPE = "Content-Type"; SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private static Gson gson = new Gson(); /** * 微信授權請求,GET類型,獲取授權響應,用於其他方法截取token * * @param Get_Token_Url * @return String 授權響應內容 * @throws IOException */ protected String toAuth(String Get_Token_Url) throws IOException { httpClient = HttpClients.createDefault(); httpGet = new HttpGet(Get_Token_Url); CloseableHttpResponse response = httpClient.execute(httpGet); String resp = ""; try { HttpEntity entity = response.getEntity(); resp = EntityUtils.toString(entity, "utf-8"); EntityUtils.consume(entity); } catch (Exception e) { e.getStackTrace(); } finally { response.close(); } LoggerFactory.getLogger(getClass()).info(" resp:{}", resp); return resp; } /** * corpid應用組織編號 corpsecret應用秘鑰 獲取toAuth(String * Get_Token_Url)返回結果中鍵值對中access_token鍵的值 * * @param */ public String getToken(String corpid, String corpsecret) throws IOException { WeChatMsgSend sw = new WeChatMsgSend(); WeChatUrlData uData = new WeChatUrlData(); uData.setGet_Token_Url(corpid, corpsecret); String resp = sw.toAuth(uData.getGet_Token_Url()); System.out.println("resp=====:" + resp); try { Map<String, Object> map = gson.fromJson(resp, new TypeToken<Map<String, Object>>() { }.getType()); return map.get("access_token").toString(); } catch (Exception e) { e.getStackTrace(); return resp; } } /** * 創建微信發送請求post數據 touser發送消息接收者 ,msgtype消息類型(文本/圖片等), application_id應用編號。 * 本方法適用於text型微信消息,contentKey和contentValue只能組一對 * * @param touser * @param msgtype * @param application_id * @param contentKey * @param contentValue * @return */ public String createpostdata(String touser, String msgtype, int application_id, String contentKey, String contentValue) { WeChatData wcd = new WeChatData(); wcd.setTouser(touser); wcd.setAgentid(application_id + ""); wcd.setMsgtype(msgtype); Map<Object, Object> content = new HashMap<Object, Object>(); content.put(contentKey, contentValue); wcd.setText(content); return gson.toJson(wcd); } /** * @Title 創建微信發送請求post實體,charset消息編碼 ,contentType消息體內容類型, * url微信消息發送請求地址,data為post數據,token鑒權token * @param charset * @param contentType * @param url * @param data * @param token * @return * @throws IOException */ public String post(String charset, String contentType, String url, String data, String token) throws IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); httpPost = new HttpPost(url + token); httpPost.setHeader(CONTENT_TYPE, contentType); 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(); } LoggerFactory.getLogger(getClass()).info("call [{}], param:{}, resp:{}", url, data, resp); return resp; } }
/** * 微信授權請求 * @author PC-MXF * */ public class WeChatUrlData { /** * 企業Id */ private String corpid; /** * secret管理組的憑證密鑰 */ private String corpsecret; /** * 獲取ToKen的請求 */ private String Get_Token_Url; /** * 發送消息的請求 */ private String SendMessage_Url; public String getCorpid() { return corpid; } public void setCorpid(String corpid) { this.corpid = corpid; } public String getCorpsecret() { return corpsecret; } public void setCorpsecret(String corpsecret) { this.corpsecret = corpsecret; } public String getGet_Token_Url() { return Get_Token_Url; } public void setGet_Token_Url(String corpid,String corpsecret) { Get_Token_Url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+corpid+"&corpsecret="+corpsecret; } public String getSendMessage_Url() { SendMessage_Url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="; return SendMessage_Url; } public void setSendMessage_Url(String sendMessage_Url) { SendMessage_Url = sendMessage_Url; } }
相關的代碼准備完成之后,開始創建企業微信,打開企業微信官網:https://work.weixin.qq.com/注冊,並登陸。點擊應用與小程序,自建里面創建應用:
然后進入自己創建的應用,找到這兩個信息,
分別對應的是
然后打開我的企業最下面有個企業ID
對應的是上面的getToken()第一個參數。下面的swx.createpostdata方法的第一個參數是用戶名稱,打開通訊錄:
點進去你想要給他發送消息的公司成員:
這個賬號即為用戶的名稱,修改為自己想要的。就可以發送文本信息。還有其他的相關的功能,可以自己去查看一下企業微信API尋找,比如發送圖片等。