. Net public account development-template message,. net public template message
Author: Wang Xianrong
This article describes the template messages in the public account, including: (1) TemplateMessage class introduction; (2) setting the industry; (3) Obtaining the template id; (4) Sending template messages; (5) receive the message sending result event of the push template.
This article demo address: http://xrwang.net/Example/TemplateMessage.aspx
Source Code address:
Http://git.oschina.net/xrwang2/xrwang.weixin.PublicAccount/tree/master/PublicAccount/TemplateMessage
Http://git.oschina.net/xrwang2/xrwang.weixin.PublicAccount/blob/master/xrwang.net/Example/TemplateMessage.aspx.cs
1 TemplateMessage class Introduction
The TemplateMessage static class encapsulates methods related to template messages, as shown in the following table:
| Method Name |
Function |
| SetIndustry |
Set Industry |
| GetId |
Get template id |
| Send |
Send template message |
2. Set the industry
The SetIndustry method of the TemplateMessage class is used to set the industry to which the public account belongs. The method is defined as follows:
/// <Summary> /// set the industry // </summary> /// <param name = "userName"> Public Account </param> /// <param name = "code1"> Industry Code 1 </param> /// <param name = "code2"> Industry Code 2 </param> /// <returns> returns whether to set successful </returns> public static ErrorMessage SetIndustry (string userName, string code1, string code2) /// or /// <summary> /// set the industry // </summary> /// <param name = "userName"> Public Account </param> // /<param name = "industry1"> Industry 1 </param> // <param name = "industry2"> Industry 2 </param> // <returns> return settings successful </returns> public static ErrorMessage SetIndustry (string userName, industry industry1, Industry industry2)
Specifically, the Industry class is the Industry class, and the static members in the class contain all known industries, such as: Industry. onlineGame represents the online game Industry. The Industry class has three attributes: Code-Industry Code, Name-Industry Name, And PrimaryIndustry-main Industry.
Example of setting the industry:
/// <Summary> /// set the industry to which it belongs /// </summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> 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 = "select two industries. "; Else {ErrorMessage errorMessage = TemplateMessage. SetIndustry (userName, industryCode1, industryCode2); ltrMessage. Text = string. Format (" set industry {0 }. {1} ", errorMessage. IsSuccess? "Successful": "failed", errorMessage. IsSuccess? "": ErrorMessage. ToString ());}}
3. Obtain the template id.
The GetId method of the TemplateMessage class is used to obtain the template id. The method is defined as follows:
/// <Summary> /// obtain the template ID /// </summary> /// <param name = "userName"> Public Account </param> /// <param name = "Your templateid"> ID of the template in the template library, there are "TM **" and "OPENTMTM **" formats </param> // <param name = "errorMessage"> whether the query is successful </param> /// <returns> return the template ID; if the query fails, an empty string is returned. </Returns> public static string GetId (string userName, string optional templateid, out ErrorMessage errorMessage)
Note: (1) if no template is added, the method first adds the template and then returns the template id; (2) if the template has been added, call this method again, A new template id is returned, which is different from the template id obtained last time.
Example of getting template id:
/// <Summary> /// add and template id /// </summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> 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. form At ("template id added and obtained successfully. Template id: {0} ", templateId); else ltrMessage. Text = string. Format (" failed to add and retrieve template id. {0} ", errorMessage. ToString ());}
4. Send template messages
The Send method of the TemplateMessage class is used to Send template messages. The method is defined as follows:
/// <Summary> /// send template message /// </summary> /// <param name = "userName"> Public Account </param> /// <param name = "touser"> account for receiving messages </param> /// <param name = "templateId"> template id </param> /// <param name =" detailUrl "> details address </param> /// <param name =" topColor "> top color </param> /// <param name =" data "> data </ param> /// <param name = "errorMessage"> returns whether the message is successfully sent </param> /// <returns> returns the Message id; if sending fails,-1 is returned. </Returns> public static long Send (string userName, string touser, string templateId, string detailUrl, Color topColor, Tuple <string, string, Color> [] data, out ErrorMessage errorMessage)
The data parameter is of the Tuple type 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 displayed data.
Example of sending a template message:
/// <Summary> /// send template message /// </summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> protected void btnSend_Click (object sender, eventArgs e) {if (rblUser. selectedIndex> = 0) {string userName = lbPublicAccount. selectedValue; string openId = rblUser. selectedValue; string templateId = "z8zHvTm2gpU0gZUBwA0dXibMO_VYy6iwJYgtW6qeyPg"; string title = txtTitle. text; string name = txtUse RName. text; string time = DateTime. now. toString (); Tuple <string, string, Color> [] data = new Tuple <string, string, Color> [] {new Tuple <string, string, color> ("title", title, Color. blue), new Tuple <string, string, Color> ("username", name, Color. green), new Tuple <string, string, Color> ("time", time, Color. red)}; ErrorMessage errorMessage; long msgId = TemplateMessage. send (userName, rblUser. selectedValue, templ AteId, "", Color. Black, data, out errorMessage); if (errorMessage. IsSuccess) ltrMessage. Text = string. Format ("template message sent successfully. Message id: {0} ", msgId); else ltrMessage. Text = string. Format (" failed to send template message. {0} ", errorMessage );}}
5. Receive push template message sending result events
After a template message is sent, the server pushes the result to the specified URL of the public account. The public account server receives a request message of the RequestTemplateSendJobFinishMessage type.
The RequestTemplateSendJobFinishMessage class has the following read-only attributes:
/// <Summary> /// obtain the Message id /// </summary> public long MsgID {get; private set ;} /// <summary> /// obtain the group message sending result /// </summary> public string Status {get; private set ;} /// <summary> /// obtain whether the message is sent in a group successfully. /// </summary> 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 ;}}
Thank you for reading this article and hope to help you.