風乍起,吹皺一池春水。
微信中服務號,訂閱號可通過“消息模板”接口向成員推送指定模板消息。企業號則比較開發。
首先需要找到企業號的消息發送的接口【這個很重要】,在API中查得接口為:
public static final String SEND_MSG_URL = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=ACCESS_TOKEN";
其中,ACCESS_TOKEN 由企業號的corid與screct獲得。
代碼如下:
public class WxConstants {
/**
* 常量說明:
* 在多企業號中,最好以每個應用來定義。
*/
public static final int AGENTID = 1;
public static final String CORPID = "";
public static final String SECRET = "";
public static final String SEND_MSG_URL = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=ACCESS_TOKEN";
}
**
* 通用工具類
*
*/
public class ComUtil {
// 憑證獲取(GET)
public final static String token_url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=CORPID&corpsecret=CORPSECRET";
/**
* 發送https請求
* @param requestUrl 請求地址
* @param requestMethod 請求方式(GET、POST)
* @param outputStr 提交的數據
* @return JSONObject(通過JSONObject.get(key)的方式獲取json對象的屬性值)
*/
public static JSONObject httpsRequest(String requestUrl, String requestMethod, String outputStr) {
JSONObject jsonObject = null;
try {
// 創建SSLContext對象,並使用我們指定的信任管理器初始化
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 從上述SSLContext對象中得到SSLSocketFactory對象
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL url = new URL(requestUrl);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setSSLSocketFactory(ssf);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
// 設置請求方式(GET/POST)
conn.setRequestMethod(requestMethod);
// 當outputStr不為null時向輸出流寫數據
if (null != outputStr) {
OutputStream outputStream = conn.getOutputStream();
// 注意編碼格式
outputStream.write(outputStr.getBytes("UTF-8"));
outputStream.close();
}
// 從輸入流讀取返回內容
InputStream inputStream = conn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
StringBuffer buffer = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
// 釋放資源
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
inputStream = null;
conn.disconnect();
jsonObject = JSONObject.fromObject(buffer.toString());
} catch (ConnectException ce) {
} catch (Exception e) {
}
return jsonObject;
}
/**
* 獲取接口訪問憑證
* @param corpid 憑證
* @param corpsecret 密鑰
* @return
*/
public static String getAccessToken(String corpid, String corpsecret) {
String access_token = "";
String requestUrl = token_url.replace("CORPID", corpid).replace("CORPSECRET", corpsecret);
// 發起GET請求獲取憑證
JSONObject jsonObject = httpsRequest(requestUrl, "GET", null);
if (null != jsonObject) {
try {
access_token = jsonObject.getString("access_token");
} catch (JSONException e) {
access_token = null;
}
}
return access_token;
}
}
public class SendWxInfo {
/**
* @param touser 成員ID列表
* @param toparty 部門ID列表
* @param totag 標簽ID列表
* @param msgtype 消息類型,此時固定為:text(支持消息型應用跟主頁型應用)
* @param agentid 企業應用的id,整型。可在應用的設置頁面查看
* @param content 消息內容,最長不超過2048個字節,注意:主頁型應用推送的文本消息在微信端最多只顯示20個字(包含中英文)
* @return int 表示是否是保密消息,0表示否,1表示是,默認0
*/
public static int Send_msg(String touser,String toparty,String totag,String msgtype,int agentid,String content){
int errCode=0;
//拼接請求地址
String requestUrl=WxConstants.SEND_MSG_URL.replace("ACCESS_TOKEN", ComUtil.getAccessToken(WxConstants.CORPID, WxConstants.SECRET));
//需要提交的數據
String postJson = "{\"agentid\":%s,\"touser\": \"%s\",\"toparty\": \"%s\",\"totag\": \"%s\","+
"\"msgtype\":\"%s\",\"text\": {\"content\": \"%s\"},\"safe\":0}";
String outputStr=String.format(postJson,agentid,touser,toparty,totag,msgtype,content);
System.out.println(outputStr);
//創建成員
JSONObject jsonObject=ComUtil.httpsRequest(requestUrl, "POST", outputStr);
if(null!=jsonObject){
System.out.println(jsonObject.toString()+"=====");
}
return errCode;
}
/***
* 測試
* @param args
*/
public static void main(String[] args) {
//參數:成員ID,部門ID,標簽ID,消息類型,企業應用的id,消息內容
//注意:部門不為""或者標簽不為"",將會給該組的每個成員發送消息
//所以給某個單獨的成員推送消息,請將二、三參數設為空
Send_msg("hongdan","","","text",WxConstants.AGENTID,"我把這個寫成靜態類,吳濤那邊直接調用就行了");
}
}