微信公共服务平台开发(.Net 的实现)6-------自定义菜单

来源:互联网 发布:人工智能四大领域 编辑:程序博客网 时间:2024/05/02 10:26

用户自定义菜单制作时,需要用到access_token,我们直接使用前面讲解的IsExistAccess_Token()函数。我理解的微信公共平台里面菜单分为button和sub_button,即菜单和子菜单,这些菜单都有一个name的属性,类别分为click和view,click类有key属性;而view类有url属性,含有子菜单的菜单没有key属性也没有url属性。这些情况可以从下面的例子看出来。

[csharp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. public void MyMenu() 
  2.      { 
  3.          string weixin1 =""
  4.          weixin1 = @" { 
  5.      ""button"":[ 
  6.      {   
  7.           ""type"":""click""
  8.           ""name"":""你好!""
  9.           ""key"":""Hello"" 
  10.       }, 
  11.       { 
  12.            ""type"":""view""
  13.            ""name"":""公司简介""
  14.            ""url"":""http://www.4ugood.net"" 
  15.       }, 
  16.       { 
  17.            ""name"":""产品介绍""
  18.            ""sub_button"":[ 
  19.             { 
  20.                ""type"":""click""
  21.                ""name"":""产品1""
  22.                 ""key"":""P1"" 
  23.             }, 
  24.             { 
  25.                ""type"":""click""
  26.                ""name"":""产品2""
  27.                ""key"":""P2"" 
  28.             }] 
  29.        }] 
  30. "; 
  31.  
  32.          string access_token = IsExistAccess_Token(); 
  33.          string i = GetPage("https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+access_token, weixin1); 
  34.          Response.Write(i); 
  35.      } 
在你页面的 Page_Load 函数中调用这个MyMenu(),就可以显示出来了。
既然显示出来了,菜单的时间如何出发呢?我们已经了解到了如果类型为view的话,他有url属性,这个不需要处理,点击后会直接跳转到你设定的url的页面,下面我来看看如何触发click吧,按照微信的文档可以用(!string.IsNullOrEmpty(wx.EventName) && wx.EventName.Trim() == "CLICK")来判断,我把之前的代码改造一下,同时把在GetWxMessage()方法中把EventKey的值附上,wx.EventKey = xml.SelectSingleNode("xml").SelectSingleNode("EventKey").InnerText;

[csharp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. protected void Page_Load(object sender, EventArgs e) 
  2.      { 
  3.          
  4.          MyMenu(); 
  5.          wxmessage wx = GetWxMessage(); 
  6.          string res = ""
  7.  
  8.          if (!string.IsNullOrEmpty(wx.EventName) && wx.EventName.Trim() =="subscribe"
  9.          { 
  10.              string content = ""
  11.              content = "/:rose欢迎北京永杰友信科技有限公司/:rose\n直接回复“你好”"
  12.              res = sendTextMessage(wx, content); 
  13.          } 
  14.          else if (!string.IsNullOrEmpty(wx.EventName) && wx.EventName.Trim() =="CLICK"
  15.          { 
  16.              if(wx.EventKey=="Hello"
  17.                  res = sendTextMessage(wx, "你好,欢迎使用北京永杰友信科技有限公司公共微信平台!"); 
  18.              if(wx.EventKey=="P1"
  19.                  res = sendTextMessage(wx, "你好,点击了产品1"); 
  20.              if(wx.EventKey=="P2"
  21.                  res = sendTextMessage(wx, "你好,点击了产品2"); 
  22.          } 
  23.          else 
  24.          { 
  25.              if (wx.MsgType =="text" && wx.Content == "你好"
  26.              { 
  27.                  res = sendTextMessage(wx, "你好,欢迎使用北京永杰友信科技有限公司公共微信平台!"); 
  28.              } 
  29.              else if (wx.MsgType == "voice"
  30.              { 
  31.                  res = sendTextMessage(wx, wx.Recognition); 
  32.              } 
  33.              else 
  34.              { 
  35.                  res = sendTextMessage(wx, "你好,未能识别消息!"); 
  36.              } 
  37.          } 
  38.  
  39.          Response.Write(res); 
  40.      } 
  41.  
  42.  
  43.  
  44.      private wxmessage GetWxMessage() 
  45.      { 
  46.          wxmessage wx = new wxmessage(); 
  47.          StreamReader str = new StreamReader(Request.InputStream, System.Text.Encoding.UTF8); 
  48.          XmlDocument xml = new XmlDocument(); 
  49.          xml.Load(str); 
  50.          wx.ToUserName = xml.SelectSingleNode("xml").SelectSingleNode("ToUserName").InnerText; 
  51.          wx.FromUserName = xml.SelectSingleNode("xml").SelectSingleNode("FromUserName").InnerText; 
  52.          wx.MsgType = xml.SelectSingleNode("xml").SelectSingleNode("MsgType").InnerText; 
  53.          if (wx.MsgType.Trim() =="text"
  54.          { 
  55.              wx.Content = xml.SelectSingleNode("xml").SelectSingleNode("Content").InnerText; 
  56.          } 
  57.          if (wx.MsgType.Trim() =="event"
  58.          { 
  59.              wx.EventName = xml.SelectSingleNode("xml").SelectSingleNode("Event").InnerText; 
  60.              wx.EventKey = xml.SelectSingleNode("xml").SelectSingleNode("EventKey").InnerText; 
  61.          } 
  62.          if (wx.MsgType.Trim() == "voice"
  63.          { 
  64.              wx.Recognition = xml.SelectSingleNode("xml").SelectSingleNode("Recognition").InnerText; 
  65.          } 
  66.           
  67.          return wx; 
  68.      } 
  69.  
  70.  
  71.  
  72.  
  73. /// <summary>   
  74.      /// 发送文字消息   
  75.      /// </summary>   
  76.      /// <param name="wx">获取的收发者信息</param>   
  77.      /// <param name="content">内容</param>  
  78.      /// <returns></returns>   
  79.      private string sendTextMessage(wxmessage wx,string content) 
  80.      { 
  81.          string res = string.Format(@"<xml> 
  82.                                    <ToUserName><![CDATA[{0}]]></ToUserName> 
  83.                                    <FromUserName><![CDATA[{1}]]></FromUserName> 
  84.                                     <CreateTime>{2}</CreateTime> 
  85.                                     <MsgType><![CDATA[text]]></MsgType> 
  86.                                     <Content><![CDATA[{3}]]></Content> 
  87.                                    </xml> ", 
  88.              wx.FromUserName, wx.ToUserName, DateTime.Now, content); 
  89.          return res; 
  90.      } 
这样就可以相应你的菜单事件了,我上面的代码写的有很多可以优化的地方,这里主要以简介为主,以后我们会逐渐搭建起一个微信公共平台的.net框架,什么菜单类,消息类等等。

0 0
原创粉丝点击