微信公众号开发客服接口的示例分析
创始人
2024-11-23 03:20:59
0次

微信平台更新之后,发现客服接口不错。

按照官方文档,是向客服接口发送规定的JSon 就可以了。

首先先封装下 JSon 的类:

package com.lwz.wx.bean.kf;
 
// 这个是最外层的 也可以说是基类吧、
public class Basebean {
private String touser;
private String msgtype;
 
public String getTouser() {
return touser;
}
public void setTouser(String touser) {
this.touser = touser;
}
public String getMsgtype() {
return msgtype;
}
public void setMsgtype(String msgtype) {
this.msgtype = msgtype;
}
   
}
//这个类是继承基类、
 
package com.lwz.wx.bean.kf;
public class BaseNews extends Basebean{
  private Kfnews news;
public Kfnews getNews() {
return news;
}
public void setNews(Kfnews news) {
this.news = news;
}
 
}
//
package com.lwz.wx.bean.kf;
import java.util.List;
public class Kfnews {
private List articles;
public List getArticles() {
return articles;
}
public void setArticles(List articles) {
this.articles = articles;
}
}
//
package com.lwz.wx.bean.kf;
 
 
public class articles {
private String title;
  private String description;
  private String url;
  private String picurl;
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 getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getPicurl() {
return picurl;
}
public void setPicurl(String picurl) {
this.picurl = picurl;
}
 
}

以上的结构就对应

接下来就是对JSON 的数据的创建了

package com.lwz.wx.main;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import net.sf.json.JSONObject;
import com.lwz.wx.bean.AccessToken;
import com.lwz.wx.bean.Articles;
import com.lwz.wx.bean.kf.BaseNews;
import com.lwz.wx.bean.kf.BaseText;
import com.lwz.wx.bean.kf.Basebean;
import com.lwz.wx.bean.kf.Kfnews;
import com.lwz.wx.bean.kf.articles;
import com.lwz.wx.bean.kf.text;
import com.lwz.wx.util.WeixinUtil;
 
 
 
 
public class KfManager {
private final static Logger log = Logger.getLogger(Basebean.class);
   public static void Gotokf(String openid){
     String appId =""; //填上自己的APPID 下同  需要认证过的哦
String appSecret="";
// 调用接口获取access_token
AccessToken at = WeixinUtil.getAccessToken(appId, appSecret);
if (null != at) {
// 调用接口发送消息
int result = WeixinUtil.Runkf( getkfnews(openid), at.getToken()); // 这个方法会在下面 展示
//int result = WeixinUtil.createMenu(getMenu(),"1832148947");
// 判断菜单创建结果
if (0 == result)
log.info("调用客服信息发送成功!");
else
log.info("客服调用失败,错误码:" + result);
}
  }
private static BaseNews getkfnews(String openid) {
articles art1=new articles();
art1.setDescription("1");
art1.setPicurl("http://www.baidu.com");
art1.setTitle("测试1");
art1.setUrl("http://www.baidu.com");
 
articles art2=new articles();
art2.setDescription("1");
art2.setPicurl("http://www.baidu.com");
art2.setTitle("测试1");
art2.setUrl("http://www.baidu.com");
List list = new ArrayList();
Kfnews news=new Kfnews();
list.add(art1);
list.add(art2);
news.setArticles(list);
 
BaseNews kfbean=new BaseNews();
kfbean.setMsgtype("news");
kfbean.setTouser(openid);
kfbean.setNews(news);
String jsonkfbean = JSONObject.fromObject(kfbean).toString();
System.out.println(jsonkfbean);
return kfbean;
 
}
 
private static BaseText getkftext(String openid) {
  text text=new text();
text.setContent("文本内容");
BaseText textbean=new BaseText();
textbean.setMsgtype("text");
textbean.setTouser(openid);
textbean.setText(text);
String jsonkfbean = JSONObject.fromObject(textbean).toString();
System.out.println(jsonkfbean);
return textbean;
 
}
} 
// 上面的有用到一个调用接口的方法如下:
 
public static String kf_news_url= "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN";
 
public static int Runkf(Basebean getkfnews, String token) {
 
int result = 0;
 
 
 
 
 
// 拼装创建的url
 
String url = kf_news_url.replace("ACCESS_TOKEN", token);
 
// 将对象转换成json字符
 
String jsonnews = JSONObject.fromObject(getkfnews).toString();
 
//System.out.println(jsonMenu);
 
// 调用接口创建
 
JSONObject jsonObject = httpRequest(url, "POST", jsonnews);
 
if (null != jsonObject) {
 
if (0 != jsonObject.getInt("errcode")) {
 
result = jsonObject.getInt("errcode");
 
log.error("调用客服接口失败 errcode:{} errmsg:{}");
 
}
 
}
 
 
 
 
 
return result;
 
}

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...