Using java to develop WeChat Public Accounts: Receiving and passive reply to common messages (3), java public

Source: Internet
Author: User

Develop public accounts using java: Receive and passively reply to common messages (3), java public

The previous article describes how to access the public account. This article describes the basic functions of the Public Account: Receiving and replying to common messages. When it comes to common messages, what is the common message defined by the public number? The common messages mentioned in the developer documentation include the following types:

1. Text message
2. Image message
3. voice messages
4. video messages
5. Small video messages
6. geographic location message
7. Link message (passive reply message)

Common passive reply messages include:

1. Reply to text messages
2. Reply to the image message
3. Reply to voice messages
4. Reply to the video message
5. Reply to music messages
6. Reply to text message

In fact, the two actions of receiving messages and passive reply messages are independent. This is an interactive scenario. Generally, the public account will give a corresponding response by analyzing the received messages. Of course, some special services cannot be ruled out.

How to receive messages

The xml format of the messages to be received in 7 is not listed here. Please refer to the official document for detailed format definitions and attribute descriptions. The format is simple. common attributes include ToUserName, FromUserName, CreateTime, MsgType, and MsgId. Each type has its own special attributes.

The process of receiving a message is actually the process of obtaining the xml of the post request and then analyzing the xml. The portal of the post request is also the address of the Public Account Access mentioned earlier. All requests of the whole public account will go through this portal, except that the access request is a get request and the post request in other cases. Dom4j is used for xml processing. The xml Processing code is as follows. You can call the parseXml method in the post method of servlet:

Public static Map parseXml (HttpServletRequest request) throws Exception {// store the parsing result in HashMap Map = new HashMap (); // retrieve the input stream InputStream inputStream = request from the request. getInputStream ();/** read the body content of the request. This method causes 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 re Ader = new SAXReader (); Document document = reader. read (inputStream); // get the xml root Element root = document. getRootElement (); // obtain the List of all child nodes of the root Element <Element> elementList = root. elements (); // traverse all subnodes for (Element e: elementList) map. put (e. getName (), e. getText (); // release the 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

The following Logic demonstrates how to construct a reply message, receive a text message, reply to a text message, receive an image message, and reply to an image message; receives "Voice", replies to voice messages, receives "video", replies to video messages, receives "Music", replies to music messages, receives "text", and replies to text messages.

Reply text message as description:

<Xml> <ToUserName> <! [CDATA [sender, subscriber]> </ToUserName> <FromUserName> <! [CDATA [Public Account]> </FromUserName> <CreateTime> message creation time (integer) </CreateTime> <MsgType> <! [CDATA [text]> </MsgType> <Content> <! [CDATA [message Content]> </Content> </xml>

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

In the received message format, the ToUserName is the FromUserName of the reply message, and the FromUserName in the received message format is the ToUserName of the reply message.

CreateTime is the timestamp of message sending. MsgType is the message type, and text is text. Content is the message Content.

The reply of each type of message is to construct the content of this type of xml format, the format is similar, however, the xml content of music, video, voice, and text is slightly more complex than that of text messages. For more information, see the official documentation. I will not repeat it here. I believe you will understand it at a glance.

For more information about receiving messages, see here.

Code updated to github

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.