In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to create and delete custom menus in the development of Wechat public platform". The content is simple and clear. I hope it can help you solve your doubts. Now let the editor lead you to study and learn how to create and delete custom menus in the development of Wechat public platform.
When creating menus, data is transferred based on JSON, so JSON is used.
There are instructions in the public platform development documentation:
Please note:
1. Custom menus include up to 3 first-level menus, and each first-level menu contains up to 5 second-level menus.
2. There are up to 4 Chinese characters in the first-level menu and 7 Chinese characters in the second-level menu. Instead.
3. After creating a custom menu, due to Wechat client cache, it takes 24 hours for Wechat client to display it. During the test, you can try to unfollow the public account and follow it again, and you can see the effect after the creation.
A custom menu interface can implement many types of buttons, as follows:
1. Click: click push event after the user clicks the click type button, the Wechat server will push the message type as event through the message API.
The structure is given to the developer (refer to the message interface guide), and with the key value entered by the developer in the button, the developer can customize
The key value of the interacts with the user
2. View: when a URL user clicks the view type button, the Wechat client will open the developer's
The web page URL filled in in the button can be combined with the web page authorization interface to obtain the basic information of the user.
3. Scancode_push: after the user clicks the button on the scan code push event, the Wechat client will call up the scan tool to display after completing the scan operation.
The scan result is displayed (if it is URL, you will enter URL), and the result of the scan will be passed to the developer, who can send a message.
4. Scancode_waitmsg: after scanning the code push event and popping up the "message receiving" prompt box, the Wechat client will call
Scan the tool. After completing the scan operation, pass the result of the scan to the developer. At the same time, put away the scan tool, and then pop up the message.
Receive "prompt box", and then you may receive a message from the developer.
5. Pic_sysphoto: after the pop-up system takes photos and sends pictures, the Wechat client will adjust the system camera after the user clicks the button. After completing the photo operation,
The photos will be sent to the developer, the event will be pushed to the developer, and the system camera will be put away at the same time, which may be received by the developer.
Sent a message.
6. Pic_photo_or_album: when a user clicks a button to take a photo or send an album, the Wechat client will pop up a selection.
The device is for users to choose "take photos" or "choose from mobile photo albums". After the user chooses, he or she will follow the other two processes.
7. Pic_weixin: after the user clicks the button in the Wechat photo album distributor, the Wechat client will call up the Wechat photo album and complete the selection operation.
After that, send the selected photos to the developer's server, and push the event to the developer, put away the photo album at the same time, and then you may receive the opening.
The message sent by the sender.
8. Location_select: when the user clicks the button in the pop-up geolocation selector, the Wechat client will adjust the location
Manage the location selection tool, after completing the selection operation, send the selected geographic location to the developer's server, and put away the location selection at the same time
Tool, you may then receive a message from the developer.
9. Media_id: send messages (except text messages) that the user clicks
After the media_id type button, the Wechat server will send the permanent material id filled in by the developer to the user and the permanent material class.
Type can be pictures, audio, video, picture and text messages. Please note: permanent footage id must be in "material Management / New permanent footage"
The legal id obtained after the API is uploaded. 10. View_limited: after the URL user clicks the view_limited type button to jump to the picture and text message
Wechat client will open the URL corresponding to the picture and text message corresponding to the permanent material id that the developer filled in in the button. The permanent material type only supports picture and text.
News. Please note: the permanent footage id must be a legitimate id obtained after uploading the "material Management / add permanent footage" interface.
Please note that all events from 3 to 8 only support Wechat users of Wechat iPhone5.4.1 and above Android5.4. Users of the old version of Wechat will not respond after clicking, and developers will not be able to receive event push normally. 9 and 10 are event types specially prepared for Subscription account who is not certified by Wechat (specifically, the qualification verification) of a third-party platform. They do not have event push, and their capabilities are relatively limited. Other types of official accounts do not need to be used.
First, because the custom menu is using http request mode, we should use https protocol. Write a method class that handles https and json data.
Create a new class under the package com.cc.wechat.util:
-CommonUtil.java:
Package com.cc.wechat.util; import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.net.URL; import javax.net.ssl.HttpsURLConnection;import javax.net.ssl.SSLContext;import javax.net.ssl.SSLSocketFactory;import javax.net.ssl.TrustManager / * author ICHN * 2015-09-04 * / public class CommonUtil {/ * initiate a https request and get the result * @ param requestUrl request address * @ param requestMethod request method (GET, POST) * @ param outputStr submitted data * @ return JSONObject (get the property value of the json object through JSONObject.get (key)) * / public static String httpsRequest (String requestUrl) String requestMethod, String outputStr) {StringBuffer sb = new StringBuffer () / / create a SSLContext object and initialize TrustManager [] tm = {new MyX509TrustManager ()} with the trust manager we specified; try {SSLContext sslContext = SSLContext.getInstance ("SSL", "SunJSSE"); sslContext.init (null, tm, new java.security.SecureRandom ()) / / get the SSLSocketFactory object SSLSocketFactory ssf = sslContext.getSocketFactory (); URL url = new URL (requestUrl); HttpsURLConnection httpsUrlConnection = (HttpsURLConnection) url.openConnection (); httpsUrlConnection.setSSLSocketFactory (ssf); httpsUrlConnection.setDoInput (true); httpsUrlConnection.setDoOutput (true) from the above SSLSocketFactory object HttpsUrlConnection.setUseCaches (false); / / set request method (GET/POST) httpsUrlConnection.setRequestMethod (requestMethod); / / A pair of request methods to determine equalsIgnoreCase is not case-sensitive if ("GET" .equals IgnoreCase (requestMethod)) {/ / establish a connection httpsUrlConnection.connect () } / / when data needs to be submitted, if (null! = outputStr) {OutputStream os = httpsUrlConnection.getOutputStream (); / / pay attention to the encoding format to prevent Chinese garbled os.write (outputStr.getBytes ("UTF-8")); os.close () } / / convert the returned input stream to the string InputStream is = httpsUrlConnection.getInputStream (); InputStreamReader isr = new InputStreamReader (is, "UTF-8"); BufferedReader br = new BufferedReader (isr); String strLine = null While ((strLine = br.readLine ())! = null) {sb.append (strLine);} br.close (); isr.close (); / / release resource is.close (); is = null } catch (Exception e) {e.printStackTrace ();} return sb.toString ();}}
Second, define various types of buttons, extract the common variables and write them in a class.
Create the related classes under the package com.cc.wechat.menu:
1-Button.java:
Package com.cc.wechat.menu; / * menu button * @ author ICHN * / public class Button {/ / menu title, no more than 16 bytes, no more than 40 bytes of submenu private String name; public String getName () {return name;} public void setName (String name) {this.name = name;}}
2-ClickButton.java:
Package com.cc.wechat.menu; / * click type button * @ author ICHN * * / public class ClickButton extends Button {/ / menu response action type private String type; / / menu key value for message API push, no more than 128 bytes of private String key; public String getType () {return type;} public void setType (String type) {this.type = type } public String getKey () {return key;} public void setKey (String key) {this.key = key;}}
3-ComplexButton.java:
The number of package com.cc.wechat.menu; / * secondary menu array * should be 1x 5 * @ author ICHN * * / public class ComplexButton extends Button {/ / secondary menu array private Button [] sub_button; public Button [] getSub_button () {return sub_button;} public void setSub_button (Button [] sub_button) {this.sub_button = sub_button;}}
4-Menu.java:
Package com.cc.wechat.menu; / * menu * @ author ICHN * * / public class Menu {private Button [] button; public Button [] getButton () {return button;} public void setButton (Button [] button) {this.button = button;}}
5-ViewButton.java:
Package com.cc.wechat.menu; / * view type button * @ author ICHN * / public class ViewButton extends Button {/ / menu response action type private String type; / / web link. Users can click the menu to open the link, no more than 256 bytes of 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;}}
Third, create a new test source folder test, create a package com.cc.wechat.test in it, and write the related classes that create the menu in this package.
Write a class that gets the access_token:
-GetAccessToken.java:
Package com.cc.wechat.test; import com.cc.wechat.util.CommonUtil; / * obtain the appID and appsecret * * access_token of the access_token * @ author ICHN * test account is the global unique ticket of the official account. Access_token is required when the official account calls each API. * developers need to save it properly. At least 512 character space should be reserved for access_token storage. * access_token is currently valid for 2 hours and needs to be refreshed regularly. * repeated acquisition will invalidate the last acquired access_token * / public class GetAccessToken {public static void main (String [] args) {/ / print out access_token System.out.println (CommonUtil.httpsRequest ("& secret= fill in appsecret", "GET", null)) }}
two。 Create and delete menus:
-MenuTest.java:
Package com.cc.wechat.test; import net.sf.json.JSONObject; import com.cc.wechat.menu.Button;import com.cc.wechat.menu.ClickButton;import com.cc.wechat.menu.ComplexButton;import com.cc.wechat.menu.Menu;import com.cc.wechat.util.CommonUtil / * creation of execution menu * @ author ICHN * * / public class MenuTest {public static void main (String [] args) {/ * button types: * click type * view type * / * click type * secondary menu 1 * the secondary menu included: * clickButton_11 *... * five * / ClickButton clickButton_11 = new ClickButton () can be defined. / / set the button name clickButton_11.setName ("); / / set the button category according to the definition clickButton_11.setType (") given in the Wechat development document; / / set the button key value clickButton_11.setKey ("); / /. Five can be defined. / * * Secondary menu 2 * contains secondary menus: * clickButton_21 *... * / ClickButton clickButton_21 = new ClickButton (); clickButton_21.setName ("); clickButton_21.setType ("); clickButton_21.setKey (") / * define an array of first-level menus, * the number should be 1: 3 * / ClickButton button_3 = new ClickButton (); button_3.setName ("); button_3.setType ("); button_3.setKey (") / * after the secondary menu above is defined, * install it with a button (ComplexButton) with secondary menu * / / first-level menu 1 ComplexButton complexButton1 = new ComplexButton (); complexButton1.setName ("first-level menu 1"); complexButton1.setSub_button (new Button [] {clickButton_11}) / / first-level menu 2 ComplexButton complexButton2 = new ComplexButton (); complexButton2.setName ("); complexButton2.setSub_button (new Button [] {clickButton_21}); / / first-level menu 3 is defined above / / use a menu (equivalent to the total menu, in the outermost layer) to install the above menu Menu menu = new Menu () Menu.setButton (new Button [] {complexButton1, complexButton2, button_3}); / / convert menu to the json array String jsonMenu = JSONObject.fromObject (menu) .toString () / * both create and delete using https protocol * http request method: POST (please use https protocol) * / / create menu interface / / https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN String createRequest = CommonUtil.httpsRequest ( / / requestUrl "https://api.weixin.qq.com/cgi-bin/menu/create?access_token= enter the access_token obtained by the above GetAccessToken class here" / / requestMethod "POST", / / outputStr jsonMenu) / / print out creation status information (create at the same time) / / System.out.println (createRequest) / / delete the menu interface / / https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN String deleteResult = CommonUtil.httpsRequest ("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token= here fill in the access_token obtained by the above GetAccessToken class", "POST" JsonMenu) / print out the deletion status information (delete at the same time) System.out.println (deleteResult);}} these are all the contents of the article "how to create and delete custom menus in the development of Wechat public platform". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.