微信客服自动回复图片?
在调试微信客服自动回复图片时,所有都是按照文档要求开发,但是无法自动回复 首先:自动回复配置如下: [图片] 第二:验证加密(通过): [代码]boolean[代码] [代码]isGet = request.getMethod().toLowerCase().equals([代码][代码]"get"[代码][代码]);[代码][代码] [代码][代码]if[代码] [代码](isGet) {[代码][代码] [代码][代码]// 微信加密签名[代码][代码] [代码][代码]String signature = request.getParameter([代码][代码]"signature"[代码][代码]);[代码][代码] [代码][代码]// 时间戳[代码][代码] [代码][代码]String timestamp = request.getParameter([代码][代码]"timestamp"[代码][代码]);[代码][代码] [代码][代码]// 随机数[代码][代码] [代码][代码]String nonce = request.getParameter([代码][代码]"nonce"[代码][代码]);[代码][代码] [代码][代码]// 随机字符串[代码][代码] [代码][代码]String echostr = request.getParameter([代码][代码]"echostr"[代码][代码]);[代码] [代码] [代码][代码]// 通过检验signature对请求进行校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败[代码][代码] [代码][代码]if[代码] [代码](signature != [代码][代码]null[代码] [代码]&& WeChatUtil.checkSignature(signature, timestamp, nonce)) {[代码][代码] [代码][代码]logger.debug([代码][代码]"signature == "[代码] [代码]+ signature);[代码][代码] [代码][代码]try[代码] [代码]{[代码][代码] [代码][代码]PrintWriter print = response.getWriter();[代码][代码] [代码][代码]print.write(echostr);[代码][代码] [代码][代码]print.flush();[代码][代码] [代码][代码]} [代码][代码]catch[代码] [代码](IOException e) {[代码][代码] [代码][代码]logger.error([代码][代码]"微信消息接收和token验证异常"[代码][代码], e);[代码][代码] [代码][代码]}[代码][代码] [代码][代码]}[代码][代码] [代码][代码]}[代码]第三:自动回复 自动回复先上传临时素材到回信服务,并获取mediaId [代码]//先上传临时素材到微信服务[代码] [代码] [代码][代码]String mediaId = uploadFileToWeChat(WeChatUtil.REQ_MESSAGE_TYPE_IMAGE);[代码] [代码] [代码][代码]/**[代码][代码] [代码][代码]* 上传图片到微信服务[代码][代码] [代码][代码]*[代码][代码] [代码][代码]* @param[代码][代码] [代码][代码]* @return[代码][代码] [代码][代码]*/[代码][代码] [代码][代码]@Override[代码][代码] [代码][代码]public[代码] [代码]String uploadFileToWeChat(String type) {[代码][代码] [代码][代码]String accessToken = weiXinToken.getAccessToken();[代码][代码] [代码][代码]System.out.println([代码][代码]"token == "[代码] [代码]+ accessToken);[代码][代码] [代码][代码]RestTemplate restTemplate = [代码][代码]new[代码] [代码]RestTemplate();[代码][代码] [代码][代码]HttpHeaders headers = [代码][代码]new[代码] [代码]HttpHeaders();[代码][代码] [代码][代码]headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);[代码][代码] [代码][代码]File file = [代码][代码]new[代码] [代码]File(IMAGE_PATH);[代码][代码] [代码][代码]if[代码] [代码](!file.exists() || !file.isFile()) {[代码][代码] [代码][代码]return[代码] [代码]null[代码][代码];[代码][代码] [代码][代码]}[代码][代码] [代码][代码]StringBuilder sb = [代码][代码]new[代码] [代码]StringBuilder();[代码][代码] [代码][代码]sb.append(weixinImageUpload)[代码][代码] [代码][代码].append([代码][代码]"access_token="[代码][代码])[代码][代码] [代码][代码].append(accessToken)[代码][代码] [代码][代码].append([代码][代码]"&type="[代码][代码])[代码][代码] [代码][代码].append(type);[代码] [代码] [代码][代码]String mediaId = WeChatUtil.httpRequest(sb.toString(), file);[代码] [代码] [代码][代码]return[代码] [代码]mediaId;[代码][代码] [代码][代码]}[代码] 获取到mediaId后,然后自动回复开发 [代码]/**[代码][代码] [代码][代码]* 此处自动回复文本[代码][代码] [代码][代码]*/[代码][代码] [代码][代码]TextMessageResponseVo textMessageResponseVo = [代码][代码]new[代码] [代码]TextMessageResponseVo();[代码][代码] [代码][代码]textMessageResponseVo.setFromUserName(requestMessageMap.get([代码][代码]"ToUserName"[代码][代码]));[代码][代码] [代码][代码]textMessageResponseVo.setToUserName(requestMessageMap.get([代码][代码]"FromUserName"[代码][代码]));[代码][代码] [代码][代码]textMessageResponseVo.setMsgType([代码][代码]"text"[代码][代码]);[代码][代码] [代码][代码]textMessageResponseVo.setCreateTime(System.currentTimeMillis() / [代码][代码]1000[代码][代码]);[代码][代码] [代码][代码]textMessageResponseVo.setContent([代码][代码]"你好"[代码][代码]);[代码] [代码] [代码][代码]/**[代码][代码] [代码][代码]* 此处自动回复图片[代码][代码] [代码][代码]*/[代码][代码] [代码][代码]ImageMessageResponseVo imageMessageResponseVo = [代码][代码]new[代码] [代码]ImageMessageResponseVo();[代码][代码] [代码][代码]imageMessageResponseVo.setFromUserName(requestMessageMap.get([代码][代码]"FromUserName"[代码][代码]));[代码][代码] [代码][代码]imageMessageResponseVo.setToUserName(requestMessageMap.get([代码][代码]"ToUserName"[代码][代码]));[代码][代码] [代码][代码]imageMessageResponseVo.setMsgType([代码][代码]"image"[代码][代码]);[代码][代码] [代码][代码]imageMessageResponseVo.setCreateTime(System.currentTimeMillis() / [代码][代码]1000[代码][代码]);[代码][代码] [代码][代码]AutoAnswer autoAnswer = [代码][代码]new[代码] [代码]AutoAnswer();[代码][代码] [代码][代码]autoAnswer.setMediaId(mediaId);[代码][代码] [代码][代码]imageMessageResponseVo.setImage(autoAnswer);[代码][代码] [代码][代码]System.out.println([代码][代码]"textMessageResponseVo>>>>>>>>"[代码][代码]+imageMessageResponseVo.toString());[代码][代码] [代码][代码]String result = WeChatUtil.messageToXml(textMessageResponseVo);[代码][代码] [代码][代码]System.out.println([代码][代码]"result XML CDATA +++++++++++++++++++++++++"[代码] [代码]+ result);[代码][代码] [代码][代码]return[代码] [代码]result;[代码] [代码] [代码][代码]/**[代码][代码] [代码][代码]* 文本消息转换成xml[代码][代码] [代码][代码]*/[代码][代码] [代码][代码]public[代码] [代码]static[代码] [代码]String messageToXml(TextMessageResponseVo textMessageResponseVo) {[代码][代码] [代码][代码]xstream.alias([代码][代码]"xml"[代码][代码], textMessageResponseVo.getClass());[代码][代码] [代码][代码]return[代码] [代码]xstream.toXML(textMessageResponseVo);[代码] [代码] [代码][代码]}[代码] [代码] [代码][代码]/**[代码][代码] [代码][代码]* 图片[代码][代码] [代码][代码]* @param imageMessageResponseVo[代码][代码] [代码][代码]* @return[代码][代码] [代码][代码]*/[代码][代码] [代码][代码]public[代码] [代码]static[代码] [代码]String messageToXml(ImageMessageResponseVo imageMessageResponseVo) {[代码][代码] [代码][代码]xstream.alias([代码][代码]"xml"[代码][代码], imageMessageResponseVo.getClass());[代码][代码] [代码][代码]return[代码] [代码]xstream.toXML(imageMessageResponseVo);[代码] [代码] [代码][代码]}[代码] [代码] [代码][代码]/**[代码][代码] [代码][代码]* 扩展xstream使其支持CDATA[代码][代码] [代码][代码]*/[代码][代码] [代码][代码]private[代码] [代码]static[代码] [代码]XStream xstream = [代码][代码]new[代码] [代码]XStream([代码][代码]new[代码] [代码]XppDriver() {[代码][代码] [代码][代码]@Override[代码][代码] [代码][代码]public[代码] [代码]HierarchicalStreamWriter createWriter(Writer out) {[代码][代码] [代码][代码]return[代码] [代码]new[代码] [代码]PrettyPrintWriter(out) {[代码][代码] [代码][代码]// 对所有xml节点的转换都增加CDATA标记[代码][代码] [代码][代码]boolean[代码] [代码]cdata = [代码][代码]true[代码][代码];[代码] [代码] [代码][代码]@Override[代码][代码] [代码][代码]@SuppressWarnings[代码][代码]([代码][代码]"unchecked"[代码][代码])[代码][代码] [代码][代码]public[代码] [代码]void[代码] [代码]startNode(String name, Class clazz) {[代码][代码] [代码][代码]super[代码][代码].startNode(name, clazz);[代码][代码] [代码][代码]}[代码] [代码] [代码][代码]@Override[代码][代码] [代码][代码]protected[代码] [代码]void[代码] [代码]writeText(QuickWriter writer, String text) {[代码][代码] [代码][代码]if[代码] [代码](cdata) {[代码][代码] [代码][代码]writer.write([代码][代码]"<![CDATA["[代码][代码]);[代码][代码] [代码][代码]writer.write(text);[代码][代码] [代码][代码]writer.write([代码][代码]"]]>"[代码][代码]);[代码][代码] [代码][代码]} [代码][代码]else[代码] [代码]{[代码][代码] [代码][代码]writer.write(text);[代码][代码] [代码][代码]}[代码][代码] [代码][代码]}[代码][代码] [代码][代码]};[代码][代码] [代码][代码]}[代码][代码] [代码][代码]});[代码] 最后,响应微信服务的是一串String的xml [代码]//文本响应微信的xml[代码][代码]<xml>[代码][代码] [代码][代码]<ToUserName><![CDATA[onv1V4-zyxgliQlXbaBujrNT-ycs]]></ToUserName>[代码][代码] [代码][代码]<FromUserName><![CDATA[gh_dc0815388d0f]]></FromUserName>[代码][代码] [代码][代码]<CreateTime><![CDATA[[代码][代码]1570614951[代码][代码]]]></CreateTime>[代码][代码] [代码][代码]<MsgType><![CDATA[text]]></MsgType>[代码][代码] [代码][代码]<Content><![CDATA[你好]]></Content>[代码][代码]</xml>[代码][代码] [代码] [代码] [代码] [代码]//图片响应微信的xml[代码][代码] [代码] [代码]<xml>[代码] [代码] [代码][代码]<ToUserName><![CDATA[onv1V4-zyxgliQlXbaBujrNT-ycs]]></ToUserName>[代码] [代码] [代码][代码]<FromUserName><![CDATA[gh_dc0815388d0f]]></FromUserName>[代码] [代码] [代码][代码]<CreateTime><![CDATA[[代码][代码]1570670451[代码][代码]]]></CreateTime>[代码] [代码] [代码][代码]<MsgType><![CDATA[image]]></MsgType>[代码] [代码] [代码][代码]<Image>[代码] [代码] [代码][代码]<MediaId><![CDATA[ukM01FczEHwYUthTQ_Sf_6XNgI1_2lScqCFuIWOs8zQ8CGp0kgH98SS8B4SxdznU]]></MediaId>[代码] [代码] [代码][代码]</Image>[代码] [代码]</xml>[代码] [代码][代码] 但是最后没有收到微信自动回复,请求各位大佬指教 ;