The previous article introduced that we will enable the callback mode. After starting the callback mode, we will implement the chat function. Usually, you can send text messages, voice, pictures, videos, etc. by using WeChat chat. Only some of these functions are implemented here to share with everyone.
1. Establish a connection with WeChat enterprise account
1. Enterprise applications call the interface provided by the enterprise account, manage or query the resources managed by the enterprise account background, or send messages to members, etc., which is called the active call mode.
2. The enterprise account pushes messages sent by the user or events triggered by the user to the enterprise application, and is processed by the enterprise application, which is referred to as the callback mode.
3. Users read the H5 page sent by the enterprise application in WeChat. This page can call the native interface provided by WeChat and use the terminal capabilities open to WeChat, which is called the JSAPI model.
This is written in the development document of WeChat enterprise account, but we generally use the first two methods .
In fact, active calls and callbacks are relative. This is from the perspective of the WeChat server. The WeChat server sends messages to the WeChat client through a corporate account, which is an active call. The WeChat client actively sends messages to call the WeChat server is a passive call (callback) compared to the server.
2. Passively call the data format processed by WeChat server
1. Main tune: The server sends data in json format to the WeChat client, and the data does not need to be encrypted.
2. Callback: The messages sent by the WeChat client require AES encryption, and the messages accepted by the server are in xml format.
These two are represented by a picture as follows:
3. Chat principle diagram
First, the WeChat client sends a message to the server for processing. After the data is transmitted to the third-party server in XML format, the third-party server converts the data to json format, transmits it to the WeChat server, and sends it to the client.
4. Code implementation
With the above principle basis, the following is the code section
1.servlet
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); String msg_signature = request.getParameter("msg_signature"); String timestamp = request.getParameter("timestamp"); String nonce = request.getParameter("nonce"); InputStream inputStream = request.getInputStream(); String postData = IOUtils.toString(inputStream, "UTF-8"); System.out.println(postData); String msg = ""; WXBizMsgCrypt wxcpt = null; try { wxcpt = new WXBizMsgCrypt(sToken, sEncodingAESKey, sCorpID); msg = wxcpt.DecryptMsg(msg_signature, timestamp, nonce, postData); } catch (AesException e) { e.printStackTrace(); } System.out.println("msg=" + msg); //Get interface access credentials String accessToken = CommonUtil.getToken("wxe510946434680dab", "eWTaho766INvp4e1MCsz1mHYuT2DAleb62REQ3vsFizhY4vtmwZpKweuxUVh33G0").getAccessToken(); try { @SuppressWarnings("unused") boolean flag = ChatService1.sendMessage(accessToken,msg); } catch (Exception e) { e.printStackTrace(); }} 2.service
public class ChatService1 { public static boolean sendMessage(String accessToken,String msg) throws Exception{ boolean flag = false; Map<String, String> requestMap = MessageUtil.parseXml(msg); // Sender String fromUserName = requestMap.get("FromUserName"); // Message type String msgType = requestMap.get("MsgType"); if(msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)){//If it is a text customer service message String Content = requestMap.get("Content");//Message content of text message//Assemble text customer service message, parameter 1: user id; Parameter 2: content sent jsonMsg = AdvancedUtil.makeTextCustomMessage("lishehe|zhisheng|zhangwenyuan|lixinjiang", Content); }else if(msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_IMAGE)){//The sent is a picture String mediaId = requestMap.get("MediaId"); jsonMsg = AdvancedUtil.makeImageCustomMessage("lishhe|zhisheng|zhangwenyuan|lixinjiang", mediaId); }else if(msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_VOICE)){ String mediaId = requestMap.get("MediaId"); jsonMsg = AdvancedUtil.makeVoiceCustomMessage("lishhe|zhisheng|zhangwenyuan|lixinjiang", mediaId); } flag = AdvancedUtil.sendCustomMessage(accessToken, jsonMsg); return flag; }} 3. Tools
public class AdvancedUtil { private static Logger log = LoggerFactory.getLogger(AdvancedUtil.class); /** * Assembly and send text message* @return */ public static String makeTextCustomMessage(String openId, String content) { content = content.replace("/"", "///"); String jsonMsg = "{/"touser/":/"%s/",/"msgtype/":/"text/",/"agentid/":/"%s/",/"text/":{/"content/":/"%s/"}}"; return String.format(jsonMsg, openId, 14,content); } /** * Assembly and send image messages* * @return */ public static String makeImageCustomMessage(String openId, String mediaId) { String jsonMsg = "{/"touser/":/"%s/",/"msgtype/":/"image/",/"agentid/":/"%s/",/"image/":{/"media_id/":/"%s/"}}"; return String.format(jsonMsg, openId,14, mediaId); } /** * Assembly and send voice messages* * @return */ public static String makeVoiceCustomMessage(String openId, String mediaId) { String jsonMsg = "{/"touser/":/"%s/",/"msgtype/":/"voice/",/"agentid/":/"%s/",/"voice/":{/"media_id/":/"%s/"}}"; return String.format(jsonMsg, openId,14, mediaId); } 4. Summary
This realizes the reception and push of messages. The third-party server first decrypts and parses the received XML data, obtains the message type from it, and then encapsulates the message to be sent and converts it to json format, and transmits it to the WeChat server and sends it to the client.
This article has been compiled into "Android WeChat Development Tutorial Summary", and "Java WeChat Development Tutorial Summary" welcomes everyone to learn and read.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.