Home > WeChat Applet > WeChat Development > Detailed introduction to using Java to develop WeChat public accounts to receive and passively reply to ordinary messages

Detailed introduction to using Java to develop WeChat public accounts to receive and passively reply to ordinary messages

高洛峰
Release: 2017-03-15 17:56:34
Original
2301 people have browsed it

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();
}
Copy after login

How Passive reply message


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>
Copy after login

The first two attributes can be obtained from the received message. 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>
Copy after login

The ToUserName in the receiving message format is the FromUserName of the reply message, and the FromUserName in the receiving message format is the ToUserName of the reply message.


CreateTime is the

timestamp

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template