加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

微信公众号 客服接口的开发实例详解

发布时间:2020-12-14 17:08:54 所属栏目:资源 来源:网络整理
导读:微信平台更新之后,发现客服接口不错。研究了下 和大家分享下。 按照官方文档,是向客服接口发送规定的JSon 就可以了。 首先先封装下 JSon 的类: // 这个是最外层的 也可以说是基类吧、 public class Basebean { private String touser; private String msg

微信平台更新之后,发现客服接口不错。研究了下 和大家分享下。

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

首先先封装下 JSon 的类:

// 这个是最外层的 也可以说是基类吧、
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;
}

}
//

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 的数据的创建了

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);
}
}

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;

}

到这里就完成了。可能会比较多。其他的文本的 音乐的 也都是这样的

希望能帮助大家学习参考此部分内容,谢谢大家对本站的支持!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读