. Net WeChat public account development messages and events

Source: Internet
Author: User
This article mainly introduces. net public account development messages and events this article describes how to handle messages and events in public account development, including: (1) message (event) Overview; (2) verifying message authenticity; (3) parse messages; (4) passively reply to messages; (5) send other messages.
1. message (event) Overview

When a common user sends a message to the public account or the server pushes an event to the public account, the server sends the XML packet of the POST message (event) to the URL of the public account server filled in by the developer; the public account server then responds to the message.
1.1 Message transfer process
To facilitate differentiation, we call the message sent from the server to the public account server as a Request message, and the message sent from the public account server to the server as a Response message; the push event is considered as a special request message.
The transfer process of request and response messages is shown in:

Example:

////// Verify the message validity /////////
 
  
If the message is valid, true is returned; otherwise, false is returned.
 Private bool Validate (HttpContext context) {string username = RequestEx. tryGetQueryString ("username"); // add the username parameter to the URL configured by the interface, indicating the public account AccountInfo account = AccountInfoCollection. getAccountInfo (username); if (account = null) return false; string token = account. token; string signature = RequestEx. tryGetQueryString ("signature"); string timestamp = RequestEx. tryGetQueryString ("timestamp"); string nonce = RequestEx. tryGetQueryString ("nonce"); if (string. isNullOrWhiteSpace (signature) | string. isNullOrWhiteSpace (timestamp) | string. isNullOrWhiteSpace (nonce) return false; return xrwang. weixin. publicAccount. utility. checkSignature (signature, token, timestamp, nonce);} verifies message authenticity

Verify message authenticity

3 parse messages

If the message signature passes verification, we need to parse the message text in XML format into a request message object, and the RequestMessageHelper class is used to complete this work.

RequestMessageHelper helper = new RequestMessageHelper (context. Request); if (helper. Message! = Null) {// The message is successfully parsed and processed}

After the Message is successfully parsed, helper. Message is the Message base class RequestBaseMessage. we can determine which Message (Event) is based on the MsgType attribute and Event, and convert it into a proper subtype. For example:

RequestBaseMessage bm = helper. message; switch (bm. msgType) {case RequestMessageTypeEnum. text: // text message HandleTextMessage (RequestTextMessage) bm); break; case RequestMessageTypeEnum. image: // image message HandleImageMessage (RequestImageMessage) bm); break; // process other messages in case RequestMessageTypeEnum. event: // event RequestEventMessage ev = (RequestEventMessage) bm; switch (ev. event) {case RequestEventTypeEnum. subscribe: // subscribe HandleSubscribeMessage (RequestSubscribeMessage) ev); break; case RequestEventTypeEnum. unsubscribe: // unsubscribe HandleUnsubscribeMessage (RequestUnsubscribeMessage) ev); break; // handle other events} break; default: break ;}

For details about parsing messages, see source code: http://git.oschina.net/xrwang2/xrwang.weixin.PublicAccount/blob/master/PublicAccount/RequestMessage/RequestMessageHelper.cs

4 passive reply message
After receiving a message (event) from the server, we can directly (passively) reply to the message within 5 seconds. Or, we can directly reply to an empty string first, then, reply to customer service within 48 hours.
Initialize ResponseXxxMessage and use the ToXml method to obtain the response message content.
An example of passive reply message is as follows:

////// POST request processed /////////
 
  
Return xml response
 Private string HandlePost (HttpContext context) {RequestMessageHelper helper = new RequestMessageHelper (context. Request); if (helper. Message! = Null) {ResponseBaseMessage responseMessage = HandleRequestMessage (helper. Message); return responseMessage. ToXml (helper. EncryptType);} else return string. Empty ;}////// Process the request message and return the response message //////
 
  
Return response message
 Private ResponseBaseMessage HandleRequestMessage (RequestBaseMessage requestMessage) {ResponseTextMessage response = new ResponseTextMessage (requestMessage. fromUserName, requestMessage. toUserName, DateTime. now, string. format ("automatic reply, request content: \ r \ n {0}", requestMessage); return response ;}


5. send other messages

In addition to passively replying to messages, we can also send customer service messages, send group messages, and send template messages, which will be included in subsequent articles.

The above is the details of the. net public account development message and event. For more information, see other related articles on php Chinese network!

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.