This article mainly introduces the relevant information on Java development of WeChat public accounts to receive and passively reply to ordinary messages. Friends in need can refer to the following
After finishing the previous article, how to access WeChat public accounts, this article will talk about it. The most basic function of WeChat official account: receiving and replying 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.PictureMessage
3.Voice message
4.VideoMessage
5.Small video message
6. Geolocation message
7. Link message (passive reply message)
Ordinary messages for passive reply include:
1.Reply Text message
2.Reply to picture message
3.Reply to voice message
4.Reply to video message
5.Reply to music message
6.Reply to picture and text message
In factReceive messages and passively reply to messages. These two actions are inseparable. This is originally an interactive scene. Generally speaking, the public account will give corresponding replies by analyzing the messages received. . Of course, some special businesses cannot be ruled out.
How to receive messages
The xml format of the 7 messages to be received is not listed here. Please check the official documentation for 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.
Seeing this, it is actually very clear. The process of receiving messages is actually the process of obtaining the xml of the post request, and then analyzing the xml. The entrance for post requests is still the address where the WeChat official account is accessed previously. All requests for the entire official account will go through this entrance, except that when accessing, it is a get request, and in other cases, it is a post request. Dom4j is used to process xml. The xml processing code is as follows. Just call the parseXml method in the post method of servlet:
public static Map parseXml(HttpServletRequest request) throws Exception { // 将解析结果存储在HashMap中 Map map = new HashMap(); // 从request中取得输入流 InputStream inputStream = request.getInputStream(); /* * 读取request的body内容 此方法会导致流读取问题 Premature end of file. Nested exception: * Premature end of file String requestBody = * inputStream2String(inputStream); System.out.println(requestBody); */ // 读取输入流 SAXReader reader = new SAXReader(); Document document = reader.read(inputStream); // 得到xml根元素 Element root = document.getRootElement(); // 得到根元素的所有子节点 List<Element> elementList = root.elements(); // 遍历所有子节点 for (Element e : elementList) map.put(e.getName(), e.getText()); // 释放资源 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(); }
Below I will demonstrate the construction of a reply message based on such a logic. When receiving the text message "text", reply to the text message; when receiving "picture", reply to the picture message; When "voice" is received, reply to the voice message; when "video" is received, the video message is replied; when "music" is received, the music message is replied; when "image and text" are received, the image and text message are replied.
Take the reply text message as an explanation:
<xml> <ToUserName><![CDATA[发消息的人,即订阅者]]></ToUserName> <FromUserName><![CDATA[微信公众号本身]]></FromUserName> <CreateTime>消息创建时间(整形)</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[消息内容]]></Content> </xml>
<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>
CreateTime is the
of the message sent. 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 this type of xml format content. The formats are similar, except that the music, video, voice, and graphic formats are slightly different from the xml content constructed by text messages. A little more complicated. Please refer to the official documentation for details. I won’t go into details here, I believe you will understand it at a glance.
The above is the detailed content of Detailed introduction to using Java to develop WeChat public accounts to receive and passively reply to ordinary messages. For more information, please follow other related articles on the PHP Chinese website!