WeChat public platform Message Interface
To receive messages sent from the WeChat platform, you must first familiarize yourself with the message Interface section of the WeChat public platform API. Click here to enter the Message Interface Guide section, as shown in the following figure:
On the left side of the figure above, we can see that the WeChat public platform currently has three open interfaces: Message Interface, general interface, and custom interface.Menu interface. Common Interface and customThe menu interface can be called only when it is qualified for beta testing, and the application for beta testing is closed. We only hope that WeChat will be available to public users one day, therefore, users who are not qualified for beta testing do not need to waste time on these two interfaces. They only need to use the good news interface.
Message push and Reply
The following describes the message interfaces. To receive and respond to messages, you only need to pay attention to "4 message push" and "5 message reply" in the figure above.
Let's first understand what "message push" refers to in the interface. Click "4 message push ", you can see that "message push" in the interface refers to "when a common user sends a message to a public account, the WeChat server will POST the message to the entered URL ", this defines the types of messages that the user can send, the fields of the messages, and how the WeChat server forwards the messages to the background of our public account.
Message pushing defines five types of messages we will receive: text messages, image messages, geographic location messages, link messages, and event pushing, in fact, we can also receive voice messages, but we can't get the specific voice file (we need to test the qualification to get the voice file ).
The "message reply" in the interface defines the message type, message field, and message format that can be returned to the user. The Interface Guide of the WeChat public platform describes this as follows:
As mentioned above, there are five types of messages that can be replied to users, but currently there are only three types of messages that can be replied in the development mode: text messages, music messages, and text messages, however, voice messages and video messages can only be used in editing mode.
Message Encapsulation
The next step is to encapsulate the messages defined in message push (request) and message reply (response) and establish the corresponding Java class (Java is an object-oriented programming language, the following request message refers to the message defined in the message push, and the response message refers to the message defined in the message reply.
Request message base class
Extract all fields of the message defined in the message push and encapsulate them into a base class. These public fields include: ToUserName (developer Micro-signal) and FromUserName (sender account, OPEN_ID), CreateTime (message creation time), MsgType (message type), MsgId (message ID), encapsulate the base class org. liufeng. course. message. req. the BaseMessage code is as follows:
The code is as follows: |
Copy code |
Package org. liufeng. course. message. req; /** * Message Base Class (common user> public account) * * @ Author liufeng * @ Date 2013-05-19 */ Public class BaseMessage { // Developer's micro signal Private String ToUserName; // Sender account (one OpenID) Private String FromUserName; // Message creation time (integer) Private long CreateTime; // Message type (text/image/location/link) Private String MsgType; // Message id, 64-bit integer Private long MsgId; Public String getToUserName (){ Return ToUserName; } Public void setToUserName (String toUserName ){ ToUserName = toUserName; } Public String getFromUserName (){ Return FromUserName; } Public void setFromUserName (String fromUserName ){ FromUserName = fromUserName; } Public long getCreateTime (){ Return CreateTime; } Public void setCreateTime (long createTime ){ CreateTime = createTime; } Public String getMsgType (){ Return MsgType; } Public void setMsgType (String msgType ){ MsgType = msgType; } Public long getMsgId (){ Return MsgId; } Public void setMsgId (long msgId ){ MsgId = msgId; } } |