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 .net to develop template messages in Wechat official account

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how to use .net to develop template messages in Wechat official account. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Template messages in the official account of Wechat

1 introduction to the TemplateMessage class

The TemplateMessage static class encapsulates the methods associated with template messages, as shown in the following table:

Method name function SetIndustry set industry GetId get template idSend send template message

2 set the industry to which it belongs

The SetIndustry method of the TemplateMessage class is used to set the industry to which the official account belongs. This method is defined as follows:

/ set industry / official account / industry code 1 / industry code 2 / / returns whether the setting is successful public static ErrorMessage SetIndustry (string userName, string code1) String code2) / or / set industry / official account / Industry 1 / Industry 2 / return whether the setting is successful public static ErrorMessage SetIndustry (string userName, Industry industry1, Industry industry2)

Among them, Industry is an industry class, and the static members of the class contain all known industries, for example: Industry.OnlineGame represents the online game industry; Industry class has three attributes, namely: Code-- industry code, Name-- industry name, PrimaryIndustry-- main industry.

Example of setting up the industry you belong to:

/ protected void btnSetIndustry_Click (object sender, EventArgs e) {string userName = lbPublicAccount.SelectedValue; string industryCode1 = "", industryCode2 = ""; int count = 0; foreach (ListItem item in cblIndustry.Items) {if (item.Selected) {count++ If (count = = 1) industryCode1 = item.Value; else if (count = = 2) {industryCode2 = item.Value; break } if (count! = 2) ltrMessage.Text = "Please select two industries." ; else {ErrorMessage errorMessage = TemplateMessage.SetIndustry (userName, industryCode1, industryCode2); ltrMessage.Text = string.Format ("set industry {0}. {1}", errorMessage.IsSuccess? "success": "failure", errorMessage.IsSuccess? ": errorMessage.ToString ();}} example of setting the industry to which you belong

Set example of industry to which you belong

3 get template id

The GetId method of the TemplateMessage class is used to get the template id, which is defined as follows:

/ get the number of the template in the template ID / official account / in the form of "TM**" and "OPENTMTM**" / return whether the acquisition is successful / return the template ID; if the acquisition fails, an empty string will be returned. Public static string GetId (string userName, string shortTemplateId, out ErrorMessage errorMessage)

Note: (1) if the template has not been added, the method will first add the template, and then return the template id; (2) if you have already added the template, call the method again, it will return a new id which is different from the template obtained last time.

An example of getting a template id:

/ / add and template id / protected void btnGetTemplateId_Click (object sender, EventArgs e) {string userName = lbPublicAccount.SelectedValue; ErrorMessage errorMessage; string templateId = TemplateMessage.GetId (userName, txtTemplateIdShort.Text, out errorMessage); if (errorMessage.IsSuccess) ltrMessage.Text = string.Format ("add and get template id successfully. Template id: {0} ", templateId); else ltrMessage.Text = string.Format (" failed to add and get template id. {0} ", errorMessage.ToString ();}

Get template id example

4 send template message

The Send method of the TemplateMessage class is used to send template messages, which are defined as follows:

/ send template message / official account / / the account that receives the message / template id / details address / Top Color / data / return whether the message is sent successfully / return message id If the send fails, return-1. Public static long Send (string userName, string touser, string templateId, string detailUrl, Color topColor, Tuple [] data, out ErrorMessage errorMessage)

Where the data parameter is of type Tuple and contains the data used by the template, data.Item1 is the data key, data.Item2 is the data value, and data.Item3 is the color of the display data.

Example of sending a template message:

/ send template message / official account / / the account that receives the message / template id / details address / Top Color / data / return whether the message is sent successfully / return message id If the send fails, return-1. Public static long Send (string userName, string touser, string templateId, string detailUrl, Color topColor, Tuple [] data, out ErrorMessage errorMessage)

Send template message example

5 receive push template message send result event

After sending the template message, the Wechat server will push the result to the specified URL of the official account, and the official account server will receive a request message of type RequestTemplateSendJobFinishMessage.

The RequestTemplateSendJobFinishMessage class has the following read-only properties:

/ get the message id / public long MsgID {get; private set;} / get the result of the group message / public string Status {get; private set } / get whether the message was sent in groups successfully / public TemplateMessageSendStatusEnum SendStatus {get {TemplateMessageSendStatusEnum status; if (Status = = sendFailedUserBlock) status = TemplateMessageSendStatusEnum.UserBlock Else if (Status = = sendFailedSystemFailed) status = TemplateMessageSendStatusEnum.SystemFailed; else status = TemplateMessageSendStatusEnum.Success; return status }} this is the end of the article on "how to use .net to develop template messages in Wechat official accounts". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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