C # WeChat portal and application development (29)-Implementation of WeChat personalized menu,

Source: Internet
Author: User
Tags tojson

C # development portal and application (29)-Implementation of personalized menus,

I have not followed the topic for a while to continue to introduce the functional modules. During this time, I have made many changes, in view of these features, I fully checked the related public and enterprise account interfaces and updated the original API and system management, this article and subsequent articles are based on the summary of this work, with a view to introduce the functional modules involved using the C # implementation method. This article mainly introduces the implementation of the personalized menu of the public number. Although the public number and the enterprise number are similar in function, there is no interface for the personalized menu on the enterprise number.

1. Personalized menu Introduction

First, let's take a look at the introduction of personalized menus. According to official materials, we will introduce them as follows.

To help the public account Achieve flexible business operations, the public platform has added a personalized menu interface through which developers can view different custom menus for different user groups of the public account. This API is available to authenticated subscription numbers and Authenticated Service numbers.

Developers can set the menu displayed by the user using the following conditions:

1. User grouping (developer's business needs can be completed through user grouping) 2. Gender 3. Mobile phone OS 4. region (region set by the user on the client) 5. Language (Language set by the user on the client)

Description of personalized menu interface:

1. The personalized menu requires that the user's client version be iPhone6.2.2 or Android 6.2.4 or later. 2. When you enter the public account session page or public account profile page, if you find that the last menu pulling request was 5 minutes ago, the menu will be pulled. If the menu is updated, the menu of the client will be refreshed. During the test, you can try to remove the public account and then follow it again. Then you can see the created effect. 3. The number of new interfaces for personalized menus of common public accounts is limited to 2000 times per day, the number of interfaces for deletion is also 2000, and the number of interfaces for testing personalized menu matching results is 20000. 4. For security reasons, all personalized menus of a public account, you can only set it to link to a maximum of three domain names. 5. Before creating a personalized menu, you must create a default menu (the default menu is a menu created by using a common custom menu Creation Interface ). If you delete the default menu, the personalized menu will also be deleted.

Description of personalized menu matching rules:

When multiple personalized menus are created for the public account, they are matched one by one from new to old in the Publishing order until the user information matches the matchrule. If none of the personalized menus match successfully, the default menu is returned. For example, the default menu, personalized menu 1, personalized menu 2, and personalized menu 3 are successively released by the public account. When the user enters the public number page, the matching starts from the personalized menu 3. If the personalized menu 3 matches successfully, the user will directly return the personalized menu 3; otherwise, the user will continue to try to match the personalized menu 2, until a menu is successfully matched. According to the above matching rules, in order to avoid confusion of the menu effective time, it is decided that the personalized menu editing API will not be provided. When developers need to update the menu, they need to re-release the complete configuration.

In addition to the normal default menu, the personalized Menu provides the following processing operations.

1. Create a personalized menu

2. delete a personalized menu

3. Test personalized menu matching results

4. query personalized menus

5. Delete All menus

The custom menu function allows sellers to display the menus of public accounts based on the group, gender, mobile phone OS, and even region of fans.

For example, the Public number of a fitness club sets the custom menu based on the gender of the fans. When a male fan enters the public account, the menu content of interest to the male is displayed, such as Taekwondo and boxing. If it is a female fan, the menu courses that are of interest to female fans, such as yoga, dance, and anti-body training will be displayed.

 

2. C # design and processing of personalized menus

The menu is an important part of processing, because it is an important part of user portal experience and is also a very intuitive interface presentation, the menu display requires us to blink our brains to streamline the interface elements. Due to the importance of menus and the richness of functions, I have introduced various menu processing in several articles.

In C # development portal and application (6)-portal menu management operations, C # development portal and application (9) -portal menu management and submission to server, C # development portal and application (11)-menu presentation methods, and C # development portal and application (20) -Menu Management of Enterprise numbers has introduced relevant menus respectively. For more information, see.

The menu module can share a model between the Public Account and the enterprise account. Therefore, we place all the content in the shared module, including its entity information (such as the menu module ), although some fields do not exist in some places, we can process them using JSON identifiers of object classes to implement flexible entity information bearer.

The following code is used.

/// <Summary> /// basic menu information (public Account and enterprise account, the same as other items) /// </summary> public class MenuJson: baseJsonResult {/// <summary> /// button description. The button name must not exceed 16 bytes, sub-menu cannot exceed 40 bytes /// </summary> public string name {get; set ;}/// <summary> /// button type (click or view) /// </summary> [JsonProperty (NullValueHandling = NullValueHandling. ignore)] public string type {get; set ;}/// <summary> // button KEY value, used for message interface (event type) push, up to 128 bytes /// </su Mmary> [JsonProperty (NullValueHandling = NullValueHandling. ignore)] public string key {get; set ;}/// <summary> // webpage link. You can click the button to open the link, no more than 256 bytes // </summary> [JsonProperty (NullValueHandling = NullValueHandling. ignore)] public string url {get; set ;}/// <summary> // required for the media_id and view_limited types, call the valid media_id returned by the new permanent material Interface </summary> [JsonProperty (NullValueHandling = NullValueHandling. ignore)] pu Blic string media_id {get; set ;}/// <summary> // the array of child buttons. The number of buttons should be 2 ~ Five /// </summary> [JsonProperty (NullValueHandling = NullValueHandling. Ignore)] public List <MenuJson> sub_button {get; set ;}

The above Entity class is the basic information of menu information. We need to expand several entity classes on this basis to facilitate information conversion during building and obtaining menu information, the design of the following class diagrams is shown.

Through the relationship between these classes, we can adapt to all the default and personalized menus, as well as the menu Information Conversion Processing of enterprise numbers, and convert the corresponding entity class through JSON, implement strong type processing of information.

The code of the API implementation class processed by the menu is as follows.

/// <Summary> /// menu-related operations /// </summary> public class MenuApi: IMenuApi

In this way, we add several personalized Interfaces Based on IMenuApi.

/// <Summary> /// create a personalized menu /// </summary> /// <param name = "accessToken"> call interface creden< </param> /// <param name = "menuJson"> menu object </param> // <returns> </returns> string CreateConditionalMenu (string accessToken, menuListJson menuJson ); /// <summary> /// delete a personalized menu based on the menu ID /// </summary> /// <param name = "accessToken"> call interface credential </param> /// <param name = "menuid"> menu ID </param> /// <returns> </returns> CommonResult DeleteConditionalMenu (string accessToken, string menuid ); /// <summary> /// test the personalized menu matching result /// </summary> /// <param name = "accessToken"> call interface credential </param>/ // <returns> </returns> MenuListJson TryMatchConditionalMenu (string accessToken, string user_id );

Then implement the relevant logic processing in the MenuApi implementation class.

The following is an interface description for creating a personalized menu.

Create personalized menu

Http Request Method: POST (Please use https protocol)

https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token=ACCESS_TOKEN

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"}], "matchrule": {"group_id": "2", "sex": "1 ", "country": "China", "province": "Guangdong", "city": "Guangzhou", "client_platform_type": "2" "language": "zh_CN "}}

The information above is as follows.

MenuListJson menuJson

The API returns the following results.

{"menuid":"208379533"}

To carry this information, we need to define an entity class to carry this information.

/// <Summary> /// return the MenuId result /// </summary> public class MenuIdResult {/// <summary> /// menu ID /// </summary> public string menuid {get; set ;}}

Finally, we only need to obtain the menu ID value in the API, which is of the string type. The specific implementation code of the API is as follows.

/// <Summary> /// create a personalized menu /// </summary> /// <param name = "accessToken"> call interface creden< </param> /// <param name = "menuJson"> menu object </param> // <returns> </returns> public string CreateConditionalMenu (string accessToken, menuListJson menuJson) {string result = ""; var url = string. format ("https://api.weixin.qq.com/cgi-bin/menu/addconditional? Access_token = {0} ", accessToken); // The matchrule cannot be empty. checkForNullReference (menuJson. matchrule, "matchrule"); string postData = menuJson. toJson (); MenuIdResult list = JsonHelper <MenuIdResult>. convertJson (url, postData); if (list! = Null) {result = list. menuid;} return result ;}

The interface for deleting a personalized menu is similar, as shown below.

/// <Summary> /// delete a personalized menu based on the menu ID /// </summary> /// <param name = "accessToken"> call interface credential </param> /// <param name = "menuid"> menu ID </param> /// <returns> </returns> public CommonResult DeleteConditionalMenu (string accessToken, string menuid) {var url = string. format ("https://api.weixin.qq.com/cgi-bin/menu/delconditional? Access_token = {0} ", accessToken); var data = new {menuid = menuid}; string postData = data. ToJson (); return Helper. GetExecuteResult (url, postData );}

In addition, for personalized menus, the Operation interfaces for querying and deleting all menus are the same as those for normal default menus. Therefore, you can reuse these two interfaces. However, because we have extended the information classes returned by queries, therefore, for a personalized menu, the returned JSON string contains the conditionalmenu attribute. This is a set, that is, a personalized menu is a set of personalized menus with multiple rules.

The JSON data structure of the menu is as follows.

 

3. Use of personalized menu encapsulation Interfaces

With the encapsulation of the above interface, we can easily create a personalized menu. when building a personalized menu, we need to put all the menus that need to appear in a conditionalmenu unit, although multiple such units appear in the set.

As shown in the following code, building a personalized menu project is different from a normal default menu. It requires you to specify a matchrule object, as shown in the following code.

MenuJson conditional = new MenuJson ("related link", new MenuJson [] {new MenuJson ("personalized menu", ButtonType. click, "event_company")}); MatchRule rule = new MatchRule () {sex = "1"}; MenuListJson menuJson = new MenuListJson (); menuJson. button. addRange (new MenuJson [] {conditional}); menuJson. matchrule = rule;

Through the code above, if we need to add multiple personalized menus, we can use this method to create a personalized menu each time, of course, if we use the query results of the personalized menu, we can see that if multiple objects are created, the conditionalmenu set contains multiple objects.

The following figure shows how to add a personalized menu each time you call the code.

String menuid = menuBLL. CreateConditionalMenu (token, menuJson); Console. WriteLine ("create menu:" + (! String. IsNullOrEmpty (menuid )? "Success:" + menuid: "failed "));

After creating multiple rules, we can use TryMatch to check the matching results, that is, different personalized menu lists will appear for different users (openids.

Var myResult = menuBLL. TryMatchConditionalMenu (token, openId); if (myResult! = Null) {Console. writeLine (myResult. toJson ();} var userid_female = "oSiLnt2J4mYkhVG3aLTdMIF1hv-s"; // female ID myResult = menuBLL. tryMatchConditionalMenu (token, userid_female); if (myResult! = Null) {Console. WriteLine (myResult. ToJson ());}

 

If you are interested in this series of C # development portals and applications, you can follow my other articles as follows:

C # development portal and application (28) -- Use of the "shake around" function and implementation of interfaces

C # development portal and application (27)-Public Account template Message Management

C # development portal and application (26)-Material Management of Public Accounts

C # development portal and application (25)-enterprise account Client Management Function

C # development portal and application (24)-store shelf Information Management

C # development portal and application (23)-packaging and testing of the store product management interface

C # development portal and application (22)-Development and Use of stores

C # development portal and application (21)-receiving, processing, and decryption of enterprise numbers and events

C # development portal and application (20)-menu management of enterprise number

C # development portal and application (19)-sending of enterprise numbers (text, images, files, voice, video, text messages, etc)

C # development portal and application (18)-member management for enterprise address book management and development

C # development portal and application (17)-department management for enterprise address book management and development

C # development portal and application (16)-enterprise number configuration and use

C # development portal and application (15)-added the scan, image sending, and geographic location functions in the menu

C # development portal and application (14)-use redirection in the menu to obtain user data

C # development portal and application (13)-use geographic location Extension

C # development portal and application (12)-use voice processing

C # development portal and application (11)-menu presentation

C # development portal and application (10) -- synchronize user group information in the management system

C # development portal and application (9)-portal menu management and submission to server

C # development portal and application (8)-portal application management system function Introduction

C # development portal and application (7)-Multi-customer service functions and development integration

C # development portal and application (6)-portal menu management operations

C # development portal and application (5) -- User Group Information Management

C # development portal and application (4) -- Focus on the user list and detailed information management

C # development portal and application (3) -- Response to text messages and text messages

C # development portal and application (2) -- Message Processing and response

C # development portal and application (1) -- getting started with Interfaces

Related Article

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.