1. Customize menu description and button type
1. Menu description
1) Custom menus include up to 3 first-level menus, and each first-level menu contains up to 5 second-level menus.
2) The first-level menu has up to 4 Chinese characters, and the second-level menu has up to 7 Chinese characters. The extra parts will be replaced by "...".
3) After creating a custom menu, the menu refresh strategy is that when the user enters the official account session page or the official account profile page, if he finds that the last request to pull the menu was 5 minutes ago, he will pull the menu. If the menu is updated, the client's menu will be refreshed. During the test, you can try to unfollow the public account and follow it again, and you can see the effect after creation.
2. Custom menu interface can implement multiple types of buttons
1) click: After the user clicks the click type button when clicking the push event, the WeChat server will push the structure of the message type event to the developer through the message interface (refer to the message interface guide), and bring the key value filled in by the developer in the button. The developer can interact with the user through the customized key value;
2) view: Jump URL After the user clicks the view type button, the WeChat client will open the web page URL filled in by the developer in the button, which can be combined with the web page authorization to obtain user basic information interface to obtain user basic information.
3) scancode_push: After the user clicks the button for the scan code push event, the WeChat client will call up the scan tool, and display the scan result after completing the scan code operation (if it is a URL, it will enter the URL), and the scan result will be passed to the developer, and the developer can send a message.
4) scancode_waitmsg: Scan the code to push the event and the "Message Receive" prompt box pops up. After the user clicks the button, the WeChat client will call up the scan tool. After completing the scan operation, the result of the scan code is passed to the developer. At the same time, the scan tool is closed, and the "Message Receive" prompt box pops up, and then a message sent by the developer may be received.
5) pic_sysphoto: After the user clicks the button, the WeChat client will adjust the system camera. After completing the photo operation, the captured photos will be sent to the developer and push the event to the developer. At the same time, the system camera will be closed, and the message sent by the developer may be received.
6) pic_photo_or_album: After the user clicks the button, the WeChat client will pop up the selector for the user to select "take photos" or "select from mobile phone album". After the user selects, he will go through the other two processes.
7) pic_weixin: After the user clicks the button on the WeChat photo album sender, the WeChat client will adjust the WeChat photo album. After completing the selection operation, the selected photo will be sent to the developer's server and push the event to the developer. At the same time, the album will be closed. Then, the message sent by the developer may be received.
8) location_select: After the user clicks the button when the user clicks the button, the WeChat client will call up the geolocation selection tool. After completing the selection operation, the selected geographical location will be sent to the developer's server, and the location selection tool will be closed. Then, the message sent by the developer may be received.
9) media_id: After the user clicks the media_id type button, the WeChat server will send the material corresponding to the permanent material id filled in by the developer to the user. The permanent material types can be pictures, audio, video, and graphic messages. Please note: The permanent material id must be the legal id obtained after uploading the "Material Management/Add permanent material" interface.
10) view_limited: Jump the graphic message URL After the user clicks the view_limited type button, the WeChat client will open the graphic message URL corresponding to the permanent material id filled in by the developer in the button. The permanent material type only supports graphic message. Please note: The permanent material id must be the legal id obtained after uploading the "Material Management/Add permanent material" interface.
Note: All events from 3 to 8 only support WeChat users of iPhone 5.4.1 or above, and Android 5.4 or above. Users of the old version of WeChat will not respond after clicking, and developers cannot receive event pushes normally. 9 and 10 are event types specially prepared for subscription accounts under third-party platforms that have not been certified by WeChat (specifically, they are not approved by qualification certification). They have no event push and their capabilities are relatively limited. Other types of public accounts do not need to be used.
2. Create/query/delete menus
Official click and view event demo
{ "button":[ { "type":"click", "name":"Today's song", "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" }] }] }Other types (including 9 and 10)
{ "button": [ { "name": "Scancode_waitmsg", "name": "Scancode with prompt", "key": "selfmenu_0_0", "sub_button": [ ] }, { "type": "scancode_push", "name": "Scancode_push", "name": "Scancode push event", "key": "selfmenu_0_1", "sub_button": [ ] } ] }, { "name": "send a picture", "sub_button": [ { "type": "pic_sysphoto", "name": "System photos and post pictures", "key": "selfmenu_1_0", "sub_button": [ ] }, { "type": "pic_photo_or_album", "name": "take photos or albums to post pictures", "key": "selfmenu_1_1", "sub_button": [ ] }, { "type": "pic_weixin", "name": "WeChat album photos and post pictures", "key": "selfmenu_1_2", "sub_button": [ ] } ] }, { "name": "send location", "type": "location_select", "key": "selfmenu_2_0" }, { "type": "media_id", "name": "picture", "media_id": "MEDIA_ID1" }, { "type": "view_limited", "name": "graphic message", "media_id": "MEDIA_ID2" } ] }1. Start encapsulating the entity class according to the example
Menu button base class BasicButton.java
public class BasicButton { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } Menu.java
public class Menu { public final static String CLICK = "click"; // click menu public final static String VIEW = "view"; // url menu public final static String SCANCODE_WAITMSG = "scancode_waitmsg"; // Scan the code to send a prompt public final static String SCANCODE_PUSH = "scancode_push"; // Scan the code to push the event public final static String PIC_SYSPHOTO = "pic_sysphoto"; // System to take photos and post public final static String PIC_PHOTO_OR_ALBUM = "pic_photo_or_album"; // Take a photo or post a photo album public final static String PIC_WEIXIN = "pic_weixin"; // Post a photo on WeChat public final static String LOCATION_SELECT = "location_select"; // Send location private BasicButton[] button; public BasicButton[] getButton() { return button; } public void setButton(BasicButton[] button) { this.button = button; } } View type button class ViewButton.java. Other types can be packaged one by one according to this.
public class ViewButton extends BasicButton { private String type = Menu.VIEW; private String url; public String getType() { return type; } public void setType(String type) { this.type = type; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } }The first-level menu contains the encapsulation of the second-level menu ComplexMenu.java
public class ComplexMenu extends BasicButton { private BasicButton[] sub_button; public BasicButton[] getSub_button() { return sub_button; } public void setSub_button(BasicButton[] sub_button) { this.sub_button = sub_button; } }2. Complete packaging, assemble the menu
private static Menu getMenu() { ViewButton btn11 = new ViewButton(); btn11.setName("test 11"); btn11.setUrl("http://www.qq.com"); ClickButton btn21 = new ClickButton(); btn21.setName("test 21"); btn21.setKey("21"); ClickButton btn22 = new ClickButton(); btn22.setName("test 22"); btn22.setKey("22"); //Level 1 menu (no secondary menu) ComplexMenu mainBtn1 = new ComplexMenu(); mainBtn1.setName("test 1"); mainBtn1.setSub_button(new BasicButton[] { btn11}); //Level 1 menu (with secondary menu) ComplexMenu mainBtn2 = new ComplexMenu(); mainBtn2.setName("test 2"); mainBtn2.setSub_button(new BasicButton[] { btn21, btn22 }); Menu menu = new Menu(); menu.setButton(new BasicButton[] { mainBtn1, mainBtn2 }); return menu; }3. Creation of custom menus
/** * Menu created* * @param menu menu item* @param token Authorization token * @return {"errcode":0,"errmsg":"ok"} */ public ResultState createMenu(Menu menu, String token) { TreeMap<String, String> map = new TreeMap<String, String>(); map.put("access_token", token); String jsonData = JsonUtil.toJson(menu).toString(); String result = HttpReqUtil.HttpsDefaultExecute(HttpReqUtil.POST_METHOD, WechatConfig.MENU_CREATE_URL, map, jsonData); return JsonUtil.fromJson(result, ResultState.class); }4. Query of custom menus
Returned instance
Corresponding to create an interface, the correct Json returns the result:
{ "menu": { "button": [ { "type": "click", "name": "Today's Song", "key": "V1001_TODAY_MUSIC", "sub_button": [ ] }, { "type": "click", "name": "Singer Profile", "key": "V1001_TODAY_SINGER", "sub_button": [ ] }, { "name": "menu", "sub_button": [ { "type": "view", "name": "search", "url": "http://www.soso.com/", "sub_button": [ ] }, { "type": "view", "name": "video", "url": "http://v.qq.com/", "sub_button": [ ] }, { "type": "click", "name": "like us", "key": "V1001_GOOD", "sub_button": [ ] } ] } ] } } /** * Get custom menu* * @param token * @return */ public String getMenu(String token) { TreeMap<String, String> map = new TreeMap<String, String>(); map.put("access_token", token); String result = HttpReqUtil.HttpsDefaultExecute(HttpReqUtil.GET_METHOD, WechatConfig.MENU_GET_URL, map, ""); return result; }MenuAttr.java
/** * All properties of menu* @author phil * */ public class MenuAttr extends BasicMenu { private String type; private String url; private String key; private String sub_button; get/set method} The returned menu class MenuReturn.java
/** * Returned menu class* @author phil * */ public class MenuReturn extends BasicMenu{ private MenuAttr[] sub_button; public MenuAttr[] getSub_button() { return sub_button; } public void setSub_button(MenuAttr[] subButton) { sub_button = subButton; } }Convert json-formatted strings to Menu objects
/** * Convert a string in json format to a Menu object* @param json * @return */ public List<MenuReturn> convertMenu(String json) { List<MenuReturn> list = new ArrayList<MenuReturn>(); if (json!= null && !"".equals(json)) { JSONObject object = JSONObject.parseObject(json); JSONArray array = object.getJSONObject("menu").getJSONArray("button"); for (int i = 0; i < array.size(); i++) { MenuReturn mr= new MenuReturn(); mr= array.getObject(i, MenuReturn.class); list.add(mr); } } return list; }Note: Fastjson used here
Here is a method that needs to be improved. Please give me some advice if you have any better ones
5. Delete custom menu
/** * Delete custom menu* * @param token * @return */ public boolean deleteMenu(String token) { boolean falg = true; TreeMap<String, String> map = new TreeMap<String, String>(); map.put("access_token", token); String result = HttpReqUtil.HttpsDefaultExecute(HttpReqUtil.GET_METHOD, WechatConfig.MENU_DELTE_URL, map, ""); ResultState state = JsonUtil.fromJson(result, ResultState.class); if (state.getErrcode()!= 0|| state.getErrmsg() != "ok") { false = false; } return false; }3. Custom menu event push
After the user clicks on the custom menu, WeChat will push the click event to the developer. Please note that the click menu pops up the submenu and will not generate a report. Please note that all events from the third to the eighth only support WeChat users of the iPhone 5.4.1 or above, and Android 5.4 or above. Users of the old version of WeChat will not respond after clicking, and developers cannot receive event pushes normally.
1. Analyze the XML data packet pushed by WeChat
/** * Parsing requests from WeChat (XML) * xml example* <xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[FromUser]]></FromUserName> <CreateTime>123456789</CreateTime> <MsgType><![CDATA[event]]></MsgType> <Event><![CDATA[CLICK]]></Event> <EventKey><![CDATA[EVENTKEY]]></EventKey> </xml> * @param request * @return * @throws Exception */ public static Map<String, String> parseXml(HttpServletRequest request) throws Exception { // Store the parsed result in HashMap Map<String, String> map = new HashMap<String, String>(); // Get the input stream from the request InputStream inputStream = request.getInputStream(); // Read the input stream SAXReader reader = new SAXReader(); Document document = reader.read(inputStream); // Get the xml root element Element root = document.getRootElement(); // Get all child nodes of the root element List<Element> elementList = root.elements(); // traverse all child nodes for (Element e : elementList) map.put(e.getName(), e.getText()); // Free the resource inputStream.close(); inputStream = null; return map; }2. Use the map's get(key) to get value
MsgType message type, event
Event event type, CLICK
EventKey event KEY value corresponds to the KEY value in the custom menu interface
Note: key is the parameter name
Attachment: WechatConfig.java
// Create a menu public static final String MENU_CREATE_URL = "https://api.weixin.qq.com/cgi-bin/menu/create"; // Query the custom menu public static final String MENU_GET_URL = "https://api.weixin.qq.com/cgi-bin/menu/get"; // Delete the custom menu public static final String MENU_DELTE_URL = "https://api.weixin.qq.com/cgi-bin/menu/delete";
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.