android微信小程序自动填表_微信小程序自动回复用户消息
最近用java做了一個實現(xiàn)在微信小程序內(nèi)根據(jù)用戶發(fā)送的消息內(nèi)容回復用不通的消息功能,相當于一個自動回復的客服消息,效果圖如下:
??
?
當用戶在小程序輸入框中輸入內(nèi)容或其他操作時,后臺根據(jù)用戶輸入的內(nèi)容動態(tài)給用戶回復,微信的參考文檔為:
https://developers.weixin.qq.com/community/develop/article/doc/00066a67324e70bdf0981381b5c813 https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/customer-message/customerServiceMessage.send.html 微信的垃圾文檔我就懶得噴了啊,按它這個文檔做簡直是一團亂麻
整個流程是這樣的我們可以在微信小程序的開發(fā)者后臺設置消息的回調(diào)地址,當用戶進入小程序的客服頁面,或在客服頁面的輸入框中輸入內(nèi)容時,微信小程序就會根據(jù)我們配置的消息回調(diào)地址把這條消息或這個事件回調(diào)給我們的服務器,我們可以進行相關處理,并給用戶回復消息如上圖顯示的那些我輸入 22 ,給我回復一個鏈接地址或其他東西,可以在回調(diào)中配置;
然后遇到的幾個問題說一下:
1、回調(diào)地址解析:在微信后臺配置回調(diào)地址時它會先發(fā)一個get請求測試地址是否可用,然后用戶在發(fā)消息的時候會發(fā)post請求把用戶消息發(fā)過來:如下
@Override public void autoResponse(HttpServletRequest req, HttpServletResponse resp) throws Exception { switch (req.getMethod().toUpperCase()){ case GET: doGet(req, resp); break; case POST: doPost(req, resp); break; default: throw new IllegalStateException("Unexpected value: " + req.getMethod()); } } //回復get請求,說明地址可用
private void doGet(HttpServletRequest req, HttpServletResponse resp) throws Exception { // 將請求、響應的編碼均設置為UTF-8(防止中文亂碼) req.setCharacterEncoding("UTF-8"); resp.setCharacterEncoding("UTF-8"); String signature = req.getParameter("signature"); String timestamp = req.getParameter("timestamp"); String nonce = req.getParameter("nonce"); String echostr = req.getParameter("echostr"); String sortString = SignUtil.sort(config.getRespToken(), timestamp, nonce); String mySignature = SignUtil.sha1(sortString); if (mySignature != null && mySignature != "" && mySignature.equals(signature)) { resp.getWriter().write(echostr); } else { log.error("簽名校驗失敗."); } } //回復post請求
private void doPost(HttpServletRequest req, HttpServletResponse resp)throws Exception { try{ resp.setCharacterEncoding("UTF-8"); resp.setCharacterEncoding("UTF-8"); String line = null; StringBuffer xmlStr = new StringBuffer(); BufferedReader reader = req.getReader(); while ((line = reader.readLine()) != null) { http://log.info(line); xmlStr.append(line); } Map map = XmlUtil.xmlToMap(xmlStr.toString()); CustomerMsgReq customerMsgReq = WXAutoRespReqUtil.buildResponseMessage(map, config.getAppletsAppId(), config.getRespHref(), config.getRespText()); http://log.info("send custom message param :{}", customerMsgReq); if(customerMsgReq == null){ return; } String accessToken = getCacheAccessToken(); JSONObject sendResult = wxAppletsRemoting.messageCustomSend(accessToken, customerMsgReq); http://log.info("send custom message result :{}", sendResult); if(sendResult.containsKey(("errcode")) && sendResult.getString("errcode").equals("0.0")){ return; } log.error("access token may expire", sendResult); accessToken = getRealAccessToken(); sendResult = wxAppletsRemoting.messageCustomSend(accessToken, customerMsgReq); http://log.info("resend custom message result :{}", sendResult); }finally { resp.getWriter().println("success"); }
這里有一個問題,根據(jù)文檔描述應該可以直接在回調(diào)接口中回復用戶消息,如下這樣操作,但是實際開發(fā)中,我測試這樣回復用戶沒有收到消息,也不報錯,不知道是我理解有問題還是哪里配置有問題,歡迎各位小伙伴指正
private void doPost(HttpServletRequest req, HttpServletResponse resp)throws Exception { try{ resp.setCharacterEncoding("UTF-8"); resp.setCharacterEncoding("UTF-8"); String line = null; StringBuffer xmlStr = new StringBuffer(); BufferedReader reader = req.getReader(); while ((line = reader.readLine()) != null) { http://log.info(line); xmlStr.append(line); } Map map = XmlUtil.xmlToMap(xmlStr.toString()); //鏈接內(nèi)容 String msgText =""+text+"";ZZ //發(fā)送方帳號 String fromUserName = map.get("FromUserName"); // 開發(fā)者微信號 String toUserName = map.get("ToUserName");
String respXml = String.format( "<xml>" +"<ToUserName><![CDATA[%s]]></ToUserName>" +"<FromUserName><![CDATA[%s]]></FromUserName>" +"<CreateTime>%s</CreateTime>" +"<MsgType><![CDATA[text]]></MsgType>" +"<Content><![CDATA[%s]]></Content>" +"</xml>", fromUserName, toUserName, getMessageCreateTime(), msgText);resp.setContentType("application/xml; charset=utf-8");resp.getWriter().println(respXml);}finally {
}
因為不能直接回復,需要調(diào)微信
POST https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN 這個接口給用戶回復,參考微信文檔
https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/customer-message/customerServiceMessage.send.html
因此又牽扯到獲取微信小程序appid,secret 然后根據(jù)appid,secret獲取access_token 還有access_token的緩存等一大堆問題,我就不細說了,涉及到的代碼已提交到碼云,地址為:
https://gitee.com/tianji_luhaichuan/pay/tree/master/wxshare-sdk-java
有問題歡迎加微信交流
?
??
補充一句,加微信別老是您您您的,都是打工人,不必這么客氣,我也才18啊哈哈
總結
以上是生活随笔為你收集整理的android微信小程序自动填表_微信小程序自动回复用户消息的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: byte数组转file不写入磁盘_Lin
- 下一篇: db2分页查询语句优化_数据量很大,分页