溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何使用.Net開發(fā)微信公眾平臺之自定義菜單

發(fā)布時間:2021-09-13 17:32:17 來源:億速云 閱讀:109 作者:小新 欄目:移動開發(fā)

這篇文章主要為大家展示了“如何使用.Net開發(fā)微信公眾平臺之自定義菜單”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“如何使用.Net開發(fā)微信公眾平臺之自定義菜單”這篇文章吧。

用戶自定義菜單制作時,需要用到access_token,我們直接使用前面講解的IsExistAccess_Token()函數(shù)。我理解的微信公共平臺里面菜單分為button和sub_button,即菜單和子菜單,這些菜單都有一個name的屬性,類別分為click和view,click類有key屬性;而view類有url屬性,含有子菜單的菜單沒有key屬性也沒有url屬性。這些情況可以從下面的例子看出來。

 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 函數(shù)中調用這個MyMenu(),就可以顯示出來了。
既然顯示出來了,菜單的時間如何出發(fā)呢?我們已經了解到了如果類型為view的話,他有url屬性,這個不需要處理,點擊后會直接跳轉到你設定的url的頁面,下面我來看看如何觸發(fā)click吧,按照微信的文檔可以用(!string.IsNullOrEmpty(wx.EventName) && wx.EventName.Trim() == "CLICK")來判斷,我把之前的代碼改造一下,同時把在GetWxMessage()方法中把EventKey的值附上,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> 
  /// 發(fā)送文字消息 
  /// </summary> 
  /// <param name="wx">獲取的收發(fā)者信息</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開發(fā)微信公眾平臺之自定義菜單”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI