WeChat public service platform development (. Net implementation) 6 ------- custom menu

Source: Internet
Author: User

Access_token is required for custom menu creation. We use the IsExistAccess_Token () function described above. In my understanding, the menus in the public platform are divided into buttons and sub_buttons, that is, menus and submenus. These menus have a name attribute, and the categories are divided into click and view. The click class has the key attribute; the view class has the url attribute, and the menu containing the sub-menu has neither the key attribute nor the url attribute. These situations can be seen from the example below.

Public void MyMenu () {string weixin1 = ""; weixin1 = @ "{" "button": [{"" type ":" "click "", "" name "": "" Hello! "", "" Key ":" "Hello" "},{" "type": "" view "", "" name "": "" Company Profile "", "" url ":" "http://www.4ugood.net" "},{" "name": "Product Introduction" "," "sub_button "": [{"" type ":" "click" "," "name": "" Product 1 "", "" key ":" P1 ""}, {"" type ":" "click" "," "name": "" product 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 );}
Call this MyMenu () in the Page_Load function on your page to display it.
Now that the menu is displayed, how does the menu start? We have learned that if the type is view, it has the url attribute, which does not need to be processed. After you click it, it will jump directly to the page of the url you set, next, let's take a look at how to trigger the click. The following document can be used (! String. isNullOrEmpty (wx. eventName) & wx. eventName. trim () = "CLICK") to determine, I will transform the previous code, and attach the EventKey VALUE IN THE GetWxMessage () method, 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 Welcome Beijing Yongjie youxin Technology Co., Ltd /: rose \ n directly replies "hello" "; res = sendTextMessage (wx, content);} else if (! String. isNullOrEmpty (wx. eventName) & wx. eventName. trim () = "CLICK") {if (wx. eventKey = "Hello") res = sendTextMessage (wx, "Hello, welcome to Beijing Yongjie youxin Technology Co., Ltd. public platform! "); If (wx. eventKey = "P1") res = sendTextMessage (wx, "Hello, click product 1"); if (wx. eventKey = "P2") res = sendTextMessage (wx, "Hello, click product 2");} else {if (wx. msgType = "text" & wx. content = "hello") {res = sendTextMessage (wx, "Hello, welcome to Beijing Yongjie youxin Technology Co., Ltd. public platform! ");} Else if (wx. msgType = "voice") {res = sendTextMessage (wx, wx. recognition);} else {res = sendTextMessage (wx, "Hello, failed to recognize the message! ") ;}} 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 ;}////// Send a text message //////Recipient Information obtained///Content///
 Private string sendTextMessage (wxmessage wx, string content) {string res = string. Format (@"
                                    
  {0}
                                     
  {1}
                                      
  
   
{2}
                                      
  text
                                      
  {3}
                                     
 ", Wx. FromUserName, wx. ToUserName, DateTime. Now, content); return res ;}
In this way, you can correspond to your menu events. The code above has a lot to be optimized. Here we mainly focus on the introduction. In the future, we will build a public platform. net Framework, menu class, message class, and so on.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.