Home Backend Development C#.Net Tutorial C# Development of WeChat Portal and Application (2) Graphical Code Tutorial for WeChat Message Processing and Response

C# Development of WeChat Portal and Application (2) Graphical Code Tutorial for WeChat Message Processing and Response

Jun 18, 2017 am 10:26 AM
.net deal with application develop information portal

The article mainly introduces the second part of C# development of WeChat portal and application in detail, WeChat message processing and response, which has certain reference value. Interested friends can refer to it

WeChat application is in full swing , many companies hope to get on 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 the development of WeChat 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# Development of WeChat Portal and Application (1)--Start Using WeChat Interface" 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.


  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();
   }
  }
Copy after login

Execute(postString); is the message processing function, which implements the distribution and processing of different messages. '


    /// <summary>
    /// 处理各种请求信息并应答(通过POST的请求)
    /// </summary>
    /// <param name="postStr">POST方式提交的数据</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);
    }
Copy after login

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. , and 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 , specifically The code definition is as follows (due to space reasons, some interfaces are omitted, please refer to the figure above for details).


  /// <summary>
  /// 客户端请求的数据接口
  /// </summary>
  public interface IWeixinAction
  {
    /// <summary>
    /// 对文本请求信息进行处理
    /// </summary>
    /// <param name="info">文本信息实体</param>
    /// <returns></returns>
    string HandleText(RequestText info);

    /// <summary>
    /// 对图片请求信息进行处理
    /// </summary>
    /// <param name="info">图片信息实体</param>
    /// <returns></returns>
    string HandleImage(RequestImage info);

...........................


    /// <summary>
    /// 对订阅请求事件进行处理
    /// </summary>
    /// <param name="info">订阅请求事件信息实体</param>
    /// <returns></returns>
    string HandleEventSubscribe(RequestEventSubscribe info);

    /// <summary>
    /// 对菜单单击请求事件进行处理
    /// </summary>
    /// <param name="info">菜单单击请求事件信息实体</param>
    /// <returns></returns>
    string HandleEventClick(RequestEventClick info);

..............................
  }
Copy after login

As can be seen from the above code, different messages are passed to the processing function in the form of different message entity classes (Note: The entity class is defined by me according to the needs of program development. It is not the entity class of WeChat itself ). This is very convenient for us to handle operations. Otherwise, different message contents need to be parsed each time, which is easy to happen. Question, such a strongly typed data type improves the robustness and efficiency of our development of WeChat applications. The objects of these entity classes have a certain inheritance relationship, and their inheritance relationship is as follows.

2. WeChat management interface

The above message classification is a message request operation sent by the WeChat server to the developer server. There is also a message, which is a message request or response from our developer server to the WeChat server. This is temporarily called WeChat's management interface, which shows that we can perform related message replies or data management operations through these interfaces. Its classification diagram is shown below.

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 to facilitate program development), and its relationship is as follows

#The most commonly used messages to reply to 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, which is a very good-looking effect. For those who have a lot of content and want to show better effects, this is generally used. The effect is as follows.

The above is the detailed content of C# Development of WeChat Portal and Application (2) Graphical Code Tutorial for WeChat Message Processing and Response. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1658
14
PHP Tutorial
1257
29
C# Tutorial
1231
24
The operation process of WIN10 service host occupying too much CPU The operation process of WIN10 service host occupying too much CPU Mar 27, 2024 pm 02:41 PM

1. First, we right-click the blank space of the taskbar and select the [Task Manager] option, or right-click the start logo, and then select the [Task Manager] option. 2. In the opened Task Manager interface, we click the [Services] tab on the far right. 3. In the opened [Service] tab, click the [Open Service] option below. 4. In the [Services] window that opens, right-click the [InternetConnectionSharing(ICS)] service, and then select the [Properties] option. 5. In the properties window that opens, change [Open with] to [Disabled], click [Apply] and then click [OK]. 6. Click the start logo, then click the shutdown button, select [Restart], and complete the computer restart.

Four recommended AI-assisted programming tools Four recommended AI-assisted programming tools Apr 22, 2024 pm 05:34 PM

This AI-assisted programming tool has unearthed a large number of useful AI-assisted programming tools in this stage of rapid AI development. AI-assisted programming tools can improve development efficiency, improve code quality, and reduce bug rates. They are important assistants in the modern software development process. Today Dayao will share with you 4 AI-assisted programming tools (and all support C# language). I hope it will be helpful to everyone. https://github.com/YSGStudyHards/DotNetGuide1.GitHubCopilotGitHubCopilot is an AI coding assistant that helps you write code faster and with less effort, so you can focus more on problem solving and collaboration. Git

How to Undo Delete from Home Screen in iPhone How to Undo Delete from Home Screen in iPhone Apr 17, 2024 pm 07:37 PM

Deleted something important from your home screen and trying to get it back? You can put app icons back on the screen in a variety of ways. We have discussed all the methods you can follow and put the app icon back on the home screen. How to Undo Remove from Home Screen in iPhone As we mentioned before, there are several ways to restore this change on iPhone. Method 1 – Replace App Icon in App Library You can place an app icon on your home screen directly from the App Library. Step 1 – Swipe sideways to find all apps in the app library. Step 2 – Find the app icon you deleted earlier. Step 3 – Simply drag the app icon from the main library to the correct location on the home screen. This is the application diagram

Learn how to handle special characters and convert single quotes in PHP Learn how to handle special characters and convert single quotes in PHP Mar 27, 2024 pm 12:39 PM

In the process of PHP development, dealing with special characters is a common problem, especially in string processing, special characters are often escaped. Among them, converting special characters into single quotes is a relatively common requirement, because in PHP, single quotes are a common way to wrap strings. In this article, we will explain how to handle special character conversion single quotes in PHP and provide specific code examples. In PHP, special characters include but are not limited to single quotes ('), double quotes ("), backslash (), etc. In strings

Which AI programmer is the best? Explore the potential of Devin, Tongyi Lingma and SWE-agent Which AI programmer is the best? Explore the potential of Devin, Tongyi Lingma and SWE-agent Apr 07, 2024 am 09:10 AM

On March 3, 2022, less than a month after the birth of the world's first AI programmer Devin, the NLP team of Princeton University developed an open source AI programmer SWE-agent. It leverages the GPT-4 model to automatically resolve issues in GitHub repositories. SWE-agent's performance on the SWE-bench test set is similar to Devin, taking an average of 93 seconds and solving 12.29% of the problems. By interacting with a dedicated terminal, SWE-agent can open and search file contents, use automatic syntax checking, edit specific lines, and write and execute tests. (Note: The above content is a slight adjustment of the original content, but the key information in the original text is retained and does not exceed the specified word limit.) SWE-A

Learn how to develop mobile applications using Go language Learn how to develop mobile applications using Go language Mar 28, 2024 pm 10:00 PM

Go language development mobile application tutorial As the mobile application market continues to boom, more and more developers are beginning to explore how to use Go language to develop mobile applications. As a simple and efficient programming language, Go language has also shown strong potential in mobile application development. This article will introduce in detail how to use Go language to develop mobile applications, and attach specific code examples to help readers get started quickly and start developing their own mobile applications. 1. Preparation Before starting, we need to prepare the development environment and tools. head

Share several .NET open source AI and LLM related project frameworks Share several .NET open source AI and LLM related project frameworks May 06, 2024 pm 04:43 PM

The development of artificial intelligence (AI) technologies is in full swing today, and they have shown great potential and influence in various fields. Today Dayao will share with you 4 .NET open source AI model LLM related project frameworks, hoping to provide you with some reference. https://github.com/YSGStudyHards/DotNetGuide/blob/main/docs/DotNet/DotNetProjectPicks.mdSemanticKernelSemanticKernel is an open source software development kit (SDK) designed to integrate large language models (LLM) such as OpenAI, Azure

Explore the advantages and application scenarios of Go language Explore the advantages and application scenarios of Go language Mar 27, 2024 pm 03:48 PM

The Go language is an open source programming language developed by Google and first released in 2007. It is designed to be a simple, easy-to-learn, efficient, and highly concurrency language, and is favored by more and more developers. This article will explore the advantages of Go language, introduce some application scenarios suitable for Go language, and give specific code examples. Advantages: Strong concurrency: Go language has built-in support for lightweight threads-goroutine, which can easily implement concurrent programming. Goroutin can be started by using the go keyword

See all articles