Custom menu of WeChat development -- weixin-java-tools

Source: Internet
Author: User
During the development process, relevant menus will be designed for use. this time, we will introduce how to use weixin-java-tools to manage menus 1. Preface

During the development process, relevant menus will be designed for use. this time, we will introduce how to use weixin-java-tools to manage menus.

II. Custom menu categories

1. click: After you click the "click type" button for a push event, the server pushes the structure of the message type "event" to the developer through the Message Interface (reference Message Interface Guide ), and the key value filled by the developer in the button. the developer can interact with the user through the custom key value;

2. view: after a jump URL user clicks the view type button, the client will open the webpage URL entered by the developer in the button, which can be combined with the webpage authorization interface to obtain user basic information, obtain basic user information.

3. scancode_push: After you click the scan button, the client starts the scan tool and displays the scan result (if it is a URL, it will enter the URL ), the result of the scan code is sent to the developer, and the developer can send messages.

4. scancode_waitmsg, send the result of the scan code to the developer, collapse the scan tool, and then pop up the message receiving prompt box, which may then receive the message sent by the developer.

5. pic_sysphoto: After you click a button to bring up a system photo and send a picture, the client starts the system camera and sends the photo to the developer, push the event to the developer, collapse the system camera, and then receive the message from the developer.

6. pic_photo_or_album: After you click a button to bring up a photo or album, the client will pop up a selector for you to choose "take a photo" or "select from mobile phone Album ". After the user selects this option, the other two processes are followed.

7. pic_weixin: After you click the button in the pop-up album poster, the client starts the album. after selecting the album, the client sends the selected photo to the developer's server, push the event to the developer, collapse the album, and then receive the message from the developer.

8. location_select, at the same time, collapse the location selection tool and you may receive a message from the developer.

9. media_id: After a downstream message (except text message) user clicks the media_id button, the server will send the materials corresponding to the permanent material id filled by the developer to the user, permanent materials can be Images, audios, videos, and text messages. Note: The permanent clip id must be the valid id obtained after the "clip management/add permanent clip" API is uploaded.

10. view_limited: Jump to the text message URL. after you click the view_limited type button, the client will open the text message URL corresponding to the permanent material id entered by the developer in the button, the permanent material type only supports text messages. Note: The permanent clip id must be the valid id obtained after the "clip management/add permanent clip" API is uploaded.

  Please note that all events from 3 to 8 only support users of iPhone 5.4.1 and later versions, and users of Android 5.4 or later versions do not respond after clicking the old version, the developer cannot receive event push normally. 9 and 10 are events that are specially prepared for subscription numbers of unauthenticated third-party platforms (specifically, they are not certified). they are not pushed by events, capability is relatively limited, and other types of public accounts do not need to be used.

3. call the interface address: http request method: POST (please use https protocol) https://api.weixin.qq.com/cgi-bin/menu/create? Access_token = ACCESS_TOKEN

IV. request example

{"Button": [{"type": "click", "name": "Today's songs", "key": "V1001_TODAY_MUSIC" },{ "name ": "Menu", "sub_button": [{"type": "view", "name": "Search", "url": "http://www.soso.com /"}, {"type": "view", "name": "video", "url": "http://v.qq.com/" },{ "type": "click", "name ": "Like Us", "key": "V1001_GOOD"}]}

V. parameter description

Parameters Required? Description
Button Yes Level 1 menu array. the number should be 1 ~ 3
Sub_button No List menu array, the number should be 1 ~ Five
Type Yes Menu response action type
Name Yes Menu title, no more than 16 bytes, and no more than 40 bytes
Key Click and other click types are required Menu KEY value, used for Message Interface push, no more than 128 bytes
Url View type is required Webpage link. you can click the menu to open the link, up to 1024 bytes
Media_id Media_id and view_limited are required. Call the valid media_id returned by the new permanent material interface

6. code implementation:

  

Package com. weixin. menu; import java. util. arrayList; import java. util. list; import me. chanjar. weixin. common. bean. wxMenu; import me. chanjar. weixin. common. bean. wxMenu. wxMenuButton; import me. chanjar. weixin. common. exception. wxErrorException; import me. chanjar. weixin. mp. api. wxMpInMemoryConfigStorage; import me. chanjar. weixin. mp. api. wxMpService; import me. chanjar. weixin. mp. api. wxMpServiceImpl; public class WeixinMenuService {public static void main (String [] args) {// create a menu // create a level-1 menu WxMenuButton button1 = new WxMenuButton (); button1.setType ("click"); // click the event button button1.setName ("click menu"); button1.setKey ("key1 "); // Obtain The clicked menu based on the flag. // create a composite menu WxMenuButton button2 = new WxMenuButton (); button2.setName ("multilevel menu"); WxMenuButton button2_1 = new WxMenuButton (); button2_1.setType ("click"); // click the event button button2_1.setName ("sub-menu 1"); button2_1.setKey ("key2 "); // Obtain the WxMenuButton button2_2 = new WxMenuButton (); button2_2.setType ("click") according to the icon; // click the event button button2_2.setName ("Sub Menu 2 "); keys ("key3"); // Obtain the WxMenuButton button3 = new WxMenuButton (); button3.setName ("url menu"); button3.setType ("view"); button3.setUrl (" http://www.baidu.com "); // HttpList must be added
 
  
SubButtons = new ArrayList
  
   
(); SubButtons. add (button2_1); subButtons. add (button2_2); button2.setSubButtons (subButtons); List
   
    
Buttons = new ArrayList
    
     
(); Buttons. add (button1); buttons. add (button2); buttons. add (button3); WxMenu menu = new WxMenu (); menu. setButtons (buttons); // send request creation menu WxMpService service = new WxMpServiceImpl (); WxMpInMemoryConfigStorage wxConfigProvider = new WxMpInMemoryConfigStorage (); wxConfigProvider. setAppId ("wx60a8f1c3a95b0b9c"); wxConfigProvider. setSecret ("5b0e8613b538da5ac4bbc610998f10ba"); service. setWxMpConfigStorage (wxConfigProvider); try {service. menuCreate (menu);} catch (WxErrorException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}
    
   
  
 

7. click menu event push

7.1 Message format

  

 
  toUser
  
  FromUser
  
  
   123456789
  
  event
  
  CLICK
  
  EVENTKEY
  
 

  

7.2 parameter description

  

Parameters Description
ToUserName Developer ID
FromUserName Sender account (one OpenID)
CreateTime Message creation time (integer)
MsgType Message type, event
Event Event type, CLICK
EventKey Event KEY value, which corresponds to the KEY value in the custom menu interface

  

7.3 message processing

Determine the message type in the post that receives the message and perform relevant processing.

// Obtain the message stream WxMpXmlMessage message = WxMpXmlMessage. fromXml (request. getInputStream (); if (message. getMsgType (). equals ("event") {// event // Determine eventif (message. getEvent (). equals ("click") & message. getEventKey (). equals ("key1") {// do something }}

  

The above is the detailed content of weixin-java-tools, a custom menu for development. For more information, see other related articles on php!

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.