微信企业号开发-向指定成员发送消息

风乍起,吹皱一池春水。

微信中服务号,订阅号可通过“消息模板”接口向成员推送指定模板消息。企业号则比较开发。

首先需要找到企业号的消息发送的接口【这个很重要】,在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,"我把这个写成静态类,吴涛那边直接调用就行了");
}

}

 

posted @ 2017-02-17 15:43  愤怒的小黑球  阅读(9491)  评论(1编辑  收藏  举报