In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to deal with news and events in the development of Wechat official account". The content is simple and clear. I hope it can help you solve your doubts. Now let the editor lead you to study and learn this article "how to deal with news and events in the development of Wechat official account".
1 Overview of messages (events)
When an ordinary Wechat user sends a message to the official account or the Wechat server pushes the event to the official account, the Wechat server sends the XML of the POST message (event) to the official account server URL filled in by the developer; the official account server then responds to the message.
1.1 the flow process of messages
In order to make it easier to distinguish, we call the message sent by the Wechat server to the official account server as the Request message; the message sent by the official account server to the Wechat server as the Response message; and the push event as a special request message.
The flow of request and response messages is shown in the following figure:
1.2 request message
There are many kinds of request messages, and we have created corresponding classes for each of them. The class hierarchy is shown in the following figure:
We can respond to some request messages, while others cannot, as shown in the following table:
Whether the message type can passively reply to the comment text × √
Picture x √
Sound × √
Video × unknown cannot receive video message. I wonder if I can passively reply to geographic location × √.
Link × √
Subscribe to √√
Unsubscribe to √ ×
Scan the QR code √ ×
Report geographical location √ ×
Click the menu to pull the message √√
Click the menu jump link √ ×
Click the menu to scan the code and push √ ×
Click the menu to scan the code and wait for a reply to √√.
Click on the menu system to send a picture √ unknown can not receive the system send a picture event Wechat server will send picture messages, can reply to click the menu to take photos or photo albums send pictures √ × Wechat server will send picture messages, can reply to click menu Wechat send pictures √ × Wechat server will send picture messages, can reply to click the menu to select geographical location √ × Wechat server will send geographic location messages, can reply to push mass message results √ ×
Push send template message result √ ×
1.3 response message
The class hierarchy of the response message is shown in the following figure:
2 verify the authenticity of the message
After the official account server receives the request from the Wechat server, the first thing is to verify the authenticity of the message.
The Utility.CheckSignature method is used to verify that the message signature is correct.
Examples are as follows:
/ / verify the validity of the message / return true; if the message is valid, otherwise return false. Private bool Validate (HttpContext context) {string username = RequestEx.TryGetQueryString ("username"); / / A username parameter has been added to the URL configured for the API, indicating which Wechat official 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);} verify the authenticity of the message
Verify the authenticity of the message
3 parsing messages
If the message signature is verified, we need to parse the message text in XML format into a request message object, and the RequestMessageHelper class is used to do this.
RequestMessageHelper helper = new RequestMessageHelper (context.Request); if (helper.Message! = null) {/ / message parsed successfully, process it}
After the message is parsed successfully, helper.Message is the message base class RequestBaseMessage. We can determine which kind of message (event) it is based on the attributes MsgType and Event, and convert it to the appropriate subtype. For example:
RequestBaseMessage bm=helper.Message;switch (bm.MsgType) {case RequestMessageTypeEnum.text: / / text message HandleTextMessage ((RequestTextMessage) bm); break; case RequestMessageTypeEnum.image: / / picture message HandleImageMessage ((RequestImageMessage) bm); break; / / handle other messages case RequestMessageTypeEnum.event: / / event RequestEventMessage ev= (RequestEventMessage) bm Switch (ev.Event) {case RequestEventTypeEnum.subscribe: / / subscribe to HandleSubscribeMessage ((RequestSubscribeMessage) ev); break; case RequestEventTypeEnum.unsubscribe: / / unsubscribe to HandleUnsubscribeMessage ((RequestUnsubscribeMessage) ev); break; / / handle other events} break Default: break;}
For details of parsing the message, please see the source code: http://git.oschina.net/xrwang2/xrwang.weixin.PublicAccount/blob/master/PublicAccount/RequestMessage/RequestMessageHelper.cs
4 passive reply message
After receiving the message (event) from the Wechat server, we can reply to the message directly (passively) within 5 seconds, or we can reply to the empty string first, and then reply to the customer service message within 48 hours.
First initialize the ResponseXxxMessage, and then use the ToXml method to get the content of the response message.
Examples of passive reply messages are as follows:
/ / process the POST request of Wechat / return the 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, return the response message / return the response message private ResponseBaseMessage HandleRequestMessage (RequestBaseMessage requestMessage) {ResponseTextMessage response = new ResponseTextMessage (requestMessage.FromUserName, requestMessage.ToUserName, DateTime.Now, string.Format ("automatic reply, the request content is as follows:\ r\ n {0}", requestMessage); return response } the above is all the contents of this article entitled "how to deal with messages and events in the development of Wechat official account". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.