微信公众号 客服接口的开发实例详解
微信平台更新之后,发现客服接口不错。研究了下和大家分享下。
按照官方文档,是向客服接口发送规定的JSon就可以了。
首先先封装下JSon的类:
packagecom.lwz.wx.bean.kf; //这个是最外层的也可以说是基类吧、 publicclassBasebean{ privateStringtouser; privateStringmsgtype; publicStringgetTouser(){ returntouser; } publicvoidsetTouser(Stringtouser){ this.touser=touser; } publicStringgetMsgtype(){ returnmsgtype; } publicvoidsetMsgtype(Stringmsgtype){ this.msgtype=msgtype; } }
//这个类是继承基类、 packagecom.lwz.wx.bean.kf; publicclassBaseNewsextendsBasebean{ privateKfnewsnews; publicKfnewsgetNews(){ returnnews; } publicvoidsetNews(Kfnewsnews){ this.news=news; } } //
packagecom.lwz.wx.bean.kf; importjava.util.List; publicclassKfnews{ privateList<articles>articles; publicList<articles>getArticles(){ returnarticles; } publicvoidsetArticles(List<articles>articles){ this.articles=articles; } } //
packagecom.lwz.wx.bean.kf; publicclassarticles{ privateStringtitle; privateStringdescription; privateStringurl; privateStringpicurl; publicStringgetTitle(){ returntitle; } publicvoidsetTitle(Stringtitle){ this.title=title; } publicStringgetDescription(){ returndescription; } publicvoidsetDescription(Stringdescription){ this.description=description; } publicStringgetUrl(){ returnurl; } publicvoidsetUrl(Stringurl){ this.url=url; } publicStringgetPicurl(){ returnpicurl; } publicvoidsetPicurl(Stringpicurl){ this.picurl=picurl; } }
以上的结构就对应
接下来就是对JSON的数据的创建了
packagecom.lwz.wx.main; importjava.util.ArrayList; importjava.util.List; importorg.apache.log4j.Logger; importnet.sf.json.JSONObject; importcom.lwz.wx.bean.AccessToken; importcom.lwz.wx.bean.Articles; importcom.lwz.wx.bean.kf.BaseNews; importcom.lwz.wx.bean.kf.BaseText; importcom.lwz.wx.bean.kf.Basebean; importcom.lwz.wx.bean.kf.Kfnews; importcom.lwz.wx.bean.kf.articles; importcom.lwz.wx.bean.kf.text; importcom.lwz.wx.util.WeixinUtil; publicclassKfManager{ privatefinalstaticLoggerlog=Logger.getLogger(Basebean.class); publicstaticvoidGotokf(Stringopenid){ StringappId="";//填上自己的APPID下同需要认证过的哦 StringappSecret=""; //调用接口获取access_token AccessTokenat=WeixinUtil.getAccessToken(appId,appSecret); if(null!=at){ //调用接口发送消息 intresult=WeixinUtil.Runkf(getkfnews(openid),at.getToken());//这个方法会在下面展示 //intresult=WeixinUtil.createMenu(getMenu(),"1832148947"); //判断菜单创建结果 if(0==result) log.info("调用客服信息发送成功!"); else log.info("客服调用失败,错误码:"+result); } }
privatestaticBaseNewsgetkfnews(Stringopenid){ articlesart1=newarticles(); art1.setDescription("1"); art1.setPicurl("http://www.baidu.com"); art1.setTitle("测试1"); art1.setUrl("http://www.baidu.com"); articlesart2=newarticles(); art2.setDescription("1"); art2.setPicurl("http://www.baidu.com"); art2.setTitle("测试1"); art2.setUrl("http://www.baidu.com"); List<articles>list=newArrayList<articles>(); Kfnewsnews=newKfnews(); list.add(art1); list.add(art2); news.setArticles(list); BaseNewskfbean=newBaseNews(); kfbean.setMsgtype("news"); kfbean.setTouser(openid); kfbean.setNews(news); Stringjsonkfbean=JSONObject.fromObject(kfbean).toString(); System.out.println(jsonkfbean); returnkfbean; } privatestaticBaseTextgetkftext(Stringopenid){ texttext=newtext(); text.setContent("文本内容"); BaseTexttextbean=newBaseText(); textbean.setMsgtype("text"); textbean.setTouser(openid); textbean.setText(text); Stringjsonkfbean=JSONObject.fromObject(textbean).toString(); System.out.println(jsonkfbean); returntextbean; } } //上面的有用到一个调用接口的方法如下: publicstaticStringkf_news_url="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN"; publicstaticintRunkf(Basebeangetkfnews,Stringtoken){ intresult=0; //拼装创建的url Stringurl=kf_news_url.replace("ACCESS_TOKEN",token); //将对象转换成json字符 Stringjsonnews=JSONObject.fromObject(getkfnews).toString(); //System.out.println(jsonMenu); //调用接口创建 JSONObjectjsonObject=httpRequest(url,"POST",jsonnews); if(null!=jsonObject){ if(0!=jsonObject.getInt("errcode")){ result=jsonObject.getInt("errcode"); log.error("调用客服接口失败errcode:{}errmsg:{}"); } } returnresult; }
到这里就完成了。可能会比较多。其他的文本的音乐的也都是这样的
希望能帮助大家学习参考此部分内容,谢谢大家对本站的支持!