SoFunction
Updated on 2025-03-07

Custom menu.Net code analysis for WeChat public platform development

When creating a user-defined menu, access_token is required. We directly use the IsExistAccess_Token() function explained earlier. The menus in the WeChat public platform I understand are divided into button and sub_button, that is, menus and submenu. These menus have a name attribute, and the categories are divided into click and view. The click class has a key attribute; while the view class has a url attribute, and the menu containing the submenu 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"":""Hello!"",
   ""key"":""Hello""
  },
  {
   ""type"":""view"",
   ""name"":""Company Profile"",
   ""url"":""http://""
  },
  {
   ""name"":""Product Introduction"",
   ""sub_button"":[
   {
    ""type"":""click"",
    ""name"":""Product 1"",
    ""key"":""P1""
   },
   {
    ""type"":""click"",
    ""name"":""Product 2"",
    ""key"":""P2""
   }]
  }]
 }
";

   string access_token = IsExistAccess_Token();
   string i = GetPage("/cgi-bin/menu/create?access_token="+access_token, weixin1);
   (i);
  }

Call this MyMenu() in the Page_Load function on your page and it will be displayed.
Since it has been displayed, how do you set the menu time? We have learned that if the type is view, it has the 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 see how to trigger click. According to the WeChat document, you can use (!() && () == "CLICK") to judge. I will modify the previous code and attach the value of EventKey in the GetWxMessage() method, = ("xml").SelectSingleNode("EventKey").InnerText;

protected void Page_Load(object sender, EventArgs e)
  {
  
   MyMenu();
   wxmessage wx = GetWxMessage();
   string res = "";

   if (!() && () == "subscribe")
   {
    string content = "";
    content = "/:rose Welcome to Beijing Yongjie Youxin Technology Co., Ltd./:rose\nReply directly "Hello"";
    res = sendTextMessage(wx, content);
   }
   else if (!() && () == "CLICK")
   {
    if(=="Hello")
     res = sendTextMessage(wx, "Hello, welcome to use the public WeChat platform of Beijing Yongjie Youxin Technology Co., Ltd.");
    if(=="P1")
     res = sendTextMessage(wx, "Hello, clicked on Product 1");
    if(=="P2")
     res = sendTextMessage(wx, "Hello, clicked on product 2");
   }
   else
   {
    if ( == "text" &&  == "Hello")
    {
     res = sendTextMessage(wx, "Hello, welcome to use the public WeChat platform of Beijing Yongjie Youxin Technology Co., Ltd.");
    }
    else if ( == "voice")
    {
     res = sendTextMessage(wx, );
    }
    else
    {
     res = sendTextMessage(wx, "Hello, the message was not recognized!");
    }
   }

   (res);
  }



  private wxmessage GetWxMessage()
  {
   wxmessage wx = new wxmessage();
   StreamReader str = new StreamReader(, .UTF8);
   XmlDocument xml = new XmlDocument();
   (str);
    = ("xml").SelectSingleNode("ToUserName").InnerText;
    = ("xml").SelectSingleNode("FromUserName").InnerText;
    = ("xml").SelectSingleNode("MsgType").InnerText;
   if (() == "text")
   {
     = ("xml").SelectSingleNode("Content").InnerText;
   }
   if (() == "event")
   {
     = ("xml").SelectSingleNode("Event").InnerText;
     = ("xml").SelectSingleNode("EventKey").InnerText;
   }
   if (() == "voice")
   {
     = ("xml").SelectSingleNode("Recognition").InnerText;
   }
   
   return wx;
  }




/// <summary> 
  /// Send text message  /// </summary> 
  /// <param name="wx">Retrieve the transceiver information</param>  /// <param name="content">Content</param>  /// &lt;returns&gt;&lt;/returns&gt; 
  private string sendTextMessage(wxmessage wx, string content)
  {
   string res = (@"&lt;xml&gt;
         &lt;ToUserName&gt;&lt;![CDATA[{0}]]&gt;&lt;/ToUserName&gt;
         &lt;FromUserName&gt;&lt;![CDATA[{1}]]&gt;&lt;/FromUserName&gt;
         &lt;CreateTime&gt;{2}&lt;/CreateTime&gt;
         &lt;MsgType&gt;&lt;![CDATA[text]]&gt;&lt;/MsgType&gt;
         &lt;Content&gt;&lt;![CDATA[{3}]]&gt;&lt;/Content&gt;
         &lt;/xml&gt; ",
    , , , content);
   return res;
  }

This will correspond to your menu events. The code above I wrote has many places that can be optimized. Here we mainly focus on introductions. In the future, we will gradually build a .net framework for the WeChat public platform, such as menu categories, message categories, etc.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.