100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 微信企业号开发三:主动调用模式之发送news消息

微信企业号开发三:主动调用模式之发送news消息

时间:2019-06-08 11:47:16

相关推荐

微信企业号开发三:主动调用模式之发送news消息

企业可以主动发消息给成员,消息量不受限制

调用接口时,使用Https协议、JSON数据包格式,数据包不需做加密处理。

目前消息型应用支持文本、图片、语音、视频、文件、图文等消息类型。除了news类型,其它类型的消息可在发送时加上保密选项,保密消息会被打上水印,并且只有接收者才能阅读。主页型应用支持文本类型,文本长度不超过20个字。

news消息

{"touser": "UserID1|UserID2|UserID3","toparty": " PartyID1 | PartyID2 ","totag": " TagID1 | TagID2 ","msgtype": "news","agentid": 1,"news": {"articles":[{"title": "Title","description": "Description","url": "URL","picurl": "PIC_URL"},{"title": "Title","description": "Description","url": "URL","picurl": "PIC_URL"} ]}}

---------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------

/*** 主动发送消息* @param mess* @return*/public boolean sendReqMsg(ReqBaseMessage mess){//消息json格式String jsonContext=mess.toJsonStr();//System.out.println(jsonContext);//获得tokenString token=getTokenFromWx();boolean flag=false;try {CloseableHttpClient httpclient = HttpClients.createDefault();HttpPost httpPost= new HttpPost("https://qyapi./cgi-bin/message/send?access_token="+token);//发送json格式的数据StringEntity myEntity = new StringEntity(jsonContext, ContentType.create("text/plain", "UTF-8"));//设置需要传递的数据httpPost.setEntity(myEntity);// Create a custom response handlerResponseHandler<JSONObject> responseHandler = new ResponseHandler<JSONObject>() {//对访问结果进行处理public JSONObject handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {int status = response.getStatusLine().getStatusCode();if (status >= 200 && status < 300) {HttpEntity entity = response.getEntity();if(null!=entity){String result= EntityUtils.toString(entity);//根据字符串生成JSON对象JSONObject resultObj = JSONObject.fromObject(result);return resultObj;}else{return null;}} else {throw new ClientProtocolException("Unexpected response status: " + status);}}};//返回的json对象JSONObject responseBody = httpclient.execute(httpPost, responseHandler);System.out.println(responseBody);int result= (Integer) responseBody.get("errcode");if(0==result){flag=true;}else{flag=false;}httpclient.close();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}return flag;}

---------------------------------------------------------------------------------------------------------------

------实体类

---------------------------------------------------------------------------------------------------------------

/*** * * @author muyunfei* * <p>Modification History:</p> * <p>Date AuthorDescription</p>* <p>------------------------------------------------------------------</p>* <p>Apr 8, 牟云飞 新建</p>*/public class WxNewsMessage extends ReqBaseMessage {public WxNewsMessage(){}//消息public List<WxArticle> news;public List<WxArticle> getNews() {return news;}public void setNews(List<WxArticle> news) {this.news = news;}public WxNewsMessage(String touser, String toparty, String totag,String msgtype, String agentid) {super();super.touser = touser;super.toparty = toparty;super.totag = totag;super.msgtype = msgtype;super.agentid = agentid;}public String toJsonStr(){StringBuffer jsonStr=new StringBuffer("{");StringBuffer str_tmp= new StringBuffer("");//"\"touser\": \"@all\",\"msgtype\": \"text\",\"agentid\": \"0\",\"text\": {\"content\": \"helloworld\"},\"safe\": \"0\"}";if(null!=touser&&!"".equals(touser)){if(!"".equals(str_tmp.toString())){str_tmp.append(",");}str_tmp.append("\"touser\": \""+touser+"\"");}if(null!=toparty&&!"".equals(toparty)){if(!"".equals(str_tmp.toString())){str_tmp.append(",");}str_tmp.append("\"toparty\": \""+toparty+"\"");}if(null!=totag&&!"".equals(totag)){if(!"".equals(str_tmp.toString())){str_tmp.append(",");}str_tmp.append("\"totag\": \""+totag+"\"");}if(null!=msgtype&&!"".equals(msgtype)){//去除空格msgtype=msgtype.trim();//判断是否加逗号if(!"".equals(str_tmp.toString())){str_tmp.append(",");}str_tmp.append("\"msgtype\": \""+msgtype+"\"");//新闻消息if(null!=news&&0!=news.size()){if(!"".equals(str_tmp.toString())){str_tmp.append(",");}StringBuffer content=new StringBuffer("");for (int i = 0; i < news.size(); i++) {if(i!=0){content.append(",");}content.append("{");//获得一条消息WxArticle info = news.get(i);StringBuffer artileContent=new StringBuffer("");if(null!=info.getTitle()&&!"".equals(info.getTitle())){if(null!=artileContent.toString()&&!"".equals(artileContent.toString())){artileContent.append(",");}//标题artileContent.append("\"title\": \""+info.getTitle()+"\"");}if(null!=info.getDescription()&&!"".equals(info.getDescription())){if(null!=artileContent&&!"".equals(artileContent)){artileContent.append(",");}//描述artileContent.append("\"description\": \""+info.getDescription()+"\"");}if(null!=info.getUrl()&&!"".equals(info.getUrl())){if(null!=artileContent&&!"".equals(artileContent)){artileContent.append(",");}//详细地址artileContent.append("\"url\": \""+info.getUrl()+"\"");}if(null!=info.getPicurl()&&!"".equals(info.getPicurl())){if(null!=artileContent&&!"".equals(artileContent)){artileContent.append(",");}//图片地址artileContent.append("\"picurl\": \""+info.getPicurl()+"\"");}content.append(artileContent.toString()+"}");}str_tmp.append("\"news\": {\"articles\":["+content+"]}");}}if(null!=agentid&&!"".equals(agentid)){if(!"".equals(str_tmp.toString())){str_tmp.append(",");}str_tmp.append("\"agentid\": \""+agentid+"\"");}jsonStr.append(str_tmp);jsonStr.append("}");return jsonStr.toString();}}

package com.entity.wx.req;/*** 图文model*/public class WxArticle {// 图文消息名称private String title;// 图文消息描述private String description;// 图片链接,支持JPG、PNG格式,较好的效果为大图640*320,小图80*80private String picurl;// 点击图文消息跳转链接private String url;public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}public String getPicurl() {return picurl;}public void setPicurl(String picurl) {this.picurl = picurl;}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。