用戶自定義菜單制作時(shí),需要用到access_token,我們直接使用前面講解的IsExistAccess_Token()函數(shù)。我理解的微信公共平臺(tái)里面菜單分為button和sub_button,即菜單和子菜單,這些菜單都有一個(gè)name的屬性,類別分為click和view,click類有key屬性;而view類有url屬性,含有子菜單的菜單沒(méi)有key屬性也沒(méi)有url屬性。這些情況可以從下面的例子看出來(lái)。
public void MyMenu() { string weixin1 = ""; weixin1 = @" { ""button"":[ { ""type"":""click"", ""name"":""你好!"", ""key"":""Hello"" }, { ""type"":""view"", ""name"":""公司簡(jiǎn)介"", ""url"":""http://www.4ugood.net"" }, { ""name"":""產(chǎn)品介紹"", ""sub_button"":[ { ""type"":""click"", ""name"":""產(chǎn)品1"", ""key"":""P1"" }, { ""type"":""click"", ""name"":""產(chǎn)品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); }
在你頁(yè)面的 Page_Load 函數(shù)中調(diào)用這個(gè)MyMenu(),就可以顯示出來(lái)了。
既然顯示出來(lái)了,菜單的時(shí)間如何出發(fā)呢?我們已經(jīng)了解到了如果類型為view的話,他有url屬性,這個(gè)不需要處理,點(diǎn)擊后會(huì)直接跳轉(zhuǎn)到你設(shè)定的url的頁(yè)面,下面我來(lái)看看如何觸發(fā)click吧,按照微信的文檔可以用(!string.IsNullOrEmpty(wx.EventName) wx.EventName.Trim() == "CLICK")來(lái)判斷,我把之前的代碼改造一下,同時(shí)把在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直接回復(fù)“你好”"; res = sendTextMessage(wx, content); } else if (!string.IsNullOrEmpty(wx.EventName) wx.EventName.Trim() == "CLICK") { if(wx.EventKey=="Hello") res = sendTextMessage(wx, "你好,歡迎使用北京永杰友信科技有限公司公共微信平臺(tái)!"); if(wx.EventKey=="P1") res = sendTextMessage(wx, "你好,點(diǎn)擊了產(chǎn)品1"); if(wx.EventKey=="P2") res = sendTextMessage(wx, "你好,點(diǎn)擊了產(chǎn)品2"); } else { if (wx.MsgType == "text" wx.Content == "你好") { res = sendTextMessage(wx, "你好,歡迎使用北京永杰友信科技有限公司公共微信平臺(tái)!"); } else if (wx.MsgType == "voice") { res = sendTextMessage(wx, wx.Recognition); } else { res = sendTextMessage(wx, "你好,未能識(shí)別消息!"); } } 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">內(nèi)容/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; }
這樣就可以相應(yīng)你的菜單事件了,我上面的代碼寫的有很多可以優(yōu)化的地方,這里主要以簡(jiǎn)介為主,以后我們會(huì)逐漸搭建起一個(gè)微信公共平臺(tái)的.net框架,什么菜單類,消息類等等。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
標(biāo)簽:海西 甘肅 清遠(yuǎn) 臨夏 中衛(wèi) 聊城 慶陽(yáng)
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《微信公眾平臺(tái)開發(fā)之自定義菜單.Net代碼解析》,本文關(guān)鍵詞 微信,公眾,平臺(tái),開,發(fā)之,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。