E企盈营销工具技术服务商 热线:4006-838-530

微信小程序爬坑日记(二)JAVA客服消息自动回复

E企盈直播平台营销卖货系统

背景说明 :用户在小程序内点击按钮,打开客服对话界面,自动发送活动二维码或根据关键词自动回复第一步去微信公众平台开启客服自动回复功能第二步查阅官方文档,找到小程序客服API文档[链接如下]https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/customer-message/customerServiceMessage.send.html根据文档描述,发送图片需要临时素材的form_id,所以首先得完成临时素材的上传获取到form_id临时素材上传代码    /**     * 微信小程序临时素材上传     *     * @param file     * @return     * @author 13     * @throws Exception     */    public String uploadFile(MultipartFile file) throws Exception {        String url = “https://api.weixin.qq.com/cgi-bin/media/upload?access_token=” + ACCESS_TOKEN + “&type=image”;        String result = null;        String fileName = file.getOriginalFilename();        URL urlObj = new URL(url);        HttpURLConnection con = (HttpURLConnection) urlObj.openConnection();        con.setRequestMethod(“POST”);        con.setDoInput(true);        con.setDoOutput(true);        con.setUseCaches(false);        // 设置请求头信息        con.setRequestProperty(“Connection”, “Keep-Alive”);        con.setRequestProperty(“Charset”, “UTF-8”);        // 设置边界        String BOUNDARY = “———-” + System.currentTimeMillis();        con.setRequestProperty(“Content-Type”, “multipart/form-data; boundary=” + BOUNDARY);        // 请求正文信息        // 第一部分:        StringBuilder sb = new StringBuilder();        sb.append(“–“); // 必须多两道线        sb.append(BOUNDARY);        sb.append(“r”);        sb.append(“Content-Disposition: form-data;name=”media”;filename=”” + fileName + “”r”);        sb.append(“Content-Type:application/octet-streamrr”);        byte[] head = sb.toString().getBytes(“utf-8”);        // 获得输出流        OutputStream out = new DataOutputStream(con.getOutputStream());        // 输出表头        out.write(head);        // 文件正文部分        // 把文件已流文件的方式 推入到url中        DataInputStream in = new DataInputStream(file.getInputStream());        int bytes = 0;        byte[] bufferOut = new byte[1024];        while ((bytes = in.read(bufferOut)) != -1) {            out.write(bufferOut, 0, bytes);        }        in.close();        // 结尾部分        byte[] foot = (“r–” + BOUNDARY + “–r”).getBytes(“utf-8”);// 定义最后数据分隔线        out.write(foot);        out.flush();        out.close();        StringBuffer buffer = new StringBuffer();        BufferedReader reader = null;        try {            // 定义BufferedReader输入流来读取URL的响应            reader = new BufferedReader(new InputStreamReader(con.getInputStream()));            String line = null;            while ((line = reader.readLine()) != null) {                buffer.append(line);            }            if (result == null) {                result = buffer.toString();            }        } catch (IOException e) {            System.out.println(“发送POST请求出现异常! {}”);            e.printStackTrace();            throw new IOException(“数据读取异常”);        } finally {            if (reader != null) {                reader.close();            }        }        // 获取到返回HTTP结果        Map<String, Object> map = JSONObject.parseObject(result, Map.class);        if (map.containsKey(“media_id”)) {            return map.get(“media_id”).toString();        }        System.out.println(“小程序上传临时素材出错,返回信息为:    ” + result);        return null;    }拿到form_id以后就可以进行客服消息自动回复了第三步,自动回复接口编写同一个接口,设置服务器地址微信会进行get请求校验服务器地址的可用性,而post接口则进行客服消息自动回复    //  开启时填写的秘钥    private static final String token=”XXXXXXXX”;    /**     * 微信小程序消息验证和配置     *     * @author 13     * @return     * @throws Exception     */    @RequestMapping(value = “/message”, method = RequestMethod.GET)    public String getProcess(HttpServletRequest request, HttpServletResponse response) throws Exception {        // 微信加密签名        String signature = request.getParameter(“signature”);        // 时间戳        String timestamp = request.getParameter(“timestamp”);        // 随机数        String nonce = request.getParameter(“nonce”);        // 随机字符串        String echostr = request.getParameter(“echostr”);        // 通过检验signature对请求进行校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败        if (this.checkSignature(signature, timestamp, nonce)) {            return echostr;        }        return null;    }  /**     * 微信小程序客服消息自动回复     *     * @author 13     * @return     * @throws Exception     */    @RequestMapping(value = “/message”, method = RequestMethod.POST)    public String replyMessage(@RequestBody Map<String, Object> msg, HttpServletRequest request, HttpServletResponse response) throws Exception {        return appletCommonService.replyMessage(msg);    }    private  boolean checkSignature(String signature, String timestamp,                                    String nonce) {        String[] arr = new String[] { token, timestamp, nonce };        // 将token、timestamp、nonce三个参数进行字典序排序        Arrays.sort(arr);        StringBuilder content = new StringBuilder();        for (int i = 0; i < arr.length; i++) {            content.append(arr[i]);        }        MessageDigest md = null;        String tmpStr = null;        try {            md = MessageDigest.getInstance(“SHA-1”);            // 将三个参数字符串拼接成一个字符串进行sha1加密            byte[] digest = md.digest(content.toString().getBytes());            tmpStr = byteToStr(digest);        } catch (Exception e) {            e.printStackTrace();        }        content = null;        // 将sha1加密后的字符串可与signature对比,标识该请求来源于微信        return tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false;    }    /**     * 将字节数组转换为十六进制字符串     * @author 13     * @param byteArray     * @return     */    private static String byteToStr(byte[] byteArray) {        String strDigest = “”;        for (int i = 0; i < byteArray.length; i++) {            strDigest += byteToHexStr(byteArray[i]);        }        return strDigest;    }    /**     * 将字节转换为十六进制字符串     * @author 13     * @param mByte     * @return     */    private static String byteToHexStr(byte mByte) {        char[] Digit = { ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘A’,                ‘B’, ‘C’

赞(0) 打赏
未经允许不得转载:E企盈小程序开发-热线:4006-838-530 » 微信小程序爬坑日记(二)JAVA客服消息自动回复
分享到: 更多 (0)
E企盈小程序直播营销卖货系统
E企盈直播平台营销卖货系统

评论 抢沙发

E企盈小程序开发

联系我们联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏