1. 程式人生 > >企業微信簡單實現消息推送

企業微信簡單實現消息推送

message util 用戶 service auth _id tpc sea agen

廢話不多說,上來就堆代碼..........

感覺挺簡單,就不過多解釋,應該一看就懂.....

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

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.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

@Service
public class SendMsg { private CloseableHttpClient httpClient; private HttpPost httpPost;//用於提交登陸數據 private HttpGet httpGet;//用於獲得登錄後的頁面 //CorpID 企業ID //AGENTID 應用的ID //Secret 應用的ID對應的密鑰 public static final String CONTENT_TYPE = "Content-Type"; public static final Integer AGENTID = 1000002
; public static final String CORPID = "************"; public static final String CORPSECRET = "*******************"; SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// private static Gson gson = new Gson(); /** * * @param toUser 用戶的ID 格式"UserID1|UserID2|UserID3" * @param contentValue 推送消息內容 * @throws IOException
*/ public void sendTextMesg(String toUser,String contentValue) throws IOException { String token = getToken(CORPID,CORPSECRET); String postData = createPostData(toUser, "text",AGENTID, "content", contentValue); String response = post("utf-8", CONTENT_TYPE,"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=", postData, token); System.out.println("獲取到的token======>" + token); System.out.println("請求數據======>" + postData); System.out.println("發送微信的響應數據======>" + response); } public String getToken(String corpId, String corpSecret) throws IOException { String resp = toAuth( "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpId + "&corpsecret=" + corpSecret);//拼接字符串得到url Map<String, Object> map = gson.fromJson(resp, new TypeToken<Map<String, Object>>() { }.getType()); System.out.println(map); return map.get("access_token").toString(); } protected String toAuth(String Get_Token_Url) throws IOException { httpClient = HttpClients.createDefault(); httpGet = new HttpGet(Get_Token_Url); CloseableHttpResponse response = httpClient.execute(httpGet); System.out.println(response.toString()); String resp; try { HttpEntity entity = response.getEntity(); System.out.println(response.getAllHeaders()); resp = EntityUtils.toString(entity, "utf-8"); EntityUtils.consume(entity); } finally { response.close(); } return resp; } private String createPostData(String touser, String msgtype, int agent_id, String contentKey, String contentValue) { Map<String,Object> weChatData = new HashMap<>(); weChatData.put("touser",touser); weChatData.put("agentid",agent_id); weChatData.put("msgtype",msgtype); Map<Object, Object> content = new HashMap<Object, Object>(); content.put(contentKey, contentValue + "\n--------\n" + df.format(new Date())); weChatData.put("text",content); System.out.println(gson.toJson(weChatData)); return gson.toJson(weChatData); } private 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(); } return resp; } }

有點忙,相關jar包看API,好像有。。。。。。。。。。

企業微信簡單實現消息推送