Home > WeChat Applet > WeChat Development > .net WeChat public account development

.net WeChat public account development

迷茫
Release: 2017-03-25 13:47:23
Original
1973 people have browsed it

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)
Copy after login

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

设置所属行业示例
Copy after login

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)
Copy after login

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());
    }
Copy after login

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)
Copy after login

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)
Copy after login

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;
            }
        }
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template