最全的java对接微信小程序客服功能实现(包含自动回复文本消息、图片消息,进入人工客服)

本文详细介绍了如何使用Java对接微信小程序的客服功能,包括验证请求、自动回复文本和图片消息,以及如何切换到人工客服。在`acceptMessage`方法中,除了回复文本消息,还需添加`sendImageMessage`来发送图片。同时,文章强调了校验用的token与对接时的token不同,并提供了获取和使用media_id发送图片的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java对接微信小程序客服功能实现(包含自动回复文本消息、图片消息,进入人工客服)

第一步:请求校验(确认请求来自微信服务器)
代码如下:

@ApiOperation(value = " 微信消息通知-请求校验(确认请求来自微信服务器)")
    @RequestMapping(value = "/signature")
    public String signature(HttpServletRequest request, HttpServletResponse response) {
   
        if (request.getMethod().equals("GET")) {
   
            //微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数
            String signature = request.getParameter("signature");
            // 时间戳
            String timestamp = request.getParameter("timestamp");
            // 随机数
            String nonce = request.getParameter("nonce");
            // 随机字符串
            String echostr = request.getParameter("echostr");
            log.info("echostr>>>>>>>>>>" + echostr);
            //通过检验signature对请求进行校验,若校验成功则原样返回echostr,否则接入失败
            if (checkSignature(signature, timestamp, nonce)) {
   
                log.info("echostr>>>>>>>>>>>>" + echostr);
                return echostr;
            }
            log.info("客服消息验证url验证结果:{}", echostr);
        }

checkSignature()方法如下:

/**
     * 校验(确认请求来自微信服务器)
     *
     * @param signature
     * @param timestamp
     * @param nonce
     * @return
     */
    public boolean checkSignature(String signature, String timestamp, String nonce) {
   
        String[] params = {
   aliProperties.getAccessToken(), timestamp, nonce};
        log.info("params>>>>>>>>>>>>>" + params);
        //1、将token、timestamp、nonce三个参数进行字典序排序
        Arrays.sort(params);
        //2、将三个参数字符串拼接成一个字符串
        StringBuilder sb = new StringBuilder();
        for (String param : params) {
   
            sb.append(param);
        }
        log.info("字符串拼接>>>>>>>>>>>>>>>>>" + sb);
        MessageDigest md = null;
        String tpmStr = null;
        try {
   
            md = MessageDigest.getInstance("SHA-1");
            log.info("md>>>>>>>>>>>>" + md);
            //3、将三个参数字符串拼接成一个字符串进行sha1加密
            byte[] digest = md.digest(sb.toString().getBytes());
            log.info("digest>>>>>>>>>>>" + digest);
            tpmStr = StringUtil.byteToHex(digest);
            log.info("加密串>>>>>>>>>>>" + tpmStr);
        } catch (Exception e) {
   
            log.info("错误信息>>>>>>>>>>>>" + e.getMessage());
            e.printStackTrace();
        }
        //4、将sha1加密后的字符串可与signature对比,标识该请求来源于微信
        return tpmStr != null && tpmStr.equals(signature);
    }

上面代码就是做的第一步,校验。根据小程序对接文档可知,校验时为GET请求(一定)。

第二步:对接小程序客服功能
代码如下:

else {
   
     try {
   
         // 接收消息并返回消息
         String result = acceptMessage(request, response);
         log.info("接受消息并返回消息result>>>>>>" + result);
         // 响应消息
         PrintWriter out = response.getWriter();
         log.info("out>>>>>>>>" + out);
         out.print(result);
         out.close();
     } catch (Exception ex) {
   
         log.error("微信帐号接口配置失败!" + ex.getMessage());
         ex.printStackTrace();
       }
     }
        return null;
  }

这里的else是连在上面if后面的(因为对接小程序,只能写在一个接口中,校验时是GET,对接时是POST,所以if为校验,else才为真正对接类容)

acceptMessage()方法

 public String acceptMessage(HttpServletRequest request, HttpServletResponse response) {
   
        //返回值
        String result = "success";
        try {
   
            // 将请求、响应的编码均设置为UTF-8(防止中文乱码)
            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");
            Map<String, String> requestMap = MessageUtil.parseXml(request);
            log.info("requestMap>>>>>>>>>>" + requestMap);
            // 发送者的openid
            String fromUserName = requestMap.get("FromUserName");
            // 小程序的原始ID
            String toUserName = requestMap.get("ToUserName");
            // 消息类型
            String msgType = requestMap.get("MsgType");
            // 文本消息内容
            String content = requestMap.get("Content");
            // 事件类型
            String event = requestMap.get("Event");
            log.info("fromUserName=" + fromUserName + ",toUserName=" + toUserName + ",msgType=" + msgType + ",content=" + content + ",event=" + event);
            StringBuffer contentMessage = new StringBuffer();
            if (msgType.equals("event")) {
   
                contentMessage.append("欢迎进入XXX客服咨询!人工服务请输入0").append("\n");
                log.info(sendCustomerTextMessage(fromUserName, contentMessage.toString(), wxXCXTempSendUtil.getToken()));
            } else if (msgType.equals("text")) {
   //文本消息
                if (content.equals("0")) {
   
                    return switchCustomerService(fromUserName, toUserName);
                } else {
   
                    contentMessage.append("欢迎进入XXX客服咨询!人工服务请输入0").append("\n");
                    log.info(sendCustomerTextMessage(fromUserName, contentMessage.toString(), wxXCXTempSendUtil.getToken()));
                }
            } else {
   
                contentMessage.append("欢迎进入XXX客服咨询!人工服务请输入0").append("\n");
                log.info(sendCustomerTextMessage(fromUserName, contentMessage.toString(), wxXCXTempSendUtil.getToken()));
            }
        } catch (Exception e) {
   
            log.info("错误信息打印>>>>>>>>>>>" + e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

switchCustomerService()方法:
小程序对接人工服务的返回固定数据结构

 /**
     * 人工服务
     *
     * @param fromUserName
     * @param toUserName
     * @param requestMap
     * @return
     */
    public String switchCustomerService(String fromUserName, String toUserName) {
   
        String returnXml = "<xml>\n" +
                "    <ToUserName><![CDATA[" + fromUserName + "]]></ToUserName>\n" +
                "    <FromUserName><![CDATA[" + toUserName + "]]></FromUserName>\n" +
                "    <CreateTime>" + System.currentTimeMillis() / 1000 + "</CreateTime>\n" +
                "    <MsgType><![CDATA[transfer_customer_service]]></MsgType>\n" +
                "</xml>";
        log.info("人工服务result>>>>>>>" + returnXml);
        return returnXml;
    }

sendCustomerTextMessage()方法:
文本消息返回固定数据结构

/**
     * 文本消息
     *
     * @param openid
     * @param text
     * @param accessToken
     * @return
     */
    private static String SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/custom/send";//固定发送地址
     
    public String sendCustomerTextMessage(String openid, String text, String accessToken) throws IOException {
   
        HashMap<String, Object> map_content = new HashMap<>();
        map_content.put("content", text);
        HashMap<String, Object> map = new HashMap<>();
        map.put("touser", openid);
        map.put("msgtype", "text");
        map.put("text", map_content);
        String content = JSON.toJSONString(map);
        log.info("文本消息content" + content);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity httpEntity 
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值