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

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

  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.      } 
 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);
     }
在你页面的 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;

  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.      } 
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;
     }
这样就可以相应你的菜单事件了,我上面的代码写的有很多可以优化的地方,这里主要以简介为主,以后我们会逐渐搭建起一个微信公共平台的.net框架,什么菜单类,消息类等等。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值