One technology per day: wechat development – Custom Menu

Time:2021-6-1

summary

Custom menus can help official account rich interface, allowing users to understand the official account number better and faster.

Document definition

Interface call request description

HTTP request mode: Post (please use the HTTPS protocol) https://api.weixin.qq.com/cgi-bin/menu/create?access_ token=ACCESS_ TOKEN

Examples of click and view requests

{
     "button":[
     {  
          "type":"click",
          "Name": "songs of the day,",
          "key":"V1001_TODAY_MUSIC"
      },
      {
           "Name": "menu,",
           "sub_button":[
           {  
               "type":"view",
               "Name": "search,",
               "url":"http://www.soso.com/"
            },
            {
                 "type":"miniprogram",
                 "name":"wxa",
                 "url":"http://mp.weixin.qq.com",
                 "appid":"wx286b93c14bbf93aa",
                 "pagepath":"pages/lunar/index"
             },
            {
               "type":"click",
               "Name": "like us,",
               "key":"V1001_GOOD"
            }]
       }]
 }

Implementation mode

1. Define menu JSON document

 

{
  "button": [

    {
      "Name": "operation improvement",
      "sub_button": [
        {
          "type": "view",
          "Name": "free trial,",
          "url": "https://acc.bqool.cn/freetrial"
        },
        {
          "type": "view",
          "Name": "new AI price adjustment",
          "url": "https://www.bqool.cn/products/repricing-central/"
        },
        {
          "type": "view",
          "Name": "hot seller cool",
          "url": "https://www.bqool.cn/bigcentral/"
        },

        {
          "type": "view",
          "Name": "hot choice cool",
          "url": "https://www.bqool.cn/products/chrome-extension/"
        },
        {
          "type": "view",
          "Name": "more products..",
          "url": "https://www.bqool.cn/"
        }
      ]
    },
    {
      "Name": "kuzai tribe",
      "sub_button": [
        {
          "type": "view",
          "Name": "limited time benefits",
          "url": "https://mp.weixin.qq.com/mp/video?__biz=MzIzMjc1NTY3MQ==&mid=100007240&sn=04973c15f924fc78a78fec50cd75d706&vid=wxv_1500216251938308102&idx=1&vidsn=a27a0d1d9503802f9f9095534d4d2507&fromid=1&xtrack=1&scene=0&subscene=10000&clicktime=1600054770&enterid=1600054770#wechat_redirect"
        },
        {
          "type": "view",
          "Name": "successful cases",
          "url": "https://www.bqool.cn/user-stories/"
        },
        {
          "type": "view",
          "Name": "teaching video,",
          "url": "https://www.bqool.cn/video-tutorial/"
        },
        {
          "type": "view",
          "Name": "tribal articles,",
          "url": "https://mp.weixin.qq.com/mp/homepage?__biz=MzIzMjc1NTY3MQ==&hid=1&sn=db4536a338f0e128ce2e5db089f381e1&scene=18"
        }

      ]
    },
    {
      "Name": "equity center,",
      "sub_button": [
        {
          "type": "view",
          "Name": "sign in lottery",
          "url": "http://u135069.s.24hwpro.com/web/game/game_id/1220189859"
        },
        {
          "type": "view",
          "Name": "business cooperation",
          "url": "https://h5.eqxiul.com/ls/EyOfmUXk"
        },
        {
          "type": "view",
          "Name": "e-commerce services,",
          "url": "https://www.bqool.cn/seller-services/"
        },
        {
          "type": "view",
          "Name": "contact kuzai",
          "url": "https://admin.qidian.qq.com/template/blue/mp/menu/qr-code-jump.html?linkType=0&env=ol&kfuin=2852161981&fid=95&key=71d05b6c5e0a545f0be22ec831f9d7af&cate=1&type=16&ftype=1&_type=wpa&qidian=true"
        }
      ]
    }
  ]
}

2. Read JSON file

/// 
        ///Custom menu
        /// 
        /// 
        /// 
        /// 
        [BQoolException]
        [HttpPost]
        public ApiResult CustomMenus(string CustomMenusPwd)
        {
            if (!string.IsNullOrWhiteSpace(CustomMenusPwd) && CustomMenusPwd.Trim() == MvcTools.GetAppSetting("WeixinCustomMenus"))
            {
                CustomMenusParam param = new CustomMenusParam() { Account = "sys", UpdateUser = "sys" };
                param.Jsonstr = WeiXinHelper.ReadAccess(HttpRuntime.AppDomainAppPath.ToString() + "/App_Data/WeChat/Custom_Menus.json");
                ApiResult result = _weChatAlertsService.CustomMenus(param, AppId, Appsecret);
                return result;
            }
            else
            {
                return new ApiResult() { Success = false, Code = ApiResultCode.InvalidError, ErrorMessage = ApiResultCode.ErrorMessages[ApiResultCode.InvalidError] };
            }
        }

3. Post submit data to wechat server

private static Tuple CustomMenus(string AppId, string Appsecret, string jsonstr, bool isResh)
        {
            var accessToken = TryGetAccessToken(AppId, Appsecret, isResh);
            string url = APIConfig.Custom_Menus(accessToken);
            string str = APIConfig.CreatePostHttpResponse(url, jsonstr);
            _ Logger. Debug ("user defined menu" + "input" + URL + "- jsonstr +" - output "+ STR");
            return new Tuple(JsonConvert.DeserializeObject(str), url, jsonstr);
        }
 public static string WEIXIN_URL = "https://api.weixin.qq.com/";

        public static string Custom_Menus(string token)
        {
            return String.Format(WEIXIN_URL + "cgi-bin/menu/create?access_token={0}", token);
        }

Note: in general, whether creating or modifying a menu, use“CGI bin / menu / create” interface.