


Share an example tutorial on developing custom menus for WeChat public accounts
May 19, 2017 pm 04:12 PMWelcome to leave a message, forward,
Click me for the project source code reference address - Welcome to Start
The previous articles have already talked about how to import the project, how to start the configuration project, how to become a developer, and what is the source code analysis news? Interaction (If the first four items are not very clear, you can read here to quickly develop WeChat public accounts. This article will talk about how to implement a custom menu
There are two ways to implement a custom menu
1. Edit mode
2. Development Mode
The editing mode is very simple and I won’t go into details...
Development mode to implement custom menu
1. Use the WeChat public platform interface debugging tool to implement it
2. Use the official interface to implement it
Early preparation
Add plug-inNote:
1. Currently,subscription accounts
can only use editing mode and cannot add hyperlinks. Development mode can only be used after WeChat authentication.
2. Editing mode and development mode cannot be turned on at the same time. 3. Generate. The menu will not be displayed immediately (the next day). If you want to see the effect immediately, you can unfollow and follow again
access_token
body
body is actually a
JSON object that needs to be generated for the menu. The official provides a chestnut for reference.
{ "button":[ { "type":"click", "name":"今日歌曲", "key":"V1001_TODAY_MUSIC" }, { "name":"菜單", "sub_button":[ { "type":"view", "name":"搜索", "url":"http://www.soso.com/" }, { "type":"view", "name":"視頻", "url":"http://v.qq.com/" }, { "type":"click", "name":"贊一下我們", "key":"V1001_GOOD" }] }] }
access_token is obtained as shown below
Students who are learning about WeChat custom menus for the first time are advised to read the official documents three times firstIn
Jfinal-weixin, there are encapsulated menu creation, query, deletion, and personalized menus Create, query, delete,
test personalized menu matching results
The following is the package provided? The interface
//查詢自定義菜單 public static ApiResult getMenu() { String jsonResult = HttpUtils.get(getMenu + AccessTokenApi.getAccessTokenStr()); return new ApiResult(jsonResult); } //創(chuàng)建自定義菜單 public static ApiResult createMenu(String jsonStr) { String jsonResult = HttpUtils.post(createMenu + AccessTokenApi.getAccessTokenStr(), jsonStr); return new ApiResult(jsonResult); } //刪除自定義菜單 public static ApiResult deleteMenu() { String jsonResult = HttpUtils.get(deleteMenuUrl + AccessTokenApi.getAccessTokenStr()); return new ApiResult(jsonResult); } //創(chuàng)建個(gè)性化自定義菜單 public static ApiResult addConditional(String jsonStr) { String jsonResult = HttpUtils.post(addConditionalUrl + AccessTokenApi.getAccessTokenStr(), jsonStr); return new ApiResult(jsonResult); } //刪除個(gè)性化自定義菜單 public static ApiResult delConditional(String menuid) { HashMap params = new HashMap(); params.put("menuid", menuid); String url = delConditionalUrl + AccessTokenApi.getAccessTokenStr(); String jsonResult = HttpUtils.post(url, JsonUtils.toJson(params)); return new ApiResult(jsonResult); } //測試個(gè)性化菜單匹配結(jié)果 public static ApiResult tryMatch(String userId) { HashMap params = new HashMap(); params.put("user_id", userId); String url = tryMatchUrl + AccessTokenApi.getAccessTokenStr(); String jsonResult = HttpUtils.post(url, JsonUtils.toJson(params)); return new ApiResult(jsonResult); } public static ApiResult getCurrentSelfMenuInfo() { String jsonResult = HttpUtils.get(getCurrentSelfMenuInfoUrl + AccessTokenApi.getAccessTokenStr()); return new ApiResult(jsonResult); }provides a detailed usage demo in the open source project weixin_guide
com.javen.weixin.menu.MenuManager class
public static void main(String[] args) { // 將菜單對象轉(zhuǎn)換成json字符串 String jsonMenu = JsonKit.toJson(getTestMenu()).toString(); System.out.println(jsonMenu); ApiConfig ac = new ApiConfig(); // 配置微信 API 相關(guān)常量 請使用你自己公眾號的 ac.setAppId("wx614c453e0d1dcd12"); ac.setAppSecret("19a02e4927d346484fc70327970457f9"); // ac.setAppId(PropKit.get("appId")); // ac.setAppSecret(PropKit.get("appSecret")); ApiConfigKit.setThreadLocalApiConfig(ac); //創(chuàng)建菜單 ApiResult apiResult=MenuApi.createMenu(jsonMenu); System.out.println(apiResult.getJson()); }You can see that the main method calls
MenuApi.createMenu(jsonMenu)that
Where does jsonMenu come from?
String jsonMenu = JsonKit.toJson(getTestMenu()).toString();
Executing the main method will output the JSON of the generated menu and the status of the response
/** * 組裝菜單數(shù)據(jù) * * @return */ private static Menu getTestMenu() { ClickButton btn11 = new ClickButton(); btn11.setName("微信相冊發(fā)圖"); btn11.setType("pic_weixin"); btn11.setKey("rselfmenu_1_1"); ClickButton btn12 = new ClickButton(); btn12.setName("拍照或者相冊發(fā)圖"); btn12.setType("pic_photo_or_album"); btn12.setKey("rselfmenu_1_2");; ClickButton btn13 = new ClickButton(); btn13.setName("系統(tǒng)拍照發(fā)圖"); btn13.setType("pic_sysphoto"); btn13.setKey("rselfmenu_1_3"); ClickButton btn21 = new ClickButton(); btn21.setName("掃碼帶提示"); btn21.setType("scancode_waitmsg"); btn21.setKey("rselfmenu_2_1");; ClickButton btn22 = new ClickButton(); btn22.setName("掃碼推事件"); btn22.setType("scancode_push"); btn22.setKey("rselfmenu_2_2");; ViewButton btn23 = new ViewButton(); btn23.setName("我的設(shè)備"); btn23.setType("view"); btn23.setUrl("https://hw.weixin.qq.com/devicectrl/panel/device-list.html?appid=wx614c453e0d1dcd12"); ViewButton btn31 = new ViewButton(); btn31.setName("微社區(qū)"); btn31.setType("view"); btn31.setUrl("http://whsf.tunnel.mobi/whsf/msg/wsq"); ClickButton btn32 = new ClickButton(); btn32.setName("發(fā)送位置"); btn32.setType("location_select"); btn32.setKey("rselfmenu_3_2"); //http://tencent://message/?uin=572839485&Site=在線咨詢&Menu=yes //http://wpa.qq.com/msgrd?v=3&uin=572839485&site=qq&menu=yes ViewButton btn33 = new ViewButton(); btn33.setName("在線咨詢"); btn33.setType("view"); btn33.setUrl("http://wpa.qq.com/msgrd?v=3&uin=572839485&site=qq&menu=yes"); ViewButton btn34 = new ViewButton(); btn34.setName("我的博客"); btn34.setType("view"); btn34.setUrl("http://www.cnblogs.com/zyw-205520"); ClickButton btn35 = new ClickButton(); btn35.setName("點(diǎn)擊事件"); btn35.setType("click"); btn35.setKey("rselfmenu_3_5"); ComButton mainBtn1 = new ComButton(); mainBtn1.setName("發(fā)圖"); mainBtn1.setSub_button(new Button[] { btn11, btn12, btn13}); ComButton mainBtn2 = new ComButton(); mainBtn2.setName("掃碼"); mainBtn2.setSub_button(new Button[] { btn21, btn22 ,btn23}); ComButton mainBtn3 = new ComButton(); mainBtn3.setName("個(gè)人中心"); mainBtn3.setSub_button(new Button[] { btn31, btn32, btn33, btn34 ,btn35 }); /** * 這是公眾號xiaoqrobot目前的菜單結(jié)構(gòu),每個(gè)一級菜單都有二級菜單項(xiàng)<br> * * 在某個(gè)一級菜單下沒有二級菜單的情況,menu該如何定義呢?<br> * 比如,第三個(gè)一級菜單項(xiàng)不是“更多體驗(yàn)”,而直接是“幽默笑話”,那么menu應(yīng)該這樣定義:<br> * menu.setButton(new Button[] { mainBtn1, mainBtn2, btn33 }); */ Menu menu = new Menu(); menu.setButton(new Button[] { mainBtn1, mainBtn2, mainBtn3 }); return menu; }
The above is the whole process of generating a custom menu.
【Related recommendations】
1.
WeChat public account platform source code downloadWeizhichuang T+ WeChat robot source codeWeChat Network King v3.4.5 Advanced Business Edition WeChat Rubik’s Cube source codeThe above is the detailed content of Share an example tutorial on developing custom menus for WeChat public accounts. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PHP is an open source scripting language that is widely used in web development and server-side programming, especially in WeChat development. Today, more and more companies and developers are starting to use PHP for WeChat development because it has become a truly easy-to-learn and easy-to-use development language. In WeChat development, message encryption and decryption are a very important issue because they involve data security. For messages without encryption and decryption methods, hackers can easily obtain the data, posing a threat to users.

How to write custom menu functions for CMS systems in Python When developing and designing a CMS (content management system), custom menu functions are a very important part. It allows users to customize the menu according to their needs and preferences for quick access to various functions and pages. In this article, we will write a simple CMS system using Python and add custom menu functionality. First, we need to create a menu class to store and manage menu item information. Each menu item contains a name and corresponding page path

In the development of WeChat public accounts, the voting function is often used. The voting function is a great way for users to quickly participate in interactions, and it is also an important tool for holding events and surveying opinions. This article will introduce you how to use PHP to implement WeChat voting function. Obtain the authorization of the WeChat official account. First, you need to obtain the authorization of the WeChat official account. On the WeChat public platform, you need to configure the API address of the WeChat public account, the official account, and the token corresponding to the public account. In the process of our development using PHP language, we need to use the PH officially provided by WeChat

With the popularity of WeChat, more and more companies are beginning to use it as a marketing tool. The WeChat group messaging function is one of the important means for enterprises to conduct WeChat marketing. However, if you only rely on manual sending, it is an extremely time-consuming and laborious task for marketers. Therefore, it is particularly important to develop a WeChat mass messaging tool. This article will introduce how to use PHP to develop WeChat mass messaging tools. 1. Preparation work To develop WeChat mass messaging tools, we need to master the following technical points: Basic knowledge of PHP WeChat public platform development Development tools: Sub

WeChat is currently one of the social platforms with the largest user base in the world. With the popularity of mobile Internet, more and more companies are beginning to realize the importance of WeChat marketing. When conducting WeChat marketing, customer service is a crucial part. In order to better manage the customer service chat window, we can use PHP language for WeChat development. 1. Introduction to PHP WeChat development PHP is an open source server-side scripting language that is widely used in the field of Web development. Combined with the development interface provided by WeChat public platform, we can use PHP language to conduct WeChat

In the development of WeChat public accounts, user tag management is a very important function, which allows developers to better understand and manage their users. This article will introduce how to use PHP to implement the WeChat user tag management function. 1. Obtain the openid of the WeChat user. Before using the WeChat user tag management function, we first need to obtain the user's openid. In the development of WeChat public accounts, it is a common practice to obtain openid through user authorization. After the user authorization is completed, we can obtain the user through the following code

As WeChat becomes an increasingly important communication tool in people's lives, its agile messaging function is quickly favored by a large number of enterprises and individuals. For enterprises, developing WeChat into a marketing platform has become a trend, and the importance of WeChat development has gradually become more prominent. Among them, the group sending function is even more widely used. So, as a PHP programmer, how to implement group message sending records? The following will give you a brief introduction. 1. Understand the development knowledge related to WeChat public accounts. Before understanding how to implement group message sending records, I

How to use PHP to develop WeChat public accounts WeChat public accounts have become an important channel for promotion and interaction for many companies, and PHP, as a commonly used Web language, can also be used to develop WeChat public accounts. This article will introduce the specific steps to use PHP to develop WeChat public accounts. Step 1: Obtain the developer account of the WeChat official account. Before starting the development of the WeChat official account, you need to apply for a developer account of the WeChat official account. For the specific registration process, please refer to the official website of WeChat public platform
