微信公众平台

您所在的位置:网站首页 如何获取聊天记录 微信公众平台

微信公众平台

2024-07-17 01:43| 来源: 网络整理| 查看: 265

环境: java语言 , 服务号,

功能:服务号有一个客服功能,我目前需要获取所有客服的聊天记录。

环境介绍:

  1.首先需要在微信公众平台开发者配置中配置一些参数

   其中开发者id 和开发者密码用来获取AccessToken   , 要配置IP白名单,不然调用客户聊天记录失败。

 

  2.登录服务号,找到这个客服功能。目前我这个服务号有俩个客服,我现在需要获取这俩个客服的聊天记录。

 

2.找到客服功能的开发文档。 

地址:https://developers.weixin.qq.com/doc/offiaccount/Customer_Service/Obtain_chat_transcript.html

post  请求,传参方式如下,注意:每次查询的时间段不能超过24小时。

3.代码实战:

 

@GetMapping(value = "/sendCondition") public void sendCondition() throws ParseException { //1.获取AccessToken String accessToken = WeiXinParamesUtil.getAccessToken("customerService"); String url = "https://api.weixin.qq.com/customservice/msgrecord/getmsglist?access_token=ACCESS_TOKEN"; url = url.replace("ACCESS_TOKEN", accessToken); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = simpleDateFormat.parse("2020-02-11 8:45:25"); long startTime = date.getTime()/1000; Date date1 = simpleDateFormat.parse("2020-02-11 21:45:26"); long endTime = date1.getTime()/1000; String jsonStr = " {\n" + " \"starttime\" : "+String.valueOf(startTime)+",\n" + " \"endtime\" : "+String.valueOf(endTime)+",\n" + " \"msgid\" : 1,\n" + " \"number\" : 10000 \n" + "}"; JSONObject jsonObject = SendRequest.sendPost(url, jsonStr); System.out.println("1111-----" + jsonObject); } public static String getWeiAccessToken ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET"; /** * 获取微信公众平台的access_token * @param type * @return */ public static String getAccessToken(String type) { String url = ""; if ("users".equals(type)) { url = getWeiAccessToken.replace("APPID", WeiXinParamesUtil.APPID).replace("APPSECRET", WeiXinParamesUtil.SECRET); }else if("customerService".equals(type)){ url = getWeiAccessToken.replace("APPID", WeiXinParamesUtil.APPID).replace("APPSECRET", WeiXinParamesUtil.SECRET); } JSONObject departmentJson = SendRequest.sendGet2(url); return departmentJson.getString("access_token"); }

SendRequest类:

package com.bos.util; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.bos.data.model.vo.basic.PostValue; import com.google.common.base.Strings; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import java.io.*; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; import java.util.List; import java.util.Map; public class SendRequest { //发送GET请求 public static JSONObject sendGet(String url) { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); HttpSession session = request.getSession(); JSONObject jsonObject = null; StringBuffer sb = new StringBuffer(); BufferedReader in = null; try { String urlName = url; URL realUrl = new URL(urlName); URLConnection conn = realUrl.openConnection();// 打开和URL之间的连接 conn.setRequestProperty("accept", "*/*");// 设置通用的请求属性 conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); //针对群晖NAS请求,加一个Cookie if (session.getAttribute("sid") != null) { conn.addRequestProperty("Cookie", "id=" + session.getAttribute("sid")); } conn.setConnectTimeout(10000); conn.connect();// 建立实际的连接 in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));// 定义BufferedReader输入流来读取URL的响应 String line; while ((line = in.readLine()) != null) { sb.append(line); } jsonObject = JSON.parseObject(sb.toString()); } catch (Exception e) { System.out.println("发送GET请求出现异常!" + e); } finally {// 使用finally块来关闭输入流 try { if (in != null) { in.close(); } } catch (IOException ex) { System.out.println("关闭流异常"); } } return jsonObject; } public static JSONObject sendGet2(String url) { JSONObject jsonObject = null; StringBuffer sb = new StringBuffer(); BufferedReader in = null; try { String urlName = url; URL realUrl = new URL(urlName); // 打开和URL之间的连接 URLConnection conn = realUrl.openConnection(); // 设置通用的请求属性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); conn.setConnectTimeout(10000); // 建立实际的连接 conn.connect(); // 定义BufferedReader输入流来读取URL的响应 in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); String line; while ((line = in.readLine()) != null) { sb.append(line); } jsonObject = JSON.parseObject(sb.toString()); } catch (Exception e) { System.out.println("发送GET请求出现异常!" + e); // 使用finally块来关闭输入流 } finally { try { if (in != null) { in.close(); } } catch (IOException ex) { System.out.println("关闭流异常"); } } return jsonObject; } // 发送post请求(返回json) public static JSONObject sendPost(String url, String param) { PrintWriter out = null; BufferedReader in = null; JSONObject jsonObject = null; String result = ""; try { URL realUrl = new URL(url); // 打开和URL之间的连接 URLConnection conn = realUrl.openConnection(); // 发送POST请求必须设置如下两行 conn.setDoOutput(true); conn.setDoInput(true); // conn.addRequestProperty("Cookie", "stay_login=1 smid=DumpWzWQSaLmKlFY1PgAtURdV_u3W3beoei96zsXkdSABwjVCRrnnNBsnH1wGWI0-VIflgvMaZAfli9H2NGtJg id=EtEWf1XZRLIwk1770NZN047804");//设置获取的cookie // 获取URLConnection对象对应的输出流(设置请求编码为UTF-8) out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8")); // 发送请求参数 out.print(param); // flush输出流的缓冲 out.flush(); // 获取请求返回数据(设置返回数据编码为UTF-8) in = new BufferedReader( new InputStreamReader(conn.getInputStream(), "UTF-8")); String line; while ((line = in.readLine()) != null) { result += line; } jsonObject = JSONObject.parseObject(result); } catch (IOException e) { e.printStackTrace(); } finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return jsonObject; } // 发送post请求(返回json) public static String sendPost2String(String url, String param) { PrintWriter out = null; BufferedReader in = null; StringBuffer sb = new StringBuffer(); try { URL realUrl = new URL(url); // 打开和URL之间的连接 URLConnection conn = realUrl.openConnection(); // 设置通用的请求属性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 发送POST请求必须设置如下两行 conn.setDoOutput(true); conn.setDoInput(true); // 获取URLConnection对象对应的输出流 out = new PrintWriter(conn.getOutputStream()); // 发送请求参数 out.print(param); // flush输出流的缓冲 out.flush(); // 定义BufferedReader输入流来读取URL的响应 in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { sb.append(line); } } catch (Exception e) { System.out.println("[POST请求]向地址:" + url + " 发送数据:" + param + " 发生错误!"); } finally {// 使用finally块来关闭输出流、输入流 try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { System.out.println("关闭流异常"); } } return sb.toString(); } /** * 获取在session中保存的cookie * * @param request * @return */ private static String getSessionIncookie(HttpServletRequest request) { String back = ""; HttpSession session = request.getSession(); String verCode = (String) session.getAttribute("verCode"); String JSESSIONID = (String) session.getAttribute("JSESSIONID"); System.out.println(verCode); System.out.println(JSESSIONID); if (verCode != null) { back = verCode; } if (JSESSIONID != null) { if (!Strings.isNullOrEmpty(back)) { back += " "; } back += JSESSIONID; } System.out.println(back); return back; } }

 

4.测试结果如下: 

在网页上显示为:

5.返回结果参数说明:



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3