.net WeChat public account development
Author: Wang Xianrong
This article introduces the template message in the WeChat public account, including the following content: (1) Introduction to the TemplateMessage class; (2) Setting the industry; (3) Obtaining the template id; (4) Send template messages; (5) Receive push template message sending result events.
1 Introduction to TemplateMessage class
TemplateMessage static class encapsulates methods related to template messages, see the table below:
Method name | Function |
SetIndustry | Set industry |
GetId | Get template id |
Send | Send template message |
2 Settings Industry
The SetIndustry method of the TemplateMessage class is used to set the industry to which the official account belongs. The method is defined as follows:
/// <summary> /// 设置行业 /// </summary> /// <param name="userName">公众号</param> /// <param name="code1">行业代码1</param> /// <param name="code2">行业代码2</param> /// <returns>返回设置是否成功</returns> public static ErrorMessage SetIndustry(string userName, string code1, string code2) //或者 /// <summary> /// 设置行业 /// </summary> /// <param name="userName">公众号</param> /// <param name="industry1">行业1</param> /// <param name="industry2">行业2</param> /// <returns>返回设置是否成功</returns> public static ErrorMessage SetIndustry(string userName, Industry industry1, Industry industry2)
Among them, Industry is the industry class, and the static members in the class include all known industries. For example: Industry.OnlineGame represents the online game industry; the Industry class has three attributes, namely : Code——Industry code, Name——Industry name, PrimaryIndustry——Main industry.
Example of setting the industry:
/// <summary> /// 设置所属行业 /// </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 = "请选择两个行业。"; else { ErrorMessage errorMessage = TemplateMessage.SetIndustry(userName, industryCode1, industryCode2); ltrMessage.Text = string.Format("设置所属行业{0}。{1}", errorMessage.IsSuccess ? "成功" : "失败", errorMessage.IsSuccess ? "" : errorMessage.ToString()); } } 设置所属行业示例
Example of setting the industry
##3 Get template id
The GetId method of the TemplateMessage class is used to obtain the template id. The method is defined as follows:/// <summary> /// 获取模板ID /// </summary> /// <param name="userName">公众号</param> /// <param name="shortTemplateId">模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式</param> /// <param name="errorMessage">返回获取是否成功</param> /// <returns>返回模板ID;如果获取失败,返回空字符串。</returns> public static string GetId(string userName, string shortTemplateId, out ErrorMessage errorMessage)
Note: (1) If the template has not been added yet, this method will first Add a template, and then return the template id; (2) If a template has been added, calling this method again will return a new template id different from the last obtained template id.
Example of getting template id:/// <summary> /// 添加并模板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.Format("添加并获取模板id成功。模板id:{0}", templateId); else ltrMessage.Text = string.Format("添加并获取模板id失败。{0}", errorMessage.ToString()); }
Example of getting template id
4 Send template Message The Send method of the TemplateMessage class is used to send template messages. The method is defined as follows:
/// <summary> /// 发送模板消息 /// </summary> /// <param name="userName">公众号</param> /// <param name="touser">接收消息的账号</param> /// <param name="templateId">模板id</param> /// <param name="detailUrl">详情地址</param> /// <param name="topColor">顶端颜色</param> /// <param name="data">数据</param> /// <param name="errorMessage">返回发送是否成功</param> /// <returns>返回消息id;如果发送失败,返回-1。</returns> public static long Send(string userName, string touser, string templateId, string detailUrl, Color topColor, Tuple<string, string, Color>[] data, out ErrorMessage errorMessage)
Among them, the data parameter is of Tuple type, 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 template message:/// <summary> /// 发送模板消息 /// </summary> /// <param name="userName">公众号</param> /// <param name="touser">接收消息的账号</param> /// <param name="templateId">模板id</param> /// <param name="detailUrl">详情地址</param> /// <param name="topColor">顶端颜色</param> /// <param name="data">数据</param> /// <param name="errorMessage">返回发送是否成功</param> /// <returns>返回消息id;如果发送失败,返回-1。</returns> public static long Send(string userName, string touser, string templateId, string detailUrl, Color topColor, Tuple<string, string, Color>[] data, out ErrorMessage errorMessage)
Example of sending template message
5 Receive push template message sending 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:
/// <summary> /// 获取消息id /// </summary> public long MsgID { get; private set; } /// <summary> /// 获取群发消息的结果 /// </summary> public string Status { get; private set; } /// <summary> /// 获取消息是否群发成功 /// </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; } }
The above is the detailed content of .net WeChat public account development. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

H5 is more flexible and customizable, but requires skilled technology; mini programs are quick to get started and easy to maintain, but are limited by the WeChat framework.

Compatibility issues and troubleshooting methods for company security software and application. Many companies will install security software in order to ensure intranet security. However, security software sometimes...

Discussion on the JS resource caching issue of Enterprise WeChat. When upgrading project functions, some users often encounter situations where they fail to successfully upgrade, especially in the enterprise...

The choice of H5 and applet depends on the requirements. For applications with cross-platform, rapid development and high scalability, choose H5; for applications with native experience, rich functions and platform dependencies, choose applets.

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

There are differences in the promotion methods of H5 and mini programs: platform dependence: H5 depends on the browser, and mini programs rely on specific platforms (such as WeChat). User experience: The H5 experience is poor, and the mini program provides a smooth experience similar to native applications. Communication method: H5 is spread through links, and mini programs are shared or searched through the platform. H5 promotion methods: social sharing, email marketing, QR code, SEO, paid advertising. Mini program promotion methods: platform promotion, social sharing, offline promotion, ASO, cooperation with other platforms.

H5 development tools recommendations: VSCode, WebStorm, Atom, Brackets, Sublime Text; Mini Program Development Tools: WeChat Developer Tools, Alipay Mini Program Developer Tools, Baidu Smart Mini Program IDE, Toutiao Mini Program Developer Tools, Taro.
