您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)微信公眾平臺(tái)開發(fā)中如何使用.Net代碼自定義菜單的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
用戶自定義菜單制作時(shí),需要用到access_token,我們直接使用前面講解的IsExistAccess_Token()函數(shù)。我理解的微信公共平臺(tái)里面菜單分為button和sub_button,即菜單和子菜單,這些菜單都有一個(gè)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"":""產(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); }
在你頁面的 Page_Load 函數(shù)中調(diào)用這個(gè)MyMenu(),就可以顯示出來了。
既然顯示出來了,菜單的時(shí)間如何出發(fā)呢?我們已經(jīng)了解到了如果類型為view的話,他有url屬性,這個(gè)不需要處理,點(diǎn)擊后會(huì)直接跳轉(zhuǎn)到你設(shè)定的url的頁面,下面我來看看如何觸發(fā)click吧,按照微信的文檔可以用(!string.IsNullOrEmpty(wx.EventName) && wx.EventName.Trim() == "CLICK")來判斷,我把之前的代碼改造一下,同時(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)化的地方,這里主要以簡介為主,以后我們會(huì)逐漸搭建起一個(gè)微信公共平臺(tái)的.net框架,什么菜單類,消息類等等。
感謝各位的閱讀!關(guān)于“微信公眾平臺(tái)開發(fā)中如何使用.Net代碼自定義菜單”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。