Home  >  Article  >  WeChat Applet  >  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

Y2J
Y2JOriginal
2017-04-25 10:41:391499browse

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);
  }

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;
  }

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:
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