WeChat official account development series-custom menu creation in development mode and public Customization

Source: Internet
Author: User

Public Account Development Series-development mode Creation custom menu, public custom

Implement custom menus through programs and encapsulate Class Interaction custom menu interfaces through http requests

1. Obtain the AccessToken

Access_token is the globally unique ticket of the public account. access_token is required when the Public Account calls each interface. Under normal circumstancesAccess_token is valid for 7200 seconds, Duplicate access will invalidate the last access_token.Since the number of api calls to obtain access_token is very limited, it is recommended that developers store and update access_token globally. Refreshing access_token frequently will restrict api calls and affect their own services.

Please note that due to technical upgrades,The length of access_token for the development interface of the public platform will increase, and its storage space must be kept at least 512 characters.This modification will take effect one month later. Ask the developer to modify the compatibility as soon as possible.

The public account can use AppID and AppSecret to call this interface to obtain the access_token. AppID and AppSecret can be obtained in the Development Mode (you need to become a developer and the account is not abnormal ).Note that https is required to call all interfaces. Click to view the interface development documentation

Here, we use SettingHelper. cs to store access_token to avoid repeated acquisition;

Using System; using System. collections. generic; using System. linq; using System. text; using System. web; using System. xml. linq; public class SettingHelper {private readonly static string filePath = AppDomain. currentDomain. baseDirectory + "Config. xml "; static SettingHelper () {if (! System. IO. file. exists (filePath) SetDefSetting ();} public static void SetDefSetting () {var doc = new XDocument (); doc. add (new XElement ("root", new XElement ("accesstoken", ""), new XElement ("expires_in", ""), new XElement ("get_token_time", DateTime. now. toString (); doc. save (filePath);} public static string GetSettingByAccessToken () {var doc = XDocument. load (filePath); var token = doc. element ("Root "). element ("accesstoken "). value; var expires = doc. element ("root "). element ("expires_in "). value; var time = Convert. toDateTime (doc. element ("root "). element ("get_token_time "). value); if (string. isNullOrWhiteSpace (token) | string. isNullOrWhiteSpace (expires) return string. empty; time = time. addSeconds (Convert. toInt32 (expires); if (time> DateTime. now) return token; else return string. empty;} p Ublic static void SaveSettingByAccessToken (string accessToken, string expiresIn) {var doc = XDocument. load (filePath); doc. element ("root "). element ("accesstoken "). value = accessToken; doc. element ("root "). element ("expires_in "). value = expiresIn; doc. element ("root "). element ("get_token_time "). value = DateTime. now. toString (); doc. save (filePath) ;}/// <summary> /// obtain accesstoken /// </summary> /// <ret Urns> </returns> public static string GetAccessToken () {var pm = new Paraments (); var query = new StringBuilder (); query. append (pm. tokenUrl); query. append (string. format ("? {0} = {1} "," grant_type "," client_credential "); query. append (string. format ("& {0} = {1}", "appid", pm. appKey); query. append (string. format ("& {0} = {1}", "secret", pm. appSecret); var json = HttpHelper. get (query. toString (); var result = JsonHelper. deserialize (json); if (result. access_token = null) return string. empty; SettingHelper. saveSettingByAccessToken (string) result. access_token, (string) result. expires_in); return (string) result. access_token ;}}


2. encapsulate Class Interaction custom menu interfaces through http requests

Click here to view the previous chapter I mentioned above: Public number Development Series-Http request encapsulation base class HttPhelper. cs interface requests used.

Protected void btnCreate_Click (object sender, EventArgs e) {var accesen en = SettingHelper. getSettingByAccessToken (); if (string. isNullOrWhiteSpace (accessToken) accessToken = SettingHelper. getAccessToken (); if (string. isNullOrWhiteSpace (accessToken) {Response. write (accessToken); return;} var data = "{\" button \ ": [{\" name \ ": \" Company Profile \ ", \" sub_button \": [{\ "type \": \ "click \", \ "name \": \ "Company Profile \", \ "k Ey \ ": \" m_znq \ "},{ \" type \ ": \" click \ ", \" name \ ": \" About Us \", \ "key \": \ "m_xpdz \"}, {\ "type \": \ "click \", \ "name \": \ "transportation mode \", \ "key \": \ "m_jmt \"}]}, "; data + =" {\ "name \": \ "solution \", \ "sub_button \": [{\ "type \": \ "click \", \ "name \": \ "e-commerce solution \", \ "key \": \ "e-commerce solution \" },{ \ "type \": \ "click \", \ "name \": \ "HR personnel management solution \", \ "key \": \ "personnel management solution \" },{ \ "type \": \ "click \", \ "name \": \ "property management solution \", \ "key \": \ "property management solution \"}]}, "; data + = "{\ "Name \": \ "business field \", \ "sub_button \": [{\ "type \": \ "view \", \ "name \": \ "Business Scope \", \ "url \": \ "http://www.haiyusoft.com \" },{ \ "type \": \ "click \", \ "name \": \ "joint R & D center \", \ "key \": \ "m_about \" },{ \ "type \": \ "click \", \ "name \": \ "I want to bind \", \ "key \": \ "I want to bind \"}]} "; var json = HttpHelper. post ("https://api.weixin.qq.com/cgi-bin/menu/create? Access_token = "+ accessToken, data); Response. Write (json); Response. End ();}

Currently, a custom menu can contain up to three level-1 menus, and each level-1 menu can contain up to five level-2 menus. A level-1 menu can contain up to four Chinese characters, and a level-2 menu can contain up to seven Chinese characters. The extra content will be replaced. Please note that,After the custom menu is created, it takes 24 hours for the client to appear because of the client cache.We recommend that you try to remove the public account and follow it again during the test to see the effect after creation.

The custom menu interface supports multiple types of buttons, as shown in the following figure: Click View development documentation-create a custom menu

1. click: click to push eventsAfter you click the click type button, the server pushes the structure of the Message Type event to the developer (reference message Interface Guide) through the message interface, and carries the key value filled by the developer in the button, developers can interact with users through custom key values;2. view: Jump URLAfter you click the view type button, the client will open the webpage URL filled in by the developer in the button, which can be combined with the webpage authorization interface to obtain basic user information.3. scancode_push: QR push eventAfter you click the button, the client starts the scan tool. After scanning the code, the scan result is displayed. (if it is a URL, the scan result is displayed in the URL ), the result of the scan code is sent to the developer, and the developer can send messages.4. scancode_waitmsg: scan the code push event and the prompt box "receiving messages" appears.After the user clicks the button, the client will call the scanning tool. After scanning the code, the client will send the result to the developer and collapse the scanning tool, the prompt box "receiving messages" is displayed, and the developer may receive the message.5. pic_sysphotoAfter the user clicks the button, the client starts the System camera. After taking the photo, the client sends the photo to the developer, pushes the event to the developer, and collapse the System camera, you may receive a message from the developer later.6. pic_photo_or_album: a photo or album is displayed.After the user clicks the button, the client will pop up a selector for the user to choose "photo" or "select from mobile album ". After the user selects this option, the other two processes are followed.7. pic_weixin: the album poster is displayed.After the user clicks the button, the client will call up the album. After the selection operation is completed, the selected photo will be sent to the developer's server, and the event will be pushed to the developer. At the same time, the album will be collapsed, you may receive a message from the developer later.8. location_select: The geographical location selector is displayed.After the user clicks the button, the client starts the geographic location selection tool. After the selection is completed, the client sends the selected geographic location to the developer's server and collapse the location selection tool, you may receive a message from the developer later.

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.


My Sina Weibo: http://weibo.com/ I /1741159542



How do I edit the custom menu of the Public Account Service number in development mode? Who can solve this problem?

Only one editing mode and one development mode can be enabled. If you enable the development mode, you need to write code to set the custom menu, or find a third party to set the menu.

How can I link custom menus in the public platform development mode to my own website?

The first choice is to authenticate the public account, and then perform secondary development on the public account after authentication. That is to say, setting up a micro-website and setting a custom menu.

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.