The previous article talks about how to access the WeChat official account. This article talks about the most basic functions of the WeChat official account: the reception and reply to ordinary messages. Speaking of ordinary messages, what are ordinary messages defined by WeChat official accounts? The ordinary messages received mentioned in the WeChat developer documentation include the following categories :
1. Text message
2. Picture message
3. Voice message
4. Video message
5. Short video message
6. Geographic location message
7. Link message (passive reply message)
Regular messages that passively reply include:
1. Reply to text messages
2. Reply to the picture message
3. Reply to voice messages
4. Reply to video messages
5. Reply to music messages
6. Reply to text messages
In fact, the two actions of receiving messages and passive replying messages are inseparable. This is an interactive scenario. Generally speaking, the official account will give corresponding replies by analyzing the received messages. Of course, some special businesses cannot be ruled out.
How to receive messages
The xml format of the messages to be received in the 7 messages will not be listed here. Please go to the official document to view them, with specific format definitions and attribute descriptions. The format is very simple. The basic common attributes include ToUserName, FromUserName, CreateTime, MsgType, and MsgId, and each type has its own special attributes.
After seeing this, I actually understand that the process of receiving messages is actually the process of obtaining the xml requested by the post and then analyzing the xml. The entry for post request is still the address for access to the WeChat official account mentioned earlier. All requests for the entire official account will go to this entry, but it is a get request when accessing, and in other cases it is a post request. Dom4j is used to process xml. The xml processing code is as follows. You can call the parseXml method in the post method of the servlet:
public static Map parseXml(HttpServletRequest request) throws Exception {// Store the parsed result in HashMap Map = new HashMap();// Get the input stream from the request InputStream inputStream = request.getInputStream();/** Read the body content of the request This method will cause stream reading problems Premature end of file. Nested exception:* Premature end of file String requestBody =* inputStream2String(inputStream); System.out.println(requestBody);*/// Read the input stream SAXReader reader = new SAXReader();Document document = reader.read(inputStream);// Get the xml root element Element root = document.getRootElement();// Get all child nodes of the root element List<Element> elementList = root.elements();// Traversing all child nodes for (Element e : elementList)map.put(e.getName(), e.getText());// release resource inputStream.close();inputStream = null;return map;}private static String inputStream2String(InputStream is) throws IOException {ByteArrayOutputStream baos = new ByteArrayOutputStream();int i = -1;while ((i = is.read()) != -1) {baos.write(i);}return baos.toString();} How to passively reply to messages
Below I demonstrate the message constructing a reply based on such logic, receiving the text message "text" and replying to the text message; receiving the "picture" and replying to the picture message; receiving the "voice" and replying to the voice message; receiving the "video", replying to the video message; receiving the "music", replying to the music message; receiving the "picture" and replying to the picture message.
Reply to text messages as an explanation:
<xml><ToUserName><![CDATA[the person sending the message, i.e. the subscriber]]></ToUserName><FromUserName><![CDATA[WeChat Official Account itself]]></FromUserName><CreateTime>Message creation time (plastic surgery)</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[Message Content]]></Content></xml>
The first two attributes can be obtained from the received message, and the received message format is as follows:
<xml><ToUserName><![CDATA[toUser]]></ToUserName><FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>1348831860</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[this is a text]]></Content><MsgId>1234567890123456</MsgId></xml>
The ToUserName in the receiving message format is the FromUserName that reply to the message, and the FromUserName in the receiving message format is the ToUserName that reply to the message.
CreateTime is the timestamp sent by the message. MsgType is the message type and the text is text. Content is the message content.
The reply to each type of message is to construct the xml format content of this type. The formats are similar, but the music, video, voice, and graphic formats are slightly more complicated than the xml content constructed by text messages. For details, please refer to the official documentation. I won't go into details here, I believe you will understand it at a glance.