.net微信公众号开发——模板消息

超越梦想 超越梦想     2022-08-03     555

关键词:

    本文介绍微信公众号中的模板消息,包括以下内容:(1)TemplateMessage类简介;(2)设置所属行业;(3)获得模板id;(4)发送模板消息;(5)接收推送模板消息发送结果事件。
    本文演示地址:http://xrwang.net/Example/TemplateMessage.aspx
    本文源代码地址:
    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类简介
    TemplateMessage静态类封装了跟模板消息相关的方法,见下表:

方法名 功能
SetIndustry 设置行业
GetId 获取模板id
Send 发送模板消息

2 设置所属行业

    TemplateMessage类的SetIndustry方法用于设置公众号所属的行业,该方法的定义如下:

复制代码
        /// <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)
复制代码

    其中,Industry为行业类,类中的静态成员包含了已知的所有行业,例如:Industry.OnlineGame代表了网络游戏这一行业;Industry类有三个属性,分别为:Code——行业代码,Name——行业名称,PrimaryIndustry——主行业。

    设置所属行业的示例:

    /// <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());
        }
    }
设置所属行业示例

 

3 获得模板id

    TemplateMessage类的GetId方法用于获取模板id,该方法定义如下:

复制代码
        /// <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)
复制代码

    注意:(1)如果尚未添加模板,该方法会先添加模板,然后返回模板id;(2)如果已经添加了模板,再次调用该方法,会返回一个新的不同于上次获取到的模板id。

    获得模板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());
    }
获得模板id示例

 

4 发送模板消息
    TemplateMessage类的Send方法用于发送模板消息,该方法定义如下:

复制代码
        /// <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)
复制代码

    其中,data参数为Tuple类型,包含模板所用的数据,data.Item1为数据键,data.Item2为数据值,data.Item3为显示数据的颜色。

    发送模板消息的示例:

    /// <summary>
    /// 发送模板消息
    /// </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 = txtUserName.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, templateId, "", Color.Black, data, out errorMessage);
            if (errorMessage.IsSuccess)
                ltrMessage.Text = string.Format("发送模板消息成功。消息id:{0}", msgId);
            else
                ltrMessage.Text = string.Format("发送模板消息失败。{0}", errorMessage);
        }
    }
发送模板消息示例

 

5 接收推送模板消息发送结果事件
    在发送模板消息之后,微信服务器会推送结果到公众号的指定URL上,公众号服务器会接收到一条RequestTemplateSendJobFinishMessage类型的请求消息。
    RequestTemplateSendJobFinishMessage类有以下只读属性:

复制代码
        /// <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;
            }
        }

.net微信公众号开发——群发消息

  本文将介绍微信公众号开发中用于群发消息的类MassMessage,包括:(1)MassMessage类;(2)群发;(3)删除;(4)预览;(5)查询发送状态;(6)接收推送群发结果事件。  源代码地址:http://git.oschina.net/xrwang2/x... 查看详情

php之微信公众号发送模板消息

讲一下开发项目中微信公众号发送模板消息的实现过程(我用的还是Thinkphp5.0)。先看一下效果,如图:就是类似于这样的,下面讲一下实现过程:第一步:微信公众号申请模板消息权限:立即申请:申请过程就不说了,提交并且... 查看详情

微信小程序|微信公众平台springboot开发实例│模板消息的应用开发

 在手机微信公众号中输入文本(如“你好”),公众号发送两条模板消息,如下图所示。1、说明1●模板消息的基本规则模板消息用来帮助公众号进行业务通知,是在模板内容中设定参数(参数必须以开头࿰... 查看详情

微信公众号发送模板消息(代码片段)

目录1开发中遇到的问题汇总2模板消息创建3调试接口3.1微信公众号消息模板3.1.1基本信息3.2请求参数3.2.1Query参数及说明3.2.2body参数及说明4测试结果5工具类封装1开发中遇到的问题汇总首先是在测试中,遇到最多的就是41003->appid... 查看详情

java微信公众号通过openid发送模板消息~

1,问题产生 在微信公众号开发过程中,我们有时候做不同权限的时候,比如在注册的时候,需要审核,然后我们要想办法让对方知道审核的结果。这时候我们可以通过模板消息来通知。2,第一步,首先在微信公众号上获取... 查看详情

简单使用java实现微信公众号推送模板消息

原文链接: https://blog.csdn.net/qq_41936224/article/details/108076005以下例子是简单的使用Java代码实现微信公众号推送模板消息,不包含跳转到小程序网页代码1、pom.xml文件中添加依赖<!--lombok--><depend 查看详情

java对接微信公众号模板消息推送

内容有点多,请耐心!最近公司的有这个业务需求,又很凑巧让我来完成:首先想要对接,先要一个公众号,再就是开发文档了:https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Overview.html 不过请注意这一点 ok,我们继... 查看详情

asp群发微信公众号模板消息代码

刚刚做的一个项目需要用到asp群发微信公众号的模板消息,以前没做过,想了一会用了下面这种方法实现的模板消息群发推送,效果很好。asp群发微信公众号模板消息代码 查看详情

php实现发送模板消息到微信公众号

简述:在这里会具体讲述到如何实现:如何通过后台的代码来实现发送模板消息到已经关注了“心想”公众号的用户。  (本人新手,目前实习中,我的所有文档都是在自己开发过程中的记录,有些言语跟我的项目有关请大家... 查看详情

微信模板消息群发系统

 一、使用场景:微信公众号模板消息群发,用于提醒、通知、营销等。只要关注的用户都可以收到二、使用条件:1、认证服务号2、已经申请开通模板消息功能(认证服务号即可申请)三、功能介绍:微信公众号模板消息一... 查看详情

如何使用微信公众平台小程序

参考技术A微信公众平台在官方微信公号推送了微信公众平台开发指南。指南很详细地介绍了微信公众平台自定义菜单、消息管理、微信网页开发以及微信小店、微信客服、摇一摇周播等15项功能的开发指南。公众号主要通过公众... 查看详情

微信公众号开发16一些实战问题

1,发生text消息时的换行问题\n换行。推送的内容要用双引号参考博客微信公众号消息text换行问题-liuzp111的专栏-CSDN博客https://blog.csdn.net/everything1209/article/details/48294179 查看详情

主动发送微信公众平台“模板消息”需要啥条件?

...消息。主动发送消息目前可以通过两种方式实现:1、通过微信的,高级群发接口和客服接口(须认证后才能够使用)进行消息的群发规则服务器每个月4条订阅号每天一条客服接口可以实现向单独用户发送消息但是必须该用户24小时... 查看详情

java对接微信公众号模板消息推送(代码片段)

最近公司的有这个业务需求,又很凑巧让我来完成:首先想要对接,先要一个公众号,再就是开发文档了:https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Overview.html 不过请注意这一点ok,我们继续:... 查看详情

微信模板消息开发

 1.添加模板1)登录公众号2)添加插件登录之后,在左边栏的功能里面点击【添加功能插件】—>【模板消息】进入后,然后开始审核,大概要1-3天时间。下面为我们正在审核的界面对此,我们等待模板审核成功 2.设置... 查看详情

微信公众号模板消息推送

...通access_token的区别获取普通access_token的接口模板id需要在微信官方申请自己企业的推送模板,成功会获得一个模板id1、模板消息实体类2、access_token实体类1、获取access_token方法2、消息推送方法 查看详情

微信公众平台开发,模板消息,网页授权,微信js-sdk,二维码生成

微信公众平台开发,模板消息,什么是模板消息,模板消息接口指的是向用户发送重要的服务通知,只能用于符合场景的要求中去,如信用卡刷卡通知,购物成功通知等等。不支持广告营销,打扰用户的消息,模板消息类有固定... 查看详情

微信公众号开发系列-发送客服消息

下面是做微信公众号开发用到最多的两个客服消息发送类型,文本信息和图文信息。1、发送文本消息{"touser":"OPENID","msgtype":"text","text":{"content":"HelloWorld"}}參数是否必须说明access_token是调用接口凭证touser是普通用户openidmsgtype是消... 查看详情