WeChat enterprise account development: custom menu, custom

Source: Internet
Author: User

Enterprise ID development: custom menu, custom

The development enterprise number can be customized through the program menu, you only need to call the relevant interface to achieve.

In fact, this menu is the bottom menu. Currently, a custom menu can contain up to three level-1 menus, and each level-1 menu can contain up to five level-2 menus. A level-1 menu can contain up to four Chinese characters, and a level-2 menu can contain up to seven Chinese characters. The extra content will be replaced. In fact, creating a menu is also very simple.

However, it should be noted that if a sub-menu exists, this menu will not send events to the backend. For example, I have defined three level-1 menus with one click and two views.

If no sub-menu exists, When you click a menu of the click type, the primary node sends a report menu event to the backend. If yes, the report menu event is not sent.

If no sub-menu exists, When you click a view menu, the system sends an event to the backend by actively clicking the menu jump link. In addition, the corresponding webpage is opened. If yes, the event of clicking the menu jump link is not sent, and the corresponding webpage is not opened.

That is, if a sub-menu exists, the sub-menu is clicked to display the sub-menu, and no other actions will be performed.


For example:


Core code menu:

Public enum MenuTypeEnum {click = 1, view = 2, scancode_push = 3, scancode_waitmsg = 4, pic_sysphoto = 5, pic_photo_or_album = 6, pic_weixin = 7, location_select = 8 }; public abstract class SubButton {// <summary> // type of the menu response action, currently, there are two types of scancode_push: click and view. The scancode_waitmsg scan event is displayed. The prompt box "receiving messages" is displayed. /// </summary> public string type {get; protected set ;}/// <summary> // menu title, no more than 16 bytes, and no more than 40 sub-menus Bytes /// </summary> public string name {get; set;} public List <SubButton> sub_button {get; set;} public virtual bool HasError () {if (string. isNullOrEmpty (this. name) {LogInfo. error ("menu name is empty"); return true;} if (string. isNullOrEmpty (this. type) {LogInfo. error ("menu type is empty"); return true;} if (sub_button! = Null & sub_button.Count> 0) {foreach (SubButton bt in sub_button) {if (bt. hasError () {return true ;}} return false;} public static SubButton CreateSubButton (MenuTypeEnum type, string name, string key, string url) {SubButton subButton = null; string menuTypeText = GetMenuTypeText (type); switch (type) {case MenuTypeEnum. view: subButton = new SubViewButton (menuTypeText, name, url); break; case MenuTypeEnum. click: case MenuTypeEnum. scancode_push: case MenuTypeEnum. scancode_waitmsg: case MenuTypeEnum. pic_sysphoto: case MenuTypeEnum. pic_photo_or_album: case MenuTypeEnum. pic_weixin: case MenuTypeEnum. location_select: subButton = new SubClickButton (menuTypeText, name, key); break; default: throw new Exception ("type =" + type + ", subButton of this type is not implemented ");} return subButton;} public static MenuTyp EEnum GetMenuType (string type) {MenuTypeEnum text = MenuTypeEnum. click; switch (type) {case "click": text = MenuTypeEnum. click; break; case "view": text = MenuTypeEnum. view; break; case "scancode_push": text = MenuTypeEnum. scancode_push; break; case "scancode_waitmsg": text = MenuTypeEnum. scancode_waitmsg; break; case "pic_sysphoto": text = MenuTypeEnum. pic_sysphoto; break; case "pic_photo_or _ Album ": text = MenuTypeEnum. pic_photo_or_album; break; case "pic_weixin": text = MenuTypeEnum. pic_weixin; break; case "location_select": text = MenuTypeEnum. location_select; break; default: throw new Exception ("type =" + type + ",");} return text ;} public static string GetMenuTypeText (MenuTypeEnum type) {string text = ""; switch (type) {case MenuTypeEnum. click: text = "cl Ick "; break; case MenuTypeEnum. view: text = "view"; break; case MenuTypeEnum. scancode_push: text = "scancode_push"; break; case MenuTypeEnum. scancode_waitmsg: text = "scancode_waitmsg"; break; case MenuTypeEnum. pic_sysphoto: text = "pic_sysphoto"; break; case MenuTypeEnum. pic_photo_or_album: text = "pic_photo_or_album"; break; case MenuTypeEnum. pic_weixin: text = "pic_weixin"; break; case MenuType Enum. location_select: text = "location_select"; break; default: throw new Exception ("type =" + type + ", this type of MenuTypeEnum is not implemented");} return ;}} public class SubClickButton: SubButton {public SubClickButton (string type, string name, string key) {this. type = type; this. name = name; this. key = key;} // <summary> // menu KEY value, used for message interface push, no more than 128 bytes /// </summary> public string key {get; set;} public o Verride bool HasError () {if (string. isNullOrEmpty (this. key) {LogInfo. error ("menu key is empty"); return true;} return base. hasError () ;}} public class SubViewButton: SubButton {public SubViewButton (string type, string name, string url) {this. type = type; this. name = name; this. url = url;} // <summary> // when a Member clicks the view type button, the client will open the webpage URL filled in by the developer in the button, you can use the webpage authorization interface to obtain basic member information. /// </Summary> public string url {get; set;} public override bool HasError () {if (string. isNullOrEmpty (this. url) {LogInfo. error ("menu url is empty"); return true;} return base. hasError ();}}

/// <Summary> /// custom Menu /// </summary> public class Menu {public List <SubButton> button {get; set ;} public virtual bool HasError () {if (button. count> = 4) {LogInfo. error ("up to three level-1 Menus"); return true;} foreach (SubButton bt in button) {if (bt. hasError () {return true ;}} return false ;}}

Add, modify, and delete menus

Public class BLLMenu {// <summary> //// </summary> /// <param name = "info"> </param> // <param name = "agentid"> Enterprise Application id, integer. You can view </param> // <returns> </returns> public static bool Create (Menu info, int agentid) {if (info. hasError () {return false;} string urlFormat = "https://qyapi.weixin.qq.com/cgi-bin/menu/create? Access_token = {0} & agentid = {1} "; var url = string. format (urlFormat, bllaccesen en. getAccessToken (), agentid); WebUtils wut = new WebUtils (); var postData = Tools. toJsonString <Menu> (info); // the data does not need to be encrypted to send LogInfo. info ("create application menu message:" + postData); string sendResult = wut. doPost (url, postData); ReturnResult tempAccessTokenjson = Tools. jsonStringToObj <ReturnResult> (sendResult); if (tempAccessTokenjson. hasError () {LogInfo. error ("Error returned when sending the application creation menu:" + Tools. toJsonString <ReturnResult> (tempAccessTokenjson); return false;} return true ;} /// <summary> ///// </summary> /// <param name = "agentid"> the id of the enterprise application, which is an integer. You can view </param> /// <returns> </returns> public static bool DelAll (int agentid) {string urlFormat = "https://qyapi.weixin.qq.com/cgi-bin/menu/delete? Access_token = {0} & agentid = {1} "; var url = string. format (urlFormat, bllaccesen en. getAccessToken (), agentid); WebUtils wut = new WebUtils (); // the data does not need to be encrypted to send LogInfo. info ("Send Delete menu message:" + url); string sendResult = wut. doGet (url); ReturnResult tempAccessTokenjson = Tools. jsonStringToObj <ReturnResult> (sendResult); if (tempAccessTokenjson. hasError () {LogInfo. error ("Error returned when sending the delete menu:" + Tools. toJsonString <ReturnRe Sult> (tempAccessTokenjson); return false;} return true;} public static bool GetAll (int agentid) {string urlFormat = "https://qyapi.weixin.qq.com/cgi-bin/menu/get? Access_token = {0} & agentid = {1} "; var url = string. format (urlFormat, bllaccesen en. getAccessToken (), agentid); WebUtils wut = new WebUtils (); // the data does not need to be encrypted to send LogInfo. info ("Send get menu list message:" + url); string sendResult = wut. doGet (url); MenuListResult tempAccessTokenjson = Tools. jsonStringToObj <MenuListResult> (sendResult); if (tempAccessTokenjson. hasError () {LogInfo. error ("sending and retrieving menu list Return Error:" + Tools. toJsonString <MenuListResult> (tempAccessTokenjson); return false;} return true ;}}

Test code

Private void button7_Click (object sender, EventArgs e) {// test the addition of ConmonWeixin. menuInfo. menu info = new ConmonWeixin. menuInfo. menu (); SubButton subbt1 = SubButton. createSubButton (MenuTypeEnum. click, "Click1", "Click1", ""); subbt1.sub _ button = new List <SubButton> (); SubButton bt11 = SubButton. createSubButton (MenuTypeEnum. scancode_push, "codePush2", "CancodePushButton11", ""); SubButton bt12 = SubButton. createSubButton (MenuTypeEnum. scancode_waitmsg, "codeWaitmsg2", "CancodeWaitmsgButton12", ""); SubButton bt13 = SubButton. createSubButton (MenuTypeEnum. click, "Click12", "Click12", ""); SubButton bt14 = SubButton. createSubButton (MenuTypeEnum. view, "V2", "V22", "https://www.baidu.com"); subbt1.sub _ button. add (bt11); subbt1.sub _ button. add (bt12); subbt1.sub _ button. add (bt13); subbt1.sub _ button. add (bt14); SubButton subbt2 = SubButton. createSubButton (MenuTypeEnum. view, "V1", "", "www.baidu.com"); subbt2.sub _ button = new List <SubButton> (); SubButton bt21 = SubButton. createSubButton (MenuTypeEnum. pic_sysphoto, "PicSysphoto", "PicSysphotoButton21", ""); SubButton bt22 = SubButton. createSubButton (MenuTypeEnum. pic_photo_or_album, "photoalbum2", "PicSysphotoButton22", ""); subbt2.sub _ button. add (bt21); subbt2.sub _ button. add (bt22); SubButton subbt3 = SubButton. createSubButton (MenuTypeEnum. view, "V1", "", "http://hlogin.html"); subbt3.sub _ button = new List <SubButton> (); SubButton bt31 = SubButton. createSubButton (MenuTypeEnum. pic_weixin, "pic_weixin2", "Subpic_weixinButton31", ""); SubButton bt32 = SubButton. createSubButton (MenuTypeEnum. location_select, "location_selec", "location_selec32", ""); subbt3.sub _ button. add (bt31); subbt3.sub _ button. add (bt32); info. button = new List <SubButton> (); info. button. add (subbt1); info. button. add (subbt2); info. button. add (subbt3); BLLMenu. create (info, 7); // Menu info, int agentid} private void button8_Click (object sender, EventArgs e) {// test deleting BLLMenu. delAll (7 );}

Create Application menu Official Document

Official menu Receiving Event documentation

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.