码迷,mamicode.com
首页 > 微信 > 详细

C# 微信公众平台开发(4)-- 模版消息

时间:2015-11-23 14:41:09      阅读:397      评论:0      收藏:0      [点我收藏+]

标签:

微信公众平台开发 --发送模版消息

发送模版消息是微信服务号给某个用户发送模版消息,类似于APP的推送通知;

1、添加模版消息

   在页面的左上 有一个添加功能插件的 按钮,如题

    技术分享

 

 

       添加完成后,我们就可以在左边的菜单栏看到 相应的信息了;

2、添加模版消息

  技术分享  

   详情里面有关模版的介绍,和发送短信需要传送的数据;

3、发送模版消息

  技术分享

  

接口调用请求说明
http请求方式: POST https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN
会根据ACCESS_TOKEN来请求数据
请求数据格式:
  
技术分享
 {
           "touser":"OPENID",
           "template_id":"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY",
           "url":"http://weixin.qq.com/download",            
           "data":{
                   "first": {
                       "value":"恭喜你购买成功!",
                       "color":"#173177"
                   },
                   "keynote1":{
                       "value":"巧克力",
                       "color":"#173177"
                   },
                   "keynote2": {
                       "value":"39.8元",
                       "color":"#173177"
                   },
                   "keynote3": {
                       "value":"2014年9月22日",
                       "color":"#173177"
                   },
                   "remark":{
                       "value":"欢迎再次购买!",
                       "color":"#173177"
                   }
           }
       }
View Code

在调用模板消息接口后,会返回JSON数据包。正常时的返回JSON数据包示例:

    {
           "errcode":0,
           "errmsg":"ok",
           "msgid":200228332
    }

技术分享

官网上的例子;
技术分享个人
/// <summary>
        /// 发送模版消息,
        /// </summary>
        /// <param name="openId">关注微信号《FromUserName》</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SendMsg(string openId)
        {
            ///获取token
            string accesstoken = AccessToken.GetAccessToken(AppID, AppSecret);

            ///发送模版消息
            string url = string.Format("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={0}", accesstoken);
            string postData = "{ \"touser\":\"" + openId + "\",\"template_id\":\"kJxuPYYN5z915G0qtxec5fkohEVcXgw0aLfQ1PUoJhU\",\"url\":\"http://www.woizuqiu.com/Home/Index\", \"data\":{\"first\":{\"value\":\"亲,宝贝已经启程了,好想快点来到你身边!\",\"color\":\"#173177\"},\"keynote1\":{\"value\":\"201408189987\",\"color\":\"#173177\"},\"keynote2\":{\"value\":\"圆通快递\",\"color\":\"#173177\"},\"keynote3\":{\"value\":\"6522238281\",\"color\":\"#173177\"},\"remark\":{\"value\":\"点击查看完整的物流信息 。如有问题请致电XXX或直接在微信里留言,XXX将在第一时间为您服务!!\",\"color\":\"#173177\"}} }";

            string strPostData = "{ \"touser\":\"" + openId + "\",\"template_id\":\"LNinaAoWW12OtI41wXo2MWfC4uey2CfYjaQSIdDAm0g\",\"url\":\"http://www.woizuqiu.com/Home/Index\", \"data\":{\"channel\":{\"value\":\"微信\",\"color\":\"#173177\"},\"orderNumber\":{\"value\":\"113234\",\"color\":\"#173177\"},\"state\":{\"value\":\"进入收单状态\",\"color\":\"#173177\"},\"doSomething\":{\"value\":\"kantzou将在今天下午三点上门收件\",\"color\":\"#173177\"},\"remark\":{\"value\":\"谢谢您的支持!\",\"color\":\"#173177\"}}}";

            string strJson = HttpPostData(url, strPostData);

            return View();
        }

        /// <summary>
        /// Post请求
        /// </summary>
        /// <param name="posturl">URL</param>
        /// <param name="postData">请求数据</param>
        /// <returns></returns>
        public string HttpPostData(string posturl, string postData)
        {
            Stream outstream = null;
            Stream instream = null;
            StreamReader sr = null;
            HttpWebResponse response = null;
            HttpWebRequest request = null;
            Encoding encoding = Encoding.UTF8;
            byte[] data = encoding.GetBytes(postData);
            // 准备请求...
            try
            {
                // 设置参数
                request = WebRequest.Create(posturl) as HttpWebRequest;
                CookieContainer cookieContainer = new CookieContainer();
                request.CookieContainer = cookieContainer;
                request.AllowAutoRedirect = true;
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = data.Length;
                outstream = request.GetRequestStream();
                outstream.Write(data, 0, data.Length);
                outstream.Close();
                //发送请求并获取相应回应数据
                response = request.GetResponse() as HttpWebResponse;
                //直到request.GetResponse()程序才开始向目标网页发送Post请求
                instream = response.GetResponseStream();
                sr = new StreamReader(instream, encoding);
                //返回结果网页(html)代码
                string content = sr.ReadToEnd();
                string err = string.Empty;
                Response.Write(content);
                return content;
            }
            catch (Exception ex)
            {
                string err = ex.Message;
                return string.Empty;
            }
        }

OK 消息可以发送了;

 

C# 微信公众平台开发(4)-- 模版消息

标签:

原文地址:http://www.cnblogs.com/amoshu/p/4988365.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!