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

.net WeChat public account development

迷茫
迷茫Original
2017-03-25 13:47:231919browse

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:


        /// 631fb227578dfffda61e1fa4d04b7d25
        /// 发送模板消息        /// 039f3e95db2a684c7b74365531eb6044
        /// 20e42ef3279b479672c1207a13c1abab公众号8bb7487ae6a16a43571bc14c7fcf93c2
        /// b34a78e356dd5e42f9d330d79f145693接收消息的账号8bb7487ae6a16a43571bc14c7fcf93c2
        /// f0116b851bf4413f5bf75b2a2aeb1058模板id8bb7487ae6a16a43571bc14c7fcf93c2
        /// cbabe74617639c1c27560c98af959168详情地址8bb7487ae6a16a43571bc14c7fcf93c2
        /// 657cc48a9fcdf82a27a82fe3fa9f4e77顶端颜色8bb7487ae6a16a43571bc14c7fcf93c2
        /// c8a98340aded2d5f70b63a18be4bcac7数据8bb7487ae6a16a43571bc14c7fcf93c2
        /// 2e8f4f2a373a35ec512cab8bc7c1425a返回发送是否成功8bb7487ae6a16a43571bc14c7fcf93c2
        /// 2363942ed0d6cd3e85bae1dffa568116返回消息id;如果发送失败,返回-1。f7735d9f6a7af371769ab5c16d23b2f3
        public static long Send(string userName, string touser, string templateId, string detailUrl, Color topColor,
            Tuple72ca0ee74ca7edd147f17f5da5225be9[] 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!

Statement:
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