Examples of encapsulation tools attached to the message interface development tutorial of WeChat public platform (1/3)

Source: Internet
Author: User

To do this, you must first sharpen your tools! This article describes how to encapsulate messages and message-related operations defined by the public platform into a tool class for later use. It must be clarified that the message is actually sent to your public account by the user, and the message is first received by the platform, the platform then forwards the message to the URL address specified in the Development Mode Interface configuration.

Public platform message interface

To receive messages sent by the platform, you must first familiarize yourself with the message interface section of the public platform API. Click here to enter the message interface guide section, as shown in:

On the left side, you can see that the public platform currently has three open interfaces: message interface, common interface, and custom interface. Menu interface. Common interface and custom The menu interface can be called only when it is qualified for the internal test, and the application for the internal test qualification has been closed. We only have to look forward to opening the menu interface to the public one day in the future, 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. We only need to pay attention to "4 message push" and "5 message reply" for message receipt and response.

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 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 the methods in which the messages are forwarded by the server 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. This is described in the Interface Guide of the public platform:

 


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 ID) 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 ID
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;
}
}

1 2 3

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.