1. 程式人生 > >微信小程式傳送模版訊息

微信小程式傳送模版訊息

使用說明

步驟一:獲取模板ID

有兩個方法可以獲取模版ID

  1. 通過模版訊息管理介面獲取模版ID(詳見模版訊息管理
  2. 在微信公眾平臺手動配置獲取模版ID

步驟二:頁面的 <form/> 元件,屬性report-submittrue時,可以宣告為需發模板訊息,此時點選按鈕提交表單可以獲取formId,用於傳送模板訊息。或者當用戶完成支付行為,可以獲取prepay_id用於傳送模板訊息。

步驟三:呼叫介面下發模板訊息(詳見傳送模版訊息

傳送模版訊息

介面地址

https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=ACCESS_TOKEN

HTTP請求方式:

POST

POST引數說明:

引數必填說明
touser接收者(使用者)的 openid
template_id所需下發的模板訊息的id
page點選模板卡片後的跳轉頁面,僅限本小程式內的頁面。支援帶引數,(示例index?foo=bar)。該欄位不填則模板無跳轉。
form_id表單提交場景下,為 submit 事件帶上的 formId;支付場景下,為本次支付的 prepay_id
data模板內容,不填則下發空模板
color模板內容字型的顏色,不填預設黑色 【廢棄】
emphasis_keyword模板需要放大的關鍵詞,不填則預設無放大
示例:
{
  "touser": "OPENID"
, "template_id": "TEMPLATE_ID", "page": "index", "form_id": "FORMID", "data": { "keyword1": { "value": "339208499" }, "keyword2": { "value": "2015年01月05日 12:30" }, "keyword3": { "value": "粵海喜來登酒店" } , "keyword4": { "value"
: "廣州市天河區天河路208號" } }, "emphasis_keyword": "keyword1.DATA" }

返回碼說明:

在呼叫模板訊息介面後,會返回JSON資料包。

正常時的返回JSON資料包示例:

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

錯誤時會返回錯誤碼資訊,說明如下:

返回碼說明
40037template_id不正確
41028form_id不正確,或者過期
41029form_id已被使用
41030page不正確
45009介面呼叫超過限額(目前預設每個帳號日呼叫限額為100萬)

本次開發中用到的是支付後傳送模版訊息,所有傳遞的form_id是統一下單返回的prepay_id;

ASP.NET WEB API傳送程式碼實現如下(程式碼未做封裝):

                            //從配置檔案讀取微信公眾號appid與secret
                            string public_appid = ConfigurationManager.AppSettings["yh_client_Appid"];
                            string pulic_secret = ConfigurationManager.AppSettings["yh_client_Secret"];
                            //根據appid,secret 獲取access_token
                            var url = @"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + public_appid + "&secret=" + pulic_secret;
                            Dictionary<string, object> result = Get_Wx_DictionaryHelper.GetDictionary(url);
                            string access_token = "";
                            if (result.ContainsKey("access_token"))
                            {
                                access_token = result["access_token"].ToString();
                            }
                            Logger.Debug("Wx_Pay_Notify--access_token:" + DateTime.Now.ToString() + access_token);
                            var sendUrl = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=" + access_token + "";
                            Wx_PaY_NotifyModel sendModel = Wx_yyhz_ym_activityBLL.Get_sigup_pay_send_detail(out_trade_no.ToString(), transaction_id);
                            CYQ.Data.Cache.CacheManage cacheManage = CYQ.Data.Cache.CacheManage.Instance;
                            var form_id = cacheManage.Get(out_trade_no).ToString();//從快取中取到通一下單介面返回的prepay_id
                            Logger.Debug("當前FORMID:" + form_id + "=====");
                            Dictionary<string, object> sendDictionary = new Dictionary<string, object>();
                            sendDictionary.Add("touser", xml["openid"]);
                            sendDictionary.Add("template_id", "Ap------------0gSSan15TqW4yLxsZ28");//從微信公眾平臺賦值的模版ID
                            sendDictionary.Add("page", "pages/main/main");
                            sendDictionary.Add("form_id", form_id);
                            Dictionary<string, object> senddata = new Dictionary<string, object>();
                            senddata.Add("keyword1", new Dictionary<string, object>()
                            {
                                {"value",sendModel.vip_name}
                            });
                            senddata.Add("keyword2", new Dictionary<string, object>()
                            {
                                {"value",sendModel.total_fee+"元"}
                            });
                            senddata.Add("keyword3", new Dictionary<string, object>()
                            {
                                {"value",sendModel.activity_title}
                            });
                            senddata.Add("keyword4", new Dictionary<string, object>()
                            {
                                {"value",sendModel.activity_starttime}
                            });
                            senddata.Add("keyword5", new Dictionary<string, object>()
                            {
                                {"value",sendModel.activity_adr}
                            });
                            sendDictionary.Add("data", senddata);
                            Logger.Debug("傳送模版資料:" + sendDictionary.ToJson() + "=======");
                            HttpClient httpClient = new HttpClient();
                            httpClient.DefaultRequestHeaders.Accept.Add(
                            new MediaTypeWithQualityHeaderValue("application/json"));
                            HttpContent httpContent = new StringContent(sendDictionary.ToJson());
                            HttpResponseMessage ResponseMsg = httpClient.PostAsync(sendUrl, httpContent).Result;
                            var response = ResponseMsg.Content.ReadAsStringAsync().Result;
                            Logger.Debug("Wx_Pay_Notify--response:" + DateTime.Now.ToString() + response);
                            cacheManage.Remove(out_trade_no);


本次開發中用到的是支付後傳送模版訊息,所有傳遞的form_id是統一下單返回的prepay_id;