. Net WeChat official account development

Source: Internet
Author: User
This article describes how to use. net to develop public accounts and how to operate them. 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.

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:


////// Set the industry //////Public 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 the industry //////Public Account///Industry 1///Industry 2///
 
  
Returns whether the setting is successful.
 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:

////// Set the industry /////////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 () ;}} sets the industry example

Set industry example

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:

////// Obtain the template ID //////Public Account///Template number in the template library, in the form of "TM **" and "OPENTMTM **"///Returns whether the query is successful.///
 
  
Returns the template ID. if the template fails to be obtained, an empty string is returned.
 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:

////// 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 ("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 ());}

Sample template id

4. send template messages
The Send method of the TemplateMessage class is used to Send template messages. the method is defined as follows:


////// Send template message //////Public Account///Account for receiving messages///Template id///Detailed address///Top Color///Data///Returns whether the message is sent successfully.///
 
  
The message id is returned. if the message fails to be sent,-1 is returned.
 Public static long Send (string userName, string touser, string templateId, string detailUrl, Color topColor, Tuple
 
  
[] 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:

////// Send template message //////Public Account///Account for receiving messages///Template id///Detailed address///Top Color///Data///Returns whether the message is sent successfully.///
 
  
The message id is returned. if the message fails to be sent,-1 is returned.
 Public static long Send (string userName, string touser, string templateId, string detailUrl, Color topColor, Tuple
 
  
[] Data, out ErrorMessage errorMessage)
 

Sample template message sending

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:

////// Obtain the message id ///Public long MsgID {get; private set ;}////// Obtain the group message sending result ///Public string Status {get; private set ;}////// Obtain whether the message is sent in a group 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 ;}}

The above is the details of. net public account development. For more information, see other related articles on php Chinese network!

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.