企业可以主动发消息给成员,消息量不受限制。
调用接口时,使用Https协议、JSON数据包格式,数据包不需做加密处理。
目前消息型应用支持文本、图片、语音、视频、文件、图文等消息类型。除了news类型,其它类型的消息可在发送时加上保密选项,保密消息会被打上水印,并且只有接收者才能阅读。主页型应用支持文本类型,文本长度不超过20个字。
{ "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" } ] } }
参数 | 必须 | 说明 |
---|---|---|
touser | 否 | 成员ID列表(消息接收者,多个接收者用‘|’分隔,最多支持1000个)。特殊情况:指定为@all,则向关注该企业应用的全部成员发送 |
toparty | 否 | 部门ID列表,多个接收者用‘|’分隔,最多支持100个。当touser为@all时忽略本参数 |
totag | 否 | 标签ID列表,多个接收者用‘|’分隔。当touser为@all时忽略本参数 |
msgtype | 是 | 消息类型,此时固定为:news |
agentid | 是 | 企业应用的id,整型。可在应用的设置页面查看 |
articles | 是 | 图文消息,一个图文消息支持1到10条图文 |
title | 否 | 标题 |
description | 否 | 描述 |
url | 否 | 点击后跳转的链接。 |
picurl | 否 | 图文消息的图片链接,支持JPG、PNG格式,较好的效果为大图640*320,小图80*80。如不填,在客户端不显示图片 |
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
/** * 主动发送消息 * @param mess * @return */ public boolean sendReqMsg(ReqBaseMessage mess){ //消息json格式 String jsonContext=mess.toJsonStr(); //System.out.println(jsonContext); //获得token String token=getTokenFromWx(); boolean flag=false; try { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost httpPost= new HttpPost("https://qyapi.weixin.qq.com/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 handler ResponseHandler<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 block e.printStackTrace(); } return flag; }
---------------------------------------------------------------------------------------------------------------
------实体类
---------------------------------------------------------------------------------------------------------------
/** * * * @author muyunfei * * <p>Modification History:</p> * <p>Date Author Description</p> * <p>------------------------------------------------------------------</p> * <p>Apr 8, 2015 牟云飞 新建</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.haiyisoft.entity.wx.req; /** * 图文model */ public class WxArticle { // 图文消息名称 private String title; // 图文消息描述 private String description; // 图片链接,支持JPG、PNG格式,较好的效果为大图640*320,小图80*80 private 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; } }