


Introduction to the method of processing and responding to WeChat messages in c# using WeChat interface to develop WeChat portal applications
Mar 20, 2017 pm 02:15 PMThis article mainly introduces the process of processing and responding to WeChat messages in the WeChat portal using the WeChat interface in c#. Friends in need can refer to it
WeChat applications are in full swing, and many companies hope to catch up with the information Express, this is a business opportunity and a technical direction. Therefore, it has become one of the planned arrangements to study and learn about WeChat-related development when you have time. This series of articles hopes to comprehensively introduce the relevant development process and related experience summary of WeChat from a step-by-step perspective, hoping to give everyone an understanding of the relevant development process. This essay is mainly based on the previous article "C# Using WeChat Interface to Develop WeChat Portal Application" to provide an in-depth introduction and introduce the process of processing and responding to WeChat messages.
1. WeChat’s message response interaction
We know that WeChat’s server builds a bridge between the customer’s mobile phone and the developer’s server, through the transmission and response of messages. , realizes the interaction with the user, the following is its message flow chart.
The messages WeChat requests from the developer server include many types, but basically they are divided into text message processing, event message processing, voice message recognition, and The basic classification of message authentication operations before becoming a developer. Below is a message classification diagram I drew, which introduces these relationships and their respective message refinement classifications.
For these message requests, when we develop the server side, we need to write relevant logic for corresponding processing, and then respond to the message to the WeChat server platform.
In the previous essay, I posted the code to introduce the entry operation of WeChat message processing. The code is as follows.
The code is as follows:
public void ProcessRequest(HttpContext context) { //WHC.Framework.Commons.LogTextHelper.Info("測試記錄"); string postString = string.Empty; if (HttpContext.Current.Request.HttpMethod.ToUpper() == "POST") { using (Stream stream = HttpContext.Current.Request.InputStream) { Byte[] postBytes = new Byte[stream.Length]; stream.Read(postBytes, 0, (Int32)stream.Length); postString = Encoding.UTF8.GetString(postBytes); } if (!string.IsNullOrEmpty(postString)) { Execute(postString); } } else { Auth(); } }
Execute(postString); is the message processing function, which implements the distribution and processing of different messages.
The code is as follows:
/// <summary> /// 處理各種請求信息并應(yīng)答(通過POST的請求) /// </summary> /// <param name="postStr">POST方式提交的數(shù)據(jù)</param> private void Execute(string postStr) { WeixinApiDispatch dispatch = new WeixinApiDispatch(); string responseContent = dispatch.Execute(postStr); HttpContext.Current.Response.ContentEncoding = Encoding.UTF8; HttpContext.Current.Response.Write(responseContent); }
The WeixinApiDispatch inside is a distribution management class. It extracts the content of the request message, constructs different types of message parameters, and passes them to different response functions for processing. Then return the encapsulated XML content as a response.
The specific code processing logic is shown in the figure below.
This message processing interface actually defines a series of processing operations for request messages. The parameters are different message objects. The specific code definition is as follows ( Due to space reasons, some interfaces are omitted, please refer to the figure above for details).
The code is as follows:
/// <summary> /// 客戶端請求的數(shù)據(jù)接口 /// </summary> public interface IWeixinAction { /// <summary> /// 對文本請求信息進(jìn)行處理 /// </summary> /// <param name="info">文本信息實體</param> /// <returns></returns> string HandleText(RequestText info); /// <summary> /// 對圖片請求信息進(jìn)行處理 /// </summary> /// <param name="info">圖片信息實體</param> /// <returns></returns> string HandleImage(RequestImage info); /// <summary> /// 對訂閱請求事件進(jìn)行處理 /// </summary> /// <param name="info">訂閱請求事件信息實體</param> /// <returns></returns> string HandleEventSubscribe(RequestEventSubscribe info); /// <summary> /// 對菜單單擊請求事件進(jìn)行處理 /// </summary> /// <param name="info">菜單單擊請求事件信息實體</param> /// <returns></returns> string HandleEventClick(RequestEventClick info); }
Note: The entity class is defined by me according to the needs of program development, not the entity class of WeChat itself), which is very convenient for us to process Operation, otherwise different message content needs to be parsed each time, and problems may easily occur. Such a strongly typed data type improves the robustness and efficiency of our WeChat application development. The objects of these entity classes have a certain inheritance relationship, and their inheritance relationship is as follows.
WeChat’s reply message processing is the same as the information in the above section. It is also inherited from the BaseMessage entity class (Similarly, the entity class in the figure below and its inheritance relationship It is also customized and convenient for program development), and its relationship is as follows
Reply messages, generally the most commonly used are text messages and graphic messages .
The effect of the text message is as follows.
Graphic messages, you can add pictures, and you can also add detailed link pages. It is a very beautiful effect. For some content that is relatively large, I hope to show better effects. , this is generally used, and the effect is as follows.
The above is the detailed content of Introduction to the method of processing and responding to WeChat messages in c# using WeChat interface to develop WeChat portal applications. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)