.net微信公众号开发——模板消息
作者:王先荣
    本文介绍微信公众号中的模板消息,包括以下内容:(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 = ;
foreach (ListItem item in cblIndustry.Items)
{
if (item.Selected)
{
count++;
if (count == )
industryCode1 = item.Value;
else if (count == )
{
industryCode2 = item.Value;
break;
}
}
}
if (count != )
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 >= )
{
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微信公众号开发——模板消息的更多相关文章
- php之微信公众号发送模板消息
		讲一下开发项目中微信公众号发送模板消息的实现过程(我用的还是Thinkphp5.0). 先看一下效果,如图: 就是类似于这样的,下面讲一下实现过程: 第一步:微信公众号申请模板消息权限: 立即申请: ... 
- .net微信公众号开发——群发消息
		作者:王先荣 本文将介绍微信公众号开发中用于群发消息的类MassMessage,包括:(1)MassMessage类:(2)群发:(3)删除:(4)预览:(5)查询发送状态:(6)接收推送群发结 ... 
- php之微信公众号发送模板消息参观模仿
		上篇文章中鞋到了公众号发送末班消息,他是最后调用两个方法,本文章简化一下 将下面的php方法放到一个可以引入的公共类中即可 构建模板消息: 我把需要用到的模板消息 都写成一个个方法,放在公共类文件中了 ... 
- .net微信公众号开发——消息与事件
		作者:王先荣 本文介绍如何处理微信公众号开发中的消息与事件,包括:(1)消息(事件)概况:(2)验证消息的真实性:(3)解析消息:(4)被动回复消息:(5)发送其他消息. 开源项目地址:h ... 
- C#微信公众号开发系列教程五(接收事件推送与消息排重)
		微信公众号开发系列教程一(调试环境部署) 微信公众号开发系列教程一(调试环境部署续:vs远程调试) C#微信公众号开发系列教程二(新手接入指南) C#微信公众号开发系列教程三(消息体签名及加解密) C ... 
- PHP 微信公众号开发 - 消息推送
		项目微信公众号开发,需要做用户消息推送,记录下来以便日后使用 1,接上一篇文章,可以查看如何获取用户openid PHP 微信公众号开发 - 获取用户信息 2,添加模板消息 3,查看模板详情 根据模板 ... 
- 转:C#微信公众号开发之接收事件推送与消息排重的方法
		本文实例讲述了C#微信公众号开发之接收事件推送与消息排重的方法.分享给大家供大家参考.具体分析如下: 微信服务器在5秒内收不到响应会断掉连接,并且重新发起请求,总共重试三次.这样的话,问题就来了.有这 ... 
- C#微信公众号开发系列教程三(消息体签名及加解密)
		http://www.cnblogs.com/zskbll/p/4139039.html C#微信公众号开发系列教程一(调试环境部署) C#微信公众号开发系列教程一(调试环境部署续:vs远程调试) C ... 
- C#微信公众号开发系列教程四(接收普通消息)
		微信公众号开发系列教程一(调试环境部署) 微信公众号开发系列教程一(调试环境部署续:vs远程调试) C#微信公众号开发系列教程二(新手接入指南) C#微信公众号开发系列教程三(消息体签名及加解密) C ... 
随机推荐
- 对IEnumerable<T>和IQueryable<T>的一点见解
			今天学习了用EF模型做查询,感觉数据库上下文对象的扩展方法很强大,所以研究了一下where的实现原理,其中遇到了一个问题,就是关于IEnumerable和IQueryable的区别,所以查了查资料,这 ... 
- css3 border-image 学习随笔
			先上w3school数据: 对于分开设置如上表所示,没有疑惑.但是当缩写时: border-image:url(/i/border.png) 30 30 round; 一参:图片地址: 二参.三参:只 ... 
- [Leetcode][JAVA] Palindrome Partitioning II
			Given a string s, partition s such that every substring of the partition is a palindrome. Return the ... 
- URAL - 1917 Titan Ruins: Deadly Accuracy(水题)
			水题一个,代码挫了一下: 题意不好理解. 你去一个洞窟内探险,洞窟内有许多宝石,但都有魔法守护,你需要用魔法将它们打下来. 每个宝石都有自己的防御等级,当你的魔法超过它的防御等级时它就会被你打下来. ... 
- mongoDB研究笔记:复制集数据同步机制
			http://www.cnblogs.com/guoyuanwei/p/3279572.html 概述了复制集,整体上对复制集有了个概念,但是复制集最重要的功能之一数据同步是如何实现的?带着这个问题 ... 
- 设计模式->观察者模式
			观察者模式能非常大的减少模块之前的耦合.具体的观察者模式,客官们可以去看<设计模式>或者<Head first设计模式>等之类的书. 在java中,java.util库中封装了 ... 
- 使用SVG生成的奔跑吧兄弟的动画效果
			在线演示 本地下载 缩放一下在线演示效果窗口,看看不同大小下的动画是不是都显示的非常完美? 体验一下SVG的强大之处吧! 
- atitit.泛型编程总结最佳实践 vO99 java c++ c#.net php
			atitit.泛型编程总结最佳实践 vO99 java c++ c#.net php \ 1. 泛型历史 1 由来 1 2. 泛型的机制编辑 1 机制 1 编译机制 2 3. 泛型方法定义1::前定义 ... 
- piap.excel 微软 时间戳转换mssql sql server文件时间戳转换unix 导入mysql
			piap.excel 微软 时间戳转换mssql sql server文件时间戳转换unix 导入mysql 需要不个mssql的sql文件导入mysql.他们的时间戳格式不同..ms用的是自定义的时 ... 
- iOS开发——高级技术&蓝牙服务
			蓝牙服务 蓝牙 随着蓝牙低功耗技术BLE(Bluetooth Low Energy)的发展,蓝牙技术正在一步步成熟,如今的大部分移动设备都配备有蓝牙4.0,相比之前的蓝牙技术耗电量大大降低.从iOS的 ... 
