


WeChat public account development customer service interface example code
Mar 17, 2017 pm 03:28 PMAfter the WeChat platform was updated, I found that the customer service interface is good. I’ll do some research and share it with you.
According to the official documentation, just send the specified JSon to the customer service interface.
First encapsulate the JSON class:
package com.lwz.wx.bean.kf; // 這個(gè)是最外層的 也可以說是基類吧、 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; } }
//這個(gè)類是繼承基類、 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> articles; public List<articles> getArticles() { return articles; } public void setArticles(List<articles> 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; } }
The above structure corresponds to
The next step is to create the JSON data
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 下同 需要認(rèn)證過的哦 String appSecret=""; // 調(diào)用接口獲取access_token AccessToken at = WeixinUtil.getAccessToken(appId, appSecret); if (null != at) { // 調(diào)用接口發(fā)送消息 int result = WeixinUtil.Runkf( getkfnews(openid), at.getToken()); // 這個(gè)方法會(huì)在下面 展示 //int result = WeixinUtil.createMenu(getMenu(),"1832148947"); // 判斷菜單創(chuàng)建結(jié)果 if (0 == result) log.info("調(diào)用客服信息發(fā)送成功!"); else log.info("客服調(diào)用失敗,錯(cuò)誤碼:" + result); } }
private static BaseNews getkfnews(String openid) { articles art1=new articles(); art1.setDescription("1"); art1.setPicurl("http://www.baidu.com"); art1.setTitle("測(cè)試1"); art1.setUrl("http://www.baidu.com"); articles art2=new articles(); art2.setDescription("1"); art2.setPicurl("http://www.baidu.com"); art2.setTitle("測(cè)試1"); art2.setUrl("http://www.baidu.com"); List<articles> list = new ArrayList<articles>(); 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("文本內(nèi)容"); 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; } } // 上面的有用到一個(gè)調(diào)用接口的方法如下: 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; // 拼裝創(chuàng)建的url String url = kf_news_url.replace("ACCESS_TOKEN", token); // 將對(duì)象轉(zhuǎn)換成json字符 String jsonnews = JSONObject.fromObject(getkfnews).toString(); //System.out.println(jsonMenu); // 調(diào)用接口創(chuàng)建 JSONObject jsonObject = httpRequest(url, "POST", jsonnews); if (null != jsonObject) { if (0 != jsonObject.getInt("errcode")) { result = jsonObject.getInt("errcode"); log.error("調(diào)用客服接口失敗 errcode:{} errmsg:{}"); } } return result; }
to It's done here. There may be more. Other texts and music are also like this
The above is the detailed content of WeChat public account development customer service interface example code. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)