Home WeChat Applet WeChat Development Detailed explanation of custom menu code for .Net development of WeChat public platform

Detailed explanation of custom menu code for .Net development of WeChat public platform

Apr 25, 2017 am 10:41 AM
.net WeChat Custom menu

This article mainly provides a detailed analysis of the custom menu .Net code developed by WeChat public platform. Interested friends can refer to it

When making user-defined menus, access_token is needed , we directly use the IsExistAccess_Token() function explained earlier. As I understand it, the menus in the WeChat public platform are divided into button and sub_button, that is, menus and submenus. These menus have a name attribute, and the categories are divided into click and view. The click class has the key attribute; and the view class has the url attribute and contains the submenu. The menu menu has no key attribute or url attribute. These situations can be seen from the following examples.


 public void MyMenu()
  {
   string weixin1 = "";
   weixin1 = @" {
  ""button"":[
  { 
   ""type"":""click"",
   ""name"":""你好!"",
   ""key"":""Hello""
  },
  {
   ""type"":""view"",
   ""name"":""公司简介"",
   ""url"":""http://www.4ugood.net""
  },
  {
   ""name"":""产品介绍"",
   ""sub_button"":[
   {
    ""type"":""click"",
    ""name"":""产品1"",
    ""key"":""P1""
   },
   {
    ""type"":""click"",
    ""name"":""产品2"",
    ""key"":""P2""
   }]
  }]
 }
";
   string access_token = IsExistAccess_Token();
   string i = GetPage("https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+access_token, weixin1);
   Response.Write(i);
  }
Copy after login

Call this MyMenu() in the Page_Load function of your page and it will be displayed.
Now that it is displayed, how to start the menu time? We have already learned that if the type is view, it has a url attribute. This does not need to be processed. After clicking, it will jump directly to the page of the url you set. Let me take a look at how to trigger the click. You can follow the WeChat documentation. Use (!string.IsNullOrEmpty(wx.EventName) && wx.EventName.Trim() == "CLICK") to judge. I modified the previous code and attached the value of EventKey in the GetWxMessage() method. ,wx.EventKey = xml.SelectSingleNode("xml").SelectSingleNode("EventKey").InnerText;


##

protected void Page_Load(object sender, EventArgs e)
  {
   MyMenu();
   wxmessage wx = GetWxMessage();
   string res = "";
   if (!string.IsNullOrEmpty(wx.EventName) && wx.EventName.Trim() == "subscribe")
   {
    string content = "";
    content = "/:rose欢迎北京永杰友信科技有限公司/:rose\n直接回复“你好”";
    res = sendTextMessage(wx, content);
   }
   else if (!string.IsNullOrEmpty(wx.EventName) && wx.EventName.Trim() == "CLICK")
   {
    if(wx.EventKey=="Hello")
     res = sendTextMessage(wx, "你好,欢迎使用北京永杰友信科技有限公司公共微信平台!");
    if(wx.EventKey=="P1")
     res = sendTextMessage(wx, "你好,点击了产品1");
    if(wx.EventKey=="P2")
     res = sendTextMessage(wx, "你好,点击了产品2");
   }
   else
   {
    if (wx.MsgType == "text" && wx.Content == "你好")
    {
     res = sendTextMessage(wx, "你好,欢迎使用北京永杰友信科技有限公司公共微信平台!");
    }
    else if (wx.MsgType == "voice")
    {
     res = sendTextMessage(wx, wx.Recognition);
    }
    else
    {
     res = sendTextMessage(wx, "你好,未能识别消息!");
    }
   }

   Response.Write(res);
  }
  private wxmessage GetWxMessage()
  {
   wxmessage wx = new wxmessage();
   StreamReader str = new StreamReader(Request.InputStream, System.Text.Encoding.UTF8);
   XmlDocument xml = new XmlDocument();
   xml.Load(str);
   wx.ToUserName = xml.SelectSingleNode("xml").SelectSingleNode("ToUserName").InnerText;
   wx.FromUserName = xml.SelectSingleNode("xml").SelectSingleNode("FromUserName").InnerText;
   wx.MsgType = xml.SelectSingleNode("xml").SelectSingleNode("MsgType").InnerText;
   if (wx.MsgType.Trim() == "text")
   {
    wx.Content = xml.SelectSingleNode("xml").SelectSingleNode("Content").InnerText;
   }
   if (wx.MsgType.Trim() == "event")
   {
    wx.EventName = xml.SelectSingleNode("xml").SelectSingleNode("Event").InnerText;
    wx.EventKey = xml.SelectSingleNode("xml").SelectSingleNode("EventKey").InnerText;
   }
   if (wx.MsgType.Trim() == "voice")
   {
    wx.Recognition = xml.SelectSingleNode("xml").SelectSingleNode("Recognition").InnerText;
   }
   
   return wx;
  }

/// <summary> 
  /// 发送文字消息 
  /// </summary> 
  /// <param name="wx">获取的收发者信息</param> 
  /// <param name="content">内容</param> 
  /// <returns></returns> 
  private string sendTextMessage(wxmessage wx, string content)
  {
   string res = string.Format(@"<xml>
         <ToUserName><![CDATA[{0}]]></ToUserName>
         <FromUserName><![CDATA[{1}]]></FromUserName>
         <CreateTime>{2}</CreateTime>
         <MsgType><![CDATA[text]]></MsgType>
         <Content><![CDATA[{3}]]></Content>
         </xml> ",
    wx.FromUserName, wx.ToUserName, DateTime.Now, content);
   return res;
  }
Copy after login
This way you can respond to your menu events Okay, there are many areas that can be optimized in the code I wrote above. Here I mainly focus on the introduction. In the future, we will gradually build a .net framework for the WeChat public platform, including menu classes, message classes, etc.

The above is the detailed content of Detailed explanation of custom menu code for .Net development of WeChat public platform. 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)

Ouyi Exchange app domestic download tutorial Ouyi Exchange app domestic download tutorial Mar 21, 2025 pm 05:42 PM

This article provides a detailed guide to safe download of Ouyi OKX App in China. Due to restrictions on domestic app stores, users are advised to download the App through the official website of Ouyi OKX, or use the QR code provided by the official website to scan and download. During the download process, be sure to verify the official website address, check the application permissions, perform a security scan after installation, and enable two-factor verification. During use, please abide by local laws and regulations, use a safe network environment, protect account security, be vigilant against fraud, and invest rationally. This article is for reference only and does not constitute investment advice. Digital asset transactions are at your own risk.

The difference between H5 and mini-programs and APPs The difference between H5 and mini-programs and APPs Apr 06, 2025 am 10:42 AM

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

What should I do if the company's security software conflicts with applications? How to troubleshoot HUES security software causes common software to fail to open? What should I do if the company's security software conflicts with applications? How to troubleshoot HUES security software causes common software to fail to open? Apr 01, 2025 pm 10:48 PM

Compatibility issues and troubleshooting methods for company security software and application. Many companies will install security software in order to ensure intranet security. However, security software sometimes...

How to choose H5 and applets How to choose H5 and applets Apr 06, 2025 am 10:51 AM

The choice of H5 and applet depends on the requirements. For applications with cross-platform, rapid development and high scalability, choose H5; for applications with native experience, rich functions and platform dependencies, choose applets.

What is the difference between H5 page production and WeChat applets What is the difference between H5 page production and WeChat applets Apr 05, 2025 pm 11:51 PM

H5 is more flexible and customizable, but requires skilled technology; mini programs are quick to get started and easy to maintain, but are limited by the WeChat framework.

How to solve the problem of JS resource caching in enterprise WeChat? How to solve the problem of JS resource caching in enterprise WeChat? Apr 04, 2025 pm 05:06 PM

Discussion on the JS resource caching issue of Enterprise WeChat. When upgrading project functions, some users often encounter situations where they fail to successfully upgrade, especially in the enterprise...

What are the different ways of promoting H5 and mini programs? What are the different ways of promoting H5 and mini programs? Apr 06, 2025 am 11:03 AM

There are differences in the promotion methods of H5 and mini programs: platform dependence: H5 depends on the browser, and mini programs rely on specific platforms (such as WeChat). User experience: The H5 experience is poor, and the mini program provides a smooth experience similar to native applications. Communication method: H5 is spread through links, and mini programs are shared or searched through the platform. H5 promotion methods: social sharing, email marketing, QR code, SEO, paid advertising. Mini program promotion methods: platform promotion, social sharing, offline promotion, ASO, cooperation with other platforms.

How to download Binance in the country How to download Binance in the country Mar 21, 2025 pm 05:33 PM

This article provides a guide to safely downloading the Binance App in China. Due to restrictions on domestic app stores, it is difficult to download directly. It is recommended to download the APK installation package through the Binance official website or scan the QR code to download the App. Be sure to carefully check the official domain name, check the application permissions, perform a secure scan after installation, and enable two-factor verification (2FA). Please be sure to understand and abide by local laws and regulations before downloading and using it. The risk of digital asset transactions is high, so please operate with caution. This article is for reference only and does not constitute investment advice. All risks are borne by the user. Keywords: Binance, Binance, Download, App, Domestic, Security, Tutorial, Digital Currency, Cryptocurrency

See all articles