标签:
在微信的服务号中,往往需要对订阅用户推送消息,有时候会用到模板消息,下面的代码仅供参考,个人测试成功。
@access_token 请调用 https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=@appid&secret=@secret 接口获取。
1 public static void Send() 2 { 3 dynamic postData = new ExpandoObject(); 4 postData.touser = "OpenId"; 5 postData.template_id = "template_id"; 6 postData.url = string.Empty; 7 postData.topcolor = "#FF0000"; 8 postData.data = new ExpandoObject(); 9 var data = new[] 10 { 11 new Tuple<string, string, string>("title", "航班延误通知", "#FF0000"), 12 new Tuple<string, string, string>("trainNumber", "10分钟", "#FF0000"), 13 new Tuple<string, string, string>("fromto", "上海-北京", "#FF0000"), 14 new Tuple<string, string, string>("formerTime", "2015/7/31 14:36:32", "#FF0000"), 15 new Tuple<string, string, string>("Time", "2015/07/31 14:36:32", "#FF0000"), 16 new Tuple<string, string, string>("number", "10分钟", "#FF0000"), 17 new Tuple<string, string, string>("reason", "天气原因", "#FF0000"), 18 new Tuple<string, string, string>("remark", "请关注我们的微信通知", "#FF0000") 19 }; 20 var dataDict = (IDictionary<string, object>)postData.data; 21 foreach (var item in data) 22 { 23 dataDict.Add(item.Item1, new { value = item.Item2, color = item.Item3 }); 24 } 25 string json = ((object)postData).Serialize(); 26 27 Console.WriteLine(json); 28 var r = NetUtils.CreateHttpResponse(@"https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=@access_token", json); 29 Console.WriteLine(r); 30 }
标签:
原文地址:http://www.cnblogs.com/x1988z/p/4693509.html