当用户和公众号产生特定动作的交互时(具体动作列表请见下方说明),微信将会把消息数据推送给开发者,开发者可以在一段时间内(目前修改为48小时)调用客服接口,通过POST一个JSON数据包来发送消息给普通用户。此接口主要用于客服等有人工消息处理环节的功能,方便开发者为用户提供更加优质的服务。
添加客服帐号
开发者可以通过本接口为公众号添加客服账号,每个公众号最多添加10个客服账号。该接口调用请求如下:
http请求方式: POST https://api.weixin.qq.com/customservice/kfaccount/add?access_token=ACCESS_TOKEN
POST数据示例如下:
{ "kf_account" : "test1@test", "nickname" : "客服1", "password" : "pswmd5", }
返回说明(正确时的JSON返回结果):
{ "errcode" : 0, "errmsg" : "ok", }
错误时微信会返回错误码等信息,请根据错误码查询错误信息
注意:如果想调用上述接口必须在接口列表里面开启客服接口权限,否则会调用失败
java案例代码
package com.test.weixin; /*** * @author V型知识库 www.vxzsk.com */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import net.sf.json.JSONObject; public class TestAcessToken { /*** * 模拟get请求 * @param url * @param charset * @param timeout * @return */ public static String sendGet(String url, String charset, int timeout) { String result = ""; try { URL u = new URL(url); try { URLConnection conn = u.openConnection(); conn.connect(); conn.setConnectTimeout(timeout); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), charset)); String line=""; while ((line = in.readLine()) != null) { result = result + line; } in.close(); } catch (IOException e) { return result; } } catch (MalformedURLException e) { return result; } return result; } public static String sendPost(String requrl,String param){ URL url; String sTotalString=""; try { url = new URL(requrl); URLConnection connection = url.openConnection(); connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("Content-Type", "text/xml"); // connection.setRequestProperty("Content-Length", body.getBytes().length+""); connection.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"); connection.setDoOutput(true); OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "utf-8"); out.write(param); // 向页面传递数据。post的关键所在! out.flush(); out.close(); // 一旦发送成功,用以下方法就可以得到服务器的回应: String sCurrentLine; sCurrentLine = ""; sTotalString = ""; InputStream l_urlStream; l_urlStream = connection.getInputStream(); // 传说中的三层包装阿! BufferedReader l_reader = new BufferedReader(new InputStreamReader( l_urlStream)); while ((sCurrentLine = l_reader.readLine()) != null) { sTotalString += sCurrentLine + "\r\n"; } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(sTotalString); return sTotalString; } public static void main(String[] args) { String appid="你公众号基本设置里的应用id";//应用ID String appSecret="你公众号基本设置里的应用密钥";//(应用密钥) String url ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+appSecret+""; String backData=TestAcessToken.sendGet(url, "utf-8", 10000); System.out.println("返回:"+backData); JSONObject jsonObject = JSONObject.fromObject(backData); String access_token=jsonObject .getString("access_token"); //添加客服 String add_url = "https://api.weixin.qq.com/customservice/kfaccount/add?access_token="+access_token; String param="{\"kf_account\" : \"test1@test\",\"nickname\" : \"客服1\",\"password\" : \"pswmd5\"}"; String data = TestAcessToken.sendPost(add_url, param); System.out.println(data); } }
此文章本站原创,地址 https://www.vxzsk.com/146.html
转载请注明出处!谢谢!
感觉本站内容不错,读后有收获?小额赞助,鼓励网站分享出更好的教程