C # WeChat portal and application development (27)-Message Management of Public Account templates,

Source: Internet
Author: User
Tags openid tojson

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

Through the template message interface, the public account can send messages of the preset template to users who follow the user's account. Template messages are only used to send important service notifications to users by public accounts. They can only be used in service scenarios that meet the requirements, such as credit card swiping notifications and successful product purchase notifications. Marketing messages such as advertisements and all other messages that may cause harassment to users are not supported. This article describes how to manage messages in a public account template based on C # development.

"The launch of the template message function will greatly enhance the service notification capability of the service number." In the view of public account operators who have been expecting the template message function to be open, the social attributes of one-to-one communication are, it makes the push rate more accurate, which also gives enterprises many advantages in terms of cost, service efficiency, and performance. This not only enriches the enterprise's service forms, enhance user interaction and stickiness, and bring more diversified, rich, and timely service experience to users.

1. Industry Classification Management and description of templates

Template information is divided by industry, and different templates are designed based on different application scenarios, for example, in the software industry, templates may be used in various scenarios, such as registration success notifications, House view reminders, order reminders, member recharge notifications, and member consumption notifications.

If we need to use a template for the public account, we need to add the required template from the template library (currently the maximum number is 15 ). After the template is added to my template, each template generates a random value, that is, the [template ID]. We send the message according to the template ID.

Each template contains detailed parameter descriptions and sample results.

The team owner said: the opening of template messages mainly aims to help the public accounts complete closed-loop services. The existing public accounts have limited capability to send messages actively (four messages can be sent in a group each month ), this makes it impossible for many enterprises to push active notifications of service results and other messages to users. After the template message is opened, enterprises can use the platform to use the template message to make the information accessible more quickly during external services and internal management, and provide users with more thoughtful services.

Constantly optimizing user experience and opening template messages provide enterprises with more basic capabilities, such as richer two-way interaction and more precise information reminders, these have all improved the depth and breadth of Enterprise precision and personalized services. This is why organizations and enterprises in the financial, civil aviation, government, and other fields are looking forward to opening the template message function. In the future, with the further improvement of the template message function, you may need to change the employee's salary details, monthly household electricity usage, electricity usage, and other power usage details, or even the driver's license expiration, the template messages of public accounts of enterprises and departments can be instantly transmitted to corresponding users.

2. Use template messages for development

We have introduced the template-related information and the introduction of a single template. If we need to integrate the template message sending in the background program, what APIs do we need to know about the template message? How can I use the template message API to send messages?

Let's take a look at the template message usage instructions:

1. All service numbers can be viewed at function> Add function plug-in to apply for template message function, however, only the Authenticated Service number can apply for the permission to use the template message and obtain the permission. 2. Select the Public Account Service2Industries, which can be changed every month1Selected industry; 3. Select an existing template for calling in the template library of the selected industry; 4. Each account can use15Templates. 5. The Daily call limit for each template is0.1 million times[In November 18, 2014, the API call frequency was increased from the default daily 10 thousand to the daily 0.1 million, which can be viewed in the Developer Center after the MP logon ].

Template message management functions include:

1. Set the industry
2. Obtain the template ID.
3. Send template messages
4. Event push

2.1 set industry

The setting of the industry can be completed in MP, and the industry can be modified once a month. The account can only use the relevant templates in the industry to facilitate third-party developers, you can call interfaces to modify the industry of an account, as shown in the following code:

API call request description

Http Request Method: POSThttps: // api.weixin.qq.com/cgi-bin/template/api_set_industry? Access_token = ACCESS_TOKEN

POST Data Description

An example of POST data is as follows:

      {          "industry_id1":"1",          "industry_id2":"4"       }

According to the instructions, we can define an interface class ITemplateMessageApi, and then define the interface functions for setting the industry as follows:

/// <Summary> /// set the industry to which it belongs /// </summary> /// <param name = "accessToken"> </param> /// <param name = "industry_id1"> industry ID of the Public Account template message (main industry) </param> // <param name = "industry_id2"> industry ID of the Public Account template message (deputy battalion industry) </param> // <returns> </returns> CommonResult SetIndustry (string accessToken, IndustryCode industry_id1, IndustryCode industry_id2 );

For convenience, we define the IndustryCode as an enumeration object, which lists all industry code supported by the system, as shown below.

The implementation code is similar to the previous function processing. It only needs to POST data to a connection and parse the returned results. The specific implementation code is as follows.

/// <Summary> /// set the industry to which it belongs /// </summary> /// <param name = "accessToken"> Access Credential </param> /// <param name = "industry_id1"> industry ID of the Public Account template message (main industry) </param> // <param name = "industry_id2"> industry ID of the Public Account template message (deputy battalion industry) </param> // <returns> </returns> public CommonResult SetIndustry (string accessToken, IndustryCode industry_id1, IndustryCode industry_id2) {var url = string. format ("https://api.weixin.qq.com/cgi-bin/template/ Api_set_industry? Access_token = {0} ", accessToken); var data = new {industry_id1 = (int) industry_id1, industry_id2 = (int) industry_id2}; string postData = data. toJson (); return Helper. getExecuteResult (url, postData );}
2.2 obtain the template ID

Obtain the template ID, that is, add the corresponding template message from the template library to my template.

You can select a template from the industry Template Library to the account background to obtain the template ID in MP. To facilitate third-party developers, APIs are provided to modify the industry of the Account, as shown in the following code:

API call request description

Http Request Method: POSThttps: // api.weixin.qq.com/cgi-bin/template/api_add_template? Access_token = ACCESS_TOKEN

POST Data Description

An example of POST data is as follows:

      {           "template_id_short":"TM00015"       }

C # The function implementation code is as follows:

/// <Summary> /// obtain the template ID. /// select the template from the industry Template Library to the account background. The process of obtaining the template ID can be completed in MP. /// </Summary> /// <param name = "accessToken"> Access Credential </param> /// template number in the template library <param name = "template_id_short">, "TM **" and "OPENTMTM **" formats </param> // <returns> </returns> public AddTemplateResult AddTemplate (string accessToken, string template_id_short) {var url = string. format ("https://api.weixin.qq.com/cgi-bin/template/api_add_template? Access_token = {0} ", accessToken); var data = new {template_id_short = template_id_short}; string postData = data. toJson (); return JsonHelper <AddTemplateResult>. convertJson (url, postData );}
2.3 send template messages

According to the processing in the above section, the template ID obtained from the Operation added to my template can be called to send the template message by calling the API.

API call request description

Http Request Method: POSThttps: // api.weixin.qq.com/cgi-bin/message/template/send? Access_token = ACCESS_TOKEN

POST Data Description

An example of POST data is as follows:

{"Touser": "OPENID", "template_id": "ngqipbwh8bufcssecmogfxcv14j0tq?bo27izeyty", "url": "http://weixin.qq.com/download", "topcolor": "# FF0000", "data ": {"first": {"value": "Congratulations! "," Color ":" #173177 "}," keynote1 ": {" value ":" Chocolate "," color ":" #173177 "}," keynote2 ": {"value": "39.8 yuan", "color": "#173177"}, "keynote3": {"value": "September 16, 2014", "color ": "#173177"}, "remark": {"value": "Welcome to purchase again! "," Color ":" #173177 "}}}

Based on the preceding JSON parameters, we can see that some of them are the public part of the template message, and some are the specific parameters of the template message, these parameters must be assigned different values based on different templates.

For example, this part is common:

   touser":"OPENID",   "template_id":"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY",   "url":"http://weixin.qq.com/download",   "topcolor":"#FF0000",

According to this feature, we define the interface for sending template messages as follows:

/// <Summary> /// the template message is only used to send important service notifications to users by public accounts. It can only be used in service scenarios that meet the requirements, such as credit card swiping notifications, notification of successful product purchase. /// Marketing messages such as advertisements and all other messages that may cause harassment to users are not supported. /// </Summary> /// <param name = "accessToken"> Access Credential </param> /// <param name = "openId"> openID of the account </ param> /// <param name = "templateId"> in the online template library of the public platform, select the template to obtain the ID </param> // <param name = "data"> Template variable Parameter Data </param> /// <param name = "url">, if the URL is left blank, after sending the message, clicking the template message will enter a blank page (ios), or you cannot click (android ). </Param> /// <param name = "topcolor"> top color, the default value is #173177 </param> /// <returns> </returns> SendMassMessageResult SendTemplateMessage (string accessToken, string openId, string templateId, object data, string url, string topcolor = "#173177 ");

We useObjectData to define the parameters of the template.

The specific implementation is similar to the previous method for submitting data processing. The Code is as follows.

/// <Summary> /// the template message is only used to send important service notifications to users by public accounts. It can only be used in service scenarios that meet the requirements, such as credit card swiping notifications, notification of successful product purchase. /// Marketing messages such as advertisements and all other messages that may cause harassment to users are not supported. /// </Summary> /// <typeparam name = "T"> </typeparam> /// <param name = "accessToken"> Access Credential </param>/ // <param name = "openId"> </param> // <param name = "templateId"> select a template in the online template library of the public platform to obtain the ID. </param>/ // <param name = "data"> </param> /// <param name = "url">, if the URL is left blank, after sending the message, clicking the template message will enter a blank page (ios), or you cannot click (android ). </Param> /// <param name = "topcolor"> </param> /// <returns> </returns> public SendMassMessageResult SendTemplateMessage (string accessToken, string openId, string templateId, object data, string url, string topcolor = "#173177") {var postUrl = string. format ("https://api.weixin.qq.com/cgi-bin/message/template/send? Access_token = {0} ", accessToken); var msgData = new TemplateData () {touser = openId, template_id = templateId, topcolor = topcolor, url = url, data = data }; string postData = msgData. toJson (); SendMassMessageResult result = JsonHelper <SendMassMessageResult>. convertJson (postUrl, postData); return result ;}

The message sending template is more complex than the use of the other two interfaces. For example, the template message of one member notification is used as an example. The details of the template are as follows:

The specific test code is as follows.

# Region send template message var data = new {// use TemplateDataItem to create data. First = new TemplateDataItem ("Hello, you have become a member of [Guangzhou aiqidi. "), Type = new TemplateDataItem (" 18620292076 "), address = new TemplateDataItem (" Guangzhou Baiyun District Guangzhou Avenue North "), VIPName = new {// use the new method to build data, includes two fixed attributes: value and color. Value = "Wu huacong", color = "#173177"}, VIPPhone = new TemplateDataItem ("18620292076"), expDate = new TemplateDataItem ("April 18, 2016 "), remark = new TemplateDataItem ("if you have any questions, please contact 18620292076. "," #173177 "), };# endregion string url =" http://www.iqidi.com "; string topColor =" #173177 "; string templateId ="-success "; SendMassMessageResult sendResult = api. sendTemplateMessage (token, openId, templateId, data, url, topColor); if (sendResult! = Null) {Console. WriteLine (sendResult. msg_id );}

The result is as follows.

Template messages allow us to communicate with our customers regardless of the number of messages per month. At the same time, we can use the rich content of the template library to implement powerful application scenarios.

 

 

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 (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.