微信公衆平臺開發 --發送模版消息html
發送模版消息是微信服務號給某個用戶發送模版消息,相似於APP的推送通知;api
一、添加模版消息微信
在頁面的左上 有一個添加功能插件的 按鈕,如題cookie
添加完成後,咱們就能夠在左邊的菜單欄看到 相應的信息了;app
二、添加模版消息微信公衆平臺
詳情裏面有關模版的介紹,和發送短信須要傳送的數據;ide
三、發送模版消息post
接口調用請求說明
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" } } }
在調用模板消息接口後,會返回JSON數據包。正常時的返回JSON數據包示例:url
{ "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 消息能夠發送了;spa