Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use C # to develop official account template message Management

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use C# to develop official account template message management, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let's take a look at it.

Through the template message interface, the official account can send messages of preset templates to users who follow their accounts. Template messages are only used for official accounts to send important service notifications to users, and can only be used in service scenarios that meet their requirements, such as credit card payment notification, product purchase success notification, and so on. Marketing messages such as advertisements and all other messages that may cause harassment to users are not supported. This paper mainly introduces the development and implementation of official account template message management function based on C #.

"the introduction of the template message function will greatly enhance the service notification ability of the service number." in the view of some official account operators who have been looking forward to the opening of Wechat template message function, Wechat has the social attribute of one-to-one communication. It makes the access rate of information push more accurate, which also gives enterprises many advantages in cost, service efficiency and performance, which not only enriches the service form of enterprises, but also enhances the interaction and stickiness of users. It can also bring users more yuan, rich and timely service experience.

1. Industry classification management and explanation of templates.

Template information is divided by industry, and different templates are designed according to different usage scenarios. For example, in the software industry, there are templates that may be used in various scenarios, such as successful registration notification, house viewing reminder, order reminder, member recharge, member consumption notification, and so on.

If our official account needs to use templates, then we need to add the required templates from the template library (the current limit is 15). After the template is added to my template, each template generates a random value, that is, [template ID]. We send the message according to this template ID.

There is a detailed description of the parameters in each template, as well as the example effect.

A person in charge of the Wechat team said: the opening of template messages is mainly to help official accounts complete closed-loop services, and the existing official accounts have limited ability to send messages on their own initiative (four messages can be sent in groups per month). This makes it impossible for many enterprises to actively notify users of messages such as service results. After the opening of template messages, enterprises can make use of Wechat platform and use template messages to make the access to information faster and provide users with more thoughtful services in the process of external services and internal management.

Wechat has been constantly optimizing the user experience, and the opening of template messages has provided enterprises with more basic capabilities, such as richer two-way interaction, more accurate information reminders, etc., all these have enhanced the depth and breadth of enterprise quality and personalized services, which is why institutions and enterprises in many fields, such as finance, civil aviation, government affairs, and so on, look forward to Wechat's open template message function. In the future, with the further improvement of the template message function, perhaps the details of employees' salary, household monthly electricity consumption, electricity bill, or even the driver's license needs to be renewed when it expires, can be immediately delivered to the corresponding users through the template message of the Wechat public account of enterprises and departments.

2. Use template messages for development

The previous introduction of template information and the introduction of a single template, if we need to integrate template message sending in the daemon, then we need to know what are the API of template messages? How to send a message using the API of a template message?

Let's take a look at the instructions for the use of template messages:

1. All service numbers can see the entry of the application template message function at function-> add function plug-ins, but only the authenticated service number can apply for and obtain the permission to use the template message. 2. You need to select the two industries in which the public account service is located, and you can change the selected industry once a month. 3. Select the existing template in the template library of the selected industry. 4. Each account can use 15 templates at the same time. 5. The maximum number of daily calls per template is 100000. On November 18, 2014, the daily call frequency of the API is increased from the default 10,000 to 100000, which can be viewed in the developer Center after logging in to MP.

The management functions of template messages are:

1 set up the industry to which you belong

2 get the template ID

3 send template messages

4 event push

2.1 set the industry to which it belongs

Setting the industry can be done in MP, and the industry can be modified once a month. The account can only use the relevant templates in the industry. For the convenience of third-party developers, you can use API calls to modify the industry to which the account belongs, as shown below:

API call request description

Http request method: POST https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=ACCESS_TOKEN

POST data description

Examples of POST data are as follows:

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

According to the instructions, we can define an interface class ITemplateMessageApi, and then define the interface function of the industry to which the setting belongs as follows:

/ set the industry code of the official account template message (main industry) / the industry code of the official account template message (deputy industry) / CommonResult SetIndustry (string accessToken, IndustryCode industry_id1, IndustryCode industry_id2)

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

The implementation code is similar to the previous function processing, only POST data to a connection, and parse the returned results on it, the specific implementation code is shown below.

/ set the industry / access credentials / official account template message industry code (main industry) / official account template message industry code (secondary industry) / 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 get template ID

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

From the industry template library to the account backend, the process of obtaining the template ID can be completed in MP. For the convenience of third-party developers, you can modify the industry to which the account belongs through API calls, as shown below:

API call request description

Http request method: POST https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=ACCESS_TOKEN

POST data description

Examples of POST data are as follows:

{"template_id_short": "TM00015"}

The implementation code of the C # function is as follows:

/ get the template ID. / Select the template from the industry template library to the account backend, and the process of obtaining the template ID can be completed in MP. / access credential / / template number in template library, in the form of "TM**" and "OPENTMTM**" / 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.ConvertJson (url, postData);} 2.3 send template message

According to the template ID obtained by the operation added to my template, we can call the API that sent the template message to send the template message.

API call request description

Http request method: POST https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN

POST data description

Examples of POST data are as follows:

{"touser": "OPENID", "template_id": "ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY", "url": "http://weixin.qq.com/download"," topcolor ":" # FF0000 "," data ": {" first ": {" value ":" Congratulations on your successful purchase! " , "color": "# 173177"}, "keynote1": {"value": "Chocolate", "color": "# 173177"} "keynote2": {"value": "39.8RMB", "color": "# 173177"}, "keynote3": {"value": "September 16th, 2014" "color": "# 173177"}, "remark": {"value": "Welcome to buy again!" , "color": "# 173177"}

According to the above JSON parameters, we can see that some are the common parts of the template message, and some are the specific parameters of the template message. These parameters need to be assigned differently according to different templates.

If this part is common:

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

Based on this feature, we define the interface for sending template messages as follows:

/ template messages are only used for official accounts to send important service notifications to users, and can only be used in service scenarios that meet their requirements, such as credit card payment notification, product purchase success notification, and so on. / / Marketing messages such as advertisements and all other messages that may cause harassment to users are not supported. / access credential / / account openID / Select a template in the online template library of the public platform to obtain ID / / template change parameter data /. If URL is left empty, clicking on the template message will take you to a blank page (ios) or cannot be clicked (android). / / Top color, default is # 173177 / SendMassMessageResult SendTemplateMessage (string accessToken, string openId, string templateId, object data, string url, string topcolor = "# 173177")

We use object data to define the change parameter data of the template.

The specific implementation is similar to the previous method to submit data processing, the code is shown below.

/ template messages are only used for official accounts to send important service notifications to users, and can only be used in service scenarios that meet their requirements, such as credit card payment notification, product purchase success notification, and so on. / / Marketing messages such as advertisements and all other messages that may cause harassment to users are not supported. / / access credential / in the online template library of the public platform, select the template to obtain ID /. If URL is left empty, clicking on the template message will take you to a blank page (ios) or cannot be clicked (android). / 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.ConvertJson (postUrl, postData); return result }

Sending a template message is more complicated than the use of the other two APIs. For example, I take a template message of a member notification as an example. The details of the template are as follows:

The specific test code is shown below.

# region sends template messages var data = new {/ / use TemplateDataItem to simply create data. First = new TemplateDataItem ("Hello, you have become a member of Wechat [Guangzhou Aiqidi].") , type = new TemplateDataItem ("18620292076"), address = new TemplateDataItem ("Guangzhou Avenue North, Baiyun District, Guangzhou City"), VIPName = new {/ / use new to construct data, including two fixed attributes value and color. Value = "Wu Hua Cong", color = "# 173177"}, VIPPhone = new TemplateDataItem ("18620292076"), expDate = new TemplateDataItem ("April 18, 2016"), remark = new TemplateDataItem ("if in doubt, please contact 18620292076." , "# 173177"),}; # endregion string url = "http://www.iqidi.com"; string topColor =" # 173177 "; string templateId ="-5LbClAa9KUlEmr5bCSS0rxU_I2iT16iYBDxCVU1iJg "; SendMassMessageResult sendResult = api.SendTemplateMessage (token, openId, templateId, data, url, topColor) If (sendResult! = null) {Console.WriteLine (sendResult.msg_id);}

Wechat template messages enable us to communicate with customers without the limit of a few messages per month. At the same time, we can also make use of the rich content of Wechat template library to achieve powerful application scenarios.

Thank you for reading this article carefully. I hope the article "how to use C# to develop official account template message management" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report