企业号服务器消息推送,微信企业号的消息推送(群发)

package cn.com.chinautrust.ssoserver.utils;javascript

import java.util.HashMap;

import java.util.Map;java

import sun.org.mozilla.javascript.internal.ObjArray;json

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONArray;

import com.alibaba.fastjson.JSONObject;api

/**

* 用于微信企业号消息发送

* @author yq

*

*/

public class WxUtil {

static String corpid = "ww90e565617aff08f";

static String Secret = "xVsrITDz0G6iM6_9PouV2hjDCS38G4fllWK5EvjD";

static String AgentSecret = "zXp_DTAEAZ9A1wYJ31xBzzhS9mZmLXBZncKmxUSwahQ";

static String getAccessToken = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRECT";

static String getAgentAccessToken = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRECT";

static String getUsers = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token=ACCESS_TOKEN&department_id=DEPARTMENT_ID&fetch_child=FETCH_CHILD";

static String sendMsgUrl = " https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=ACCESS_TOKEN";

public static void main(String[] args) {

getAccessToken = getAccessToken.replace("ID", corpid);

System.out.println("==00==getAccessToken:"+getAccessToken+"=================");

getAccessToken = getAccessToken.replace("SECRECT", Secret);

System.out.println("==11==getAccessToken:"+getAccessToken+"=================");

String accessToken = HttpSendUtil.sendGet(getAccessToken);

System.out.println("===accessToken:"+accessToken+"==================");

JSONObject accesstokeJson = JSONObject.parseObject(accessToken);

String accessTokenStr = accesstokeJson.getString("access_token");

String userinfos = getUserIds(accessTokenStr);

System.out.println("=====accessTokenStr:【"+accessTokenStr+"】===========");

getAgentAccessToken = getAgentAccessToken.replace("ID", corpid);

getAgentAccessToken = getAgentAccessToken.replace("SECRECT", AgentSecret);

String agent_accessToken = HttpSendUtil.sendGet(getAgentAccessToken);

JSONObject agentAccesstokeJson = JSONObject.parseObject(agent_accessToken);

String agentAccesstokeJsonStr = agentAccesstokeJson.getString("access_token");

System.out.println("=======agentAccesstokeJsonStr:"+agentAccesstokeJsonStr+"================");

sendMsgUrl = sendMsgUrl.replace("ACCESS_TOKEN", agentAccesstokeJsonStr);

String param = "{"

+  "\"touser\":\""+userinfos+"\","

+  "\"toparty\":\"\","

+  "\"totag\":\"\","

+  "\"msgtype\":\"text\","

+  "\"agentid\": 1000002,"

+  "\"text\" : {"

+      "\"content\":\"恭喜你啊,恭喜你。\""

+  "},"

+  "\"safe\":0"

+  "}";

System.out.println("========param:"+JSON.toJSONString(param)+"===========");

String res = HttpSendUtil.sendPost(sendMsgUrl, param);

System.out.println("=====res:"+res+"======");

}

public static String getUserIds(String accessTokenStr){

getUsers = getUsers.replace("ACCESS_TOKEN", accessTokenStr);

getUsers = getUsers.replace("DEPARTMENT_ID", "1");

getUsers = getUsers.replace("FETCH_CHILD", "0");

System.out.println("===getUsers:"+getUsers+"==================");

String userinfos = HttpSendUtil.sendGet(getUsers);

System.out.println("===userinfos:"+userinfos+"==================");

String userlist = JSONObject.parseObject(userinfos).getString("userlist");

JSONArray users = JSONArray.parseArray(userlist);

JSONObject[] objs = users.toArray(new JSONObject[users.size()]);

StringBuffer buf = new StringBuffer();

String userid = "";

for (int i=0;i

userid = objs[i].getString("userid");

if( i == objs.length-1 ){

buf.append(userid);

}else{

buf.append(userid+"|");

}

}

return buf.toString();

}

}

微信

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值