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

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

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

1、添加模版消息

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

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

 

 

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

2、添加模版消息

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

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

3、发送模版消息

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

  

接口调用请求说明
http请求方式: POST https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN
会根据ACCESS_TOKEN来请求数据
请求数据格式:
  
C# 微信公众平台开发(4)-- 模版消息
 {
           "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
    }

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

官网上的例子;
C# 微信公众平台开发(4)-- 模版消息个人
/// <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)-- 模版消息

上一篇:UnitedStack有云:我乘OpenStack起航 驶向企业级开源IT的远洋!


下一篇:关于南通大学教务管理系统微信公众号的个人看法