WeChat public account development tutorial 5th-receiving and responding to various messages

Source: Internet
Author: User

In the previous article, we have encapsulated messages and related operations in public platform interfaces. This section describes how to receive and respond to messages sent by servers.

 

Specify where to receive messages

You can see from the public platform interface message guide that when a user sends a message to a public account, the server will submit the message to the URL we entered in the interface configuration information through the POST method, however, we need to receive, process, and respond to messages in the doPost method of the request processing class CoreServlet pointed to by the URL.

 

Receive, process, and respond to messages

Let's take a look at the complete code of CoreServlet that I have written:

Package org. liufeng. course. servlet; import java. io. IOException; import java. io. printWriter; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import org. liufeng. course. service. coreService; import org. liufeng. course. util. signUtil;/*** core request processing class ** @ author liufeng * @ date 2013-05-18 */public class CoreServlet extends HttpServlet {private static final long serialVersionUID = 4440739483644821986L; /*** confirm that the request is from the server */public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// encrypted signature String signature = request. getParameter ("signature"); // timestamp String timestamp = request. getParameter ("timestamp"); // random number String nonce = request. getParameter ("nonce"); // random String echostr = request. getParameter ("echostr"); PrintWriter out = response. getWriter (); // verify the request by verifying signature. if the request is verified successfully, echostr is returned as is, indicating that the access is successful. Otherwise, the access fails if (SignUtil. checkSignature (signature, timestamp, nonce) {out. print (echostr);} out. close (); out = null;}/*** process the message sent from the server */public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// set request and response encoding to UTF-8 (to prevent Chinese garbled) request. setCharacterEncoding ("UTF-8"); response. setCharacterEncoding ("UTF-8"); // call the core service class to receive and process messages String respMessage = CoreService. processRequest (request); // response Message PrintWriter out = response. getWriter (); out. print (respMessage); out. close ();}}
 

Code Description:

1) Row 3: Call the MessageUtil tool class to parse the messages in xml format and put the parsed results in HashMap;

2) 32 ~ Row 36: retrieve the fields in the message from HashMap;

3) lines 39-44 and 84: assemble the text message objects to be returned;

4) 47 ~ Row 82: demonstrate how to receive various types of messages sent and determine which type of messages are sent based on MsgType;

5) row 85: Call MessageUtil to convert the text message object TextMessage to a string in xml format;

 

Event push (follow, cancel follow, and click menu)

It is easy to understand the determination of message types, such as text messages, image messages, geographic location messages, link messages, and voice messages, many new contacts do not know how to use event push messages or how to determine the messages that users are interested in. Let's take a look at event push. It is a description of event push in the official message interface document:

Here, we only need to care about two parameters: MsgType and Event. When MsgType = event, it indicates that this is an Event push message, and event indicates the Event type, including subscription, unsubscription, and custom menu click events. That is to say, whether the user pays attention to the public account, cancels the public account, or uses the public account menu, the server will send us a message of MsgType = event, the value of the Event is used to determine whether the message is followed, removed, or clicked on the menu. (Differentiate between Event and event)

 

Summary of five consecutive tutorials

After five explanations, the development mode, interface configuration, message-related tool class encapsulation, message receipt and response have been fully explained, and the complete source code has been attached, I believe that some of my friends who have a certain degree of Java foundation can understand the knowledge of the public platform development through a series of articles. Below I will post the complete structure of the current project for your reference:


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.