Java-based development of WeChat public accounts for receiving and passive Reply to common messages

Source: Internet
Author: User
This article mainly introduces information about receiving and passive Reply to common messages by Java developers, for more information about how to receive and passively Reply to common messages, see this article.

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 following common messages are received in the developer documentation::

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 reader = 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.
 
  
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:


 
  发消息的人,即订阅者
  
  公众号本身
  
  
   
Message creation time (integer)
  
  text
  
  消息内容
  
 

The first two attributes can be obtained from the received message. the format of the received message is as follows:


 
  toUser
  
  fromUser
   
  
   1348831860
  
  text
  
  this is a text
  
  
   1234567890123456
  
 

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.

The above is a detailed description of the use of Java to develop public accounts to receive and passively Reply to common messages. For more information, see other related articles on php Chinese network!

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.