Home >WeChat Applet >WeChat Development >C# WeChat Development Series-Receive/Return Text Messages
4.0 Receiving/returning text messages
①Explanation of the principle of receiving/returning text messages
When ordinary WeChat users send messages to public accounts At this time, the WeChat server will send the XML data packet of the POST message to the URL filled in by the developer. Before starting development, read the WeChat public platform to receive ordinary messages WeChat development documentation, and start development after you have a certain understanding of WeChat's message processing mechanism ( WeChat Development Receiving Ordinary Message Development Document)
Notes:
1. Regarding retrying message duplication, it is recommended to use msgid to deduplicate messages.
2. If the WeChat server does not receive a response within five seconds, it will disconnect and re-initiate the request, retrying three times in total. If the server cannot guarantee to process and reply within five seconds, you can directly reply with an empty string. The WeChat server will not do anything with this and will not initiate a retry. For details, please see "Send a Message - Passive Reply to a Message".
3. In order to ensure higher security, developers can set up message encryption in the developer center on the official website of the public platform. After encryption is turned on, messages sent by users will be encrypted, and messages passively replied to users by official accounts also need to be encrypted (but developers sending messages to users through API calls such as customer service interfaces will not be affected). For detailed instructions on message encryption and decryption, please see "Message Encryption and Decryption Instructions".
The XML format for POST to the developer server is:
b2a0af5a8fd26276da50279a1c63a57a ea5d8177d19f22584533e5c37c389942eae09a64d076f96f3228ae9470271e996671a89dce89e879d9e9c6d81d03862b c5123754d1f4829fae4905e8abb602f9590cdcaa06dc33eb0992fff736103f4f42a4b8d57eb0afadcf16b7a02c69caaf 246311df1688542638dc52b54a1a4c871348831860e660f1169ff44ea75c5a982fcb1cde61 42815c2206ae835d7fd68cb4ae21e4dffa796850a1cf5d7bc01ca8cd7f8b83de698463fc03844fbe5a9caafaa1ebd0b1 ea63b4477034504a08070acf4e0b68b26a60ebcd9401786f5b0ee154aab8bf4caa91fa7c40b1cd973268e154dae1a50e 0d388664a03eec4d0f6697867adf11561234567890123456d62559defac6782605cccb28f6907157 21118965b89073f60271ef4a3b5d3c58
Receive message packet parameter description:
Return the XML format of the text message:
b2a0af5a8fd26276da50279a1c63a57a ea5d8177d19f22584533e5c37c389942eae09a64d076f96f3228ae9470271e996671a89dce89e879d9e9c6d81d03862b c5123754d1f4829fae4905e8abb602f9590cdcaa06dc33eb0992fff736103f4f42a4b8d57eb0afadcf16b7a02c69caaf 246311df1688542638dc52b54a1a4c8712345678e660f1169ff44ea75c5a982fcb1cde61 42815c2206ae835d7fd68cb4ae21e4dffa796850a1cf5d7bc01ca8cd7f8b83de698463fc03844fbe5a9caafaa1ebd0b1 ea63b4477034504a08070acf4e0b68b2212a4684f1079d663286208e36325081aa91fa7c40b1cd973268e154dae1a50e 21118965b89073f60271ef4a3b5d3c58
Return the text message packet parameter description :
②Receive/return text message code implementation
Developers receive WeChat on their own servers The code for receiving the XML data packet POSTed by the server is as follows:
if(IsPostBack) { //*********************************自动应答代码块********************************* string postString = string.Empty; using (Stream stream = HttpContext.Current.Request.InputStream) { Byte[] postBytes = new Byte[stream.Length]; stream.Read(postBytes, 0, (Int32)stream.Length); //接收的消息为GBK格式 postString = Encoding.GetEncoding("GBK").GetString(postBytes); string responseContent = help.ReturnMessage(postString ); //返回的消息为UTF-8格式 HttpContext.Current.Response.ContentEncoding = Encoding.UTF8; HttpContext.Current.Response.Write(responseContent); } //********************************自动应答代码块end******************************* }
Note: When receiving the message, the message format must be converted to "GBK" format, otherwise it will not be effectively parsed when parsing the message later.
ReturnMessage() processing method code is as follows:
/// <summary> /// 统一全局返回消息处理方法 /// </summary> /// <param name="postStr"></param> /// <returns></returns> public string ReturnMessage(string postStr) { string responseContent = ""; XmlDocument xmldoc = new XmlDocument(); xmldoc.Load(new System.IO.MemoryStream(System.Text.Encoding.GetEncoding("GB2312").GetBytes(postStr))); XmlNode MsgType = xmldoc.SelectSingleNode("/xml/MsgType"); if (MsgType != null) { switch (MsgType.InnerText) { case "event": responseContent = EventHandle(xmldoc);//菜单事件处理 break; case "text": responseContent = TextHandle(xmldoc);//文本消息处理 break; default: break; } } return responseContent; }
TextHandle(xmldoc) processing method code is as follows:
/// <summary> /// 接受文本消息并回复自定义消息 /// </summary> /// <param name="xmldoc"></param> /// <returns></returns> public string TextHandle(XmlDocument xmldoc) { string responseContent = ""; XmlNode ToUserName = xmldoc.SelectSingleNode("/xml/ToUserName"); XmlNode FromUserName = xmldoc.SelectSingleNode("/xml/FromUserName"); XmlNode Content = xmldoc.SelectSingleNode("/xml/Content"); if (Content != null) { if (Content.InnerText == "指定回复消息的自定义文本") { responseContent = string.Format(XMLTemplate.Message_Text, FromUserName.InnerText, ToUserName.InnerText, DateTime.Now.Ticks, "自定义回复消息内容"); } } return responseContent; }
The code demonstration to implement the function here has been completed, and the rest will follow The message processing mode also interacts based on this method. For example, receiving/replying text messages, picture messages, voice messages, video messages, short video messages, geographical location messages, link messages, etc. can be implemented by referring to the above code.
For more C# WeChat development series-receiving/returning text messages related articles, please pay attention to the PHP Chinese website!