WeChat public platform development

Source: Internet
Author: User

The public platform provides a great service model. This makes it easier for users to communicate with sellers and provides better Membership services. Message push and promotion information push. It also provides the custom menu function. From the programmer's perspective, let's look at how to develop:

 

Platform structure:

When a user uses a mobile phone to send a message:

User APP ()-> public platform-> website background developed by Alibaba Cloud (developed based on the rest api of the public platform)

When the background replies to a message based on the content sent by the user:

User APP () <-public platform <-website background developed by Alibaba Cloud (developed based on rest api of public platform)

 

Because it is based on development. What we need to do is to establish a website to receive "messages sent from the public platform and reply", while the public platform will send "messages" to users' mobile phones.

How to do it?

Prepare: define a Servlet. The get method processes "verify message Authenticity", and the POST method processes "Receive and reply message"

Step 1: Verify the message authenticity by referring to: Development Guide

To process the GET message, run the following code:

(Token = IllegalArgumentException ("the token to be verified is not specified" = Logger. getLogger ("pdwy" "log start" = request. getParameter ("signature" = request. getParameter ("nonce" = request. getParameter ("timestamp" = request. getParameter ("echostr" (= signature | "" 500 = (I = 0; I <arrs. length; I ++ =View Code

 

Step 2: Receive and reply to messages

"UTF-8" "UTF-8" String messageBody = "\ r \ n received the POST message:" + = (replayMsgStr! = ""View Code

We will receive some messages in xml format. After receiving the POST message, we need to parse it. How can we resolve it? The following is an example of parsing a text message:

String MsgType = "text" = "% s = % s", "ToUserName" "% s = % s", "FromUserName" "% s = % s ", "CreateTime" "% s = % s", "MsgType" "% s = % s", "Content" "% s = % s ", "MsgId" === InputSource ("UTF-8" = doc. getElementsByTagName ("xml" (nl! = & Nl. getLength ()> 0 = (Element) nl. item (0 = "ToUserName "). item (0 = "FromUserName "). item (0 = "CreateTime "). item (0 = rootElement. getElementsByTagName ("MsgType" 0 = rootElement. getElementsByTagName ("Content" 0 = rootElement. getElementsByTagName ("MsgId" 0 ====== "Error:" +View Code

Okay. We have completed the parsing steps. We parse the xml message into an entity, and then perform business processing based on different messages and different message content. When we receive "hello", let's reply "you" and so on. Example:

TextMessageHandler ("text" = TaskResponse response ===== TextMessageReply replayMsg === String DEFALUT_MESSAGE = "select your operation as prompted: \ r \ n "+" 1. text message example. \ R \ n "+" 2. company name. \ R \ n "+" 3. contact information. \ R \ n "+" 4. A text message with hyperlink is returned. \ R \ n "+" 5. Example of replying music message. \ R \ n "(" 2 ". equals (content) | content. contains ("company" = "Beijing ----- Development Co., Ltd.-Agricultural Resource Management Department" ("3 ". equals (content) | content. contains ("phone" = "our contact number is: 010-xxxxx" ("4" = "For more information, see: <a href = 'HTTP: // www.baidu.com '> details </a>"View Code

We have constructed a reply message. The last step for processing the POST message is to write "the content responding to the POST is the content of the message we Reply to", we usually construct a "reply message entity", convert the entity into a string in xml format, and write it to the response stream (HttpResponse ). example:

TextMessageReply String MsgType = "text" TextMessageReplyWriter. toXml (--------------------------- ==== doc. createElement ("xml" = doc. createElement ("ToUserName" = doc. createElement ("FromUserName" = doc. createElement ("CreateTime" = doc. createElement ("MsgType" = doc. createElement ("Content" = "UTF-8" =View Code

Write the xml reply content to the response stream to complete a message response.

 

However, there is still a lot to do. There are many other types of messages and event processing. We need to encapsulate different types of messages based on different messages and reply messages.

I wrote my own class library: weinxinFundation,

  • The basic functions are to receive and reply messages (text messages, music messages, etc)
  • Encapsulate class libraries for basic events (Focus, geography, scan QR code, etc.
  • Supports scalable message processors and custom message processing methods. Message Implementation Method processor logic and class library logic separation. This facilitates secondary development.
  • Message linked list. If a message is not processed, it is transmitted along the linked list until it is processed.

  

The demo source code developed using the weinxinFundation framework is provided. Here, you can decompile the source code of weinxinFundation. Click Download.

Currently, we do not provide free download of weinxinFundation source code.

 

 

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.