Tutorial on WeChat public platform development (2) Basic Principles and message interfaces, basic principles of the public

Source: Internet
Author: User
Tags openid sha1 encryption

Public platform development tutorial (ii) basic principles and message interface summary, basic principles of the public

I. Basic Principles

Before you start, you may be very interested in this, but you are quite confused. Is it complicated? Difficult to learn?

In fact, it is quite simple. To eliminate your concerns, I first briefly introduced the basic principles of the public platform.

The server is equivalent to a forwarding server. A terminal (such as a mobile phone or tablet) initiates a request to the server and forwards the request to the Custom Service (this is our specific implementation ).

After the service is processed, it is then volatile to the server, and then the server returns the specific response to the terminal.

The communication protocol is HTTP.

The data format is XML.

The specific process is shown in:

What we need to do is to respond to HTTP requests.

The specific request content is parsed according to the specific XML format. After processing, it must be returned according to the specific XML format.

We only need a simple implementation of HttpHandler.

Of course, the platform can also implement more complex services. For example, it can be used as an embedded browser. We can open the htm interface through a link and then implement our own logic.

Ii. Message interface (official documentation)

Message application interface

Click Apply and enter the url and token. You can enter the token as needed to generate a signature.

Website access

After a public platform user submits information, the server sends a GET request to the URL filled in with four parameters:

Parameters Description
Signature Encrypted Signature
Timestamp Timestamp
Nonce Random Number
Echostr Random string

The developer verifies the request by verifying signature (The following is a verification method ). If you confirm that the GET request is from the server, the echostr parameter is returned as is, and the access takes effect. Otherwise, the access fails.

Signature combines the token parameter entered by the developer with the timestamp parameter and nonce parameter in the request.
Encryption/verification process:

1. Sort the tokens, timestamp, and nonce in Lexicographic Order.

2. splice the three parameter strings into one string for sha1 Encryption

3. The encrypted string obtained by the developer can be compared with signature to identify that the request comes from

Message push

When a common user sends a message to a public account, the server will POST the message to the entered URL. The structure is as follows:

Text message

 <xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName>  <CreateTime>1348831860</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[this is a test]]></Content> <MsgId>1234567890123456</MsgId> </xml>

Parameters Description
ToUserName DeveloperNo.
FromUserName Sender account (one OpenID)
CreateTime Message creation time (integer)
MsgType Text
Content Text message content
MsgId Message id, 64-bit integer

Image message

 <xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>1348831860</CreateTime> <MsgType><![CDATA[image]]></MsgType> <PicUrl><![CDATA[this is a url]]></PicUrl> <MsgId>1234567890123456</MsgId> </xml>

Parameters Description
ToUserName DeveloperNo.
FromUserName Sender account (one OpenID)
CreateTime Message creation time (integer)
MsgType Image
PicUrl Image Link
MsgId Message id, 64-bit integer

Geographic location message

<Xml> <ToUserName> <! [CDATA [toUser]> </ToUserName> <FromUserName> <! [CDATA [fromUser]> </FromUserName> <CreateTime> 1351776360 </CreateTime> <MsgType> <! </MsgType> <Location_X> 23.134521 </Location_X> <Location_Y> 113.358803 </Location_Y> <Scale> 20 </Scale> <Label> <! [CDATA [location information]> </Label> <MsgId> 1234567890123456 </MsgId> </xml>

Parameters Description
ToUserName DeveloperNo.
FromUserName Sender account (one OpenID)
CreateTime Message creation time (integer)
MsgType Location
Location_X Geographic location and latitude
Location_Y Geographic longitude
Scale Map zoom
Label Geographic location information
MsgId Message id, 64-bit integer

Link message

<Xml> <ToUserName> <! [CDATA [toUser]> </ToUserName> <FromUserName> <! [CDATA [fromUser]> </FromUserName> <CreateTime> 1351776360 </CreateTime> <MsgType> <! [CDATA [link]> </MsgType> <Title> <! [CDATA [official website of the public platform]> </Title> <Description> <! [CDATA [official website of the public platform]> </Description> <Url> <! </Url> <MsgId> 1234567890123456 </MsgId> </xml>

Parameters Description
ToUserName Recipient number
FromUserName Sender id. If it is a common user, it is an OpenID.
CreateTime Message Creation Time
MsgType Message Type, link
Title Message Title
Description Message description
Url Message Link
MsgId Message id, 64-bit integer

Event push

Event push only supports version 4.5. Currently, you can enable event push, follow, and cancel follow event push for custom menu interfaces. Other functions will be available soon.

<xml><ToUserName><![CDATA[toUser]]></ToUserName><FromUserName><![CDATA[FromUser]]></FromUserName><CreateTime>123456789</CreateTime><MsgType><![CDATA[event]]></MsgType><Event><![CDATA[EVENT]]></Event><EventKey><![CDATA[EVENTKEY]]></EventKey></xml>

Parameters Description
ToUserName Recipient number
FromUserName Sender id. If it is a common user, it is an OpenID.
CreateTime Message Creation Time
MsgType Message Type, event
Event Event Type: subscribe, unsubscribe, and CLICK)
EventKey Event KEY value, which corresponds to the KEY value in the custom menu interface

Message reply

For each POST request, the developer returns a specific xml structure in the response packet to respond to the message (now supports text, text, voice, video, music ).

If the server fails to receive the response within five seconds, the connection will be disconnected.

The xml structure of the reply is as follows:

Reply text message

 <xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>12345678</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[content]]></Content> </xml>

Parameters Description
ToUserName Recipient account (received OpenID)
FromUserName Developer ID
CreateTime Message Creation Time
MsgType Text
Content The content of the reply message. The length cannot exceed 2048 bytes.

Reply music message

 <xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>12345678</CreateTime> <MsgType><![CDATA[music]]></MsgType> <Music> <Title><![CDATA[TITLE]]></Title> <Description><![CDATA[DESCRIPTION]]></Description> <MusicUrl><![CDATA[MUSIC_Url]]></MusicUrl> <HQMusicUrl><![CDATA[HQ_MUSIC_Url]]></HQMusicUrl> </Music> </xml>

Parameters Description
ToUserName Recipient account (received OpenID)
FromUserName Developer ID
CreateTime Message Creation Time
MsgType Music
MusicUrl Music Link
HQMusicUrl High-quality music link, which is preferred for playing music in WIFI Environments

Reply to text message

 <xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>12345678</CreateTime> <MsgType><![CDATA[news]]></MsgType> <ArticleCount>2</ArticleCount> <Articles> <item> <Title><![CDATA[title1]]></Title>  <Description><![CDATA[description1]]></Description> <PicUrl><![CDATA[picurl]]></PicUrl> <Url><![CDATA[url]]></Url> </item> <item> <Title><![CDATA[title]]></Title> <Description><![CDATA[description]]></Description> <PicUrl><![CDATA[picurl]]></PicUrl> <Url><![CDATA[url]]></Url> </item> </Articles> </xml>

Parameters Description
ToUserName Recipient account (received OpenID)
FromUserName Developer ID
CreateTime Message Creation Time
MsgType News
ArticleCount Number of text and text messages, up to 10
Articles Multiple text messages. The first item is a large image by default.
Title Text message title
Description Text Message description
PicUrl Image links. JPG and PNG formats are supported. The larger image size is 640*320, and the smaller image size is 80*80.
Url Click the text message jump Link

Official interface documentation: http://mp.weixin.qq.com/wiki/index.php? Title = % E6 % B6 % 88% E6 % 81% AF % E6 % 8E % A5 % E5 % 8F % A3 % E6 % 8C % 87% E5 % 8D % 97

Notes

1. the user's OpenID is a fixed and unique string for a public number.

2. Use port 80

Please note: we will explain the specific development process in the future.

3. Message class diagram

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.