本文介紹微信公衆號中的模板消息,包括如下內容:(1)TemplateMessage類簡介;(2)設置所屬行業;(3)得到模板id;(4)發送模板消息;(5)接收推送模板消息發送結果事件。
本文演示地址:http://xrwang.net/Example/TemplateMessage.aspx
本文源代碼地址:
http://git.oschina.net/xrwang2/xrwang.weixin.PublicAccount/tree/master/PublicAccount/TemplateMessage
http://git.oschina.net/xrwang2/xrwang.weixin.PublicAccount/blob/master/xrwang.net/Example/TemplateMessage.aspx.csgit
1 TemplateMessage類簡介
TemplateMessage靜態類封裝了跟模板消息相關的方法,見下表:服務器
方法名 | 功能 |
SetIndustry | 設置行業 |
GetId | 獲取模板id |
Send | 發送模板消息 |
2 設置所屬行業微信
TemplateMessage類的SetIndustry方法用於設置公衆號所屬的行業,該方法的定義以下:網絡
/// <summary> /// 設置行業 /// </summary> /// <param name="userName">公衆號</param> /// <param name="code1">行業代碼1</param> /// <param name="code2">行業代碼2</param> /// <returns>返回設置是否成功</returns> public static ErrorMessage SetIndustry(string userName, string code1, string code2) //或者 /// <summary> /// 設置行業 /// </summary> /// <param name="userName">公衆號</param> /// <param name="industry1">行業1</param> /// <param name="industry2">行業2</param> /// <returns>返回設置是否成功</returns> public static ErrorMessage SetIndustry(string userName, Industry industry1, Industry industry2)
其中,Industry爲行業類,類中的靜態成員包含了已知的全部行業,例如:Industry.OnlineGame表明了網絡遊戲這一行業;Industry類有三個屬性,分別爲:Code——行業代碼,Name——行業名稱,PrimaryIndustry——主行業。ide
設置所屬行業的示例:this
/// <summary> /// 設置所屬行業 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnSetIndustry_Click(object sender, EventArgs e) { string userName = lbPublicAccount.SelectedValue; string industryCode1 = "", industryCode2 = ""; int count = 0; foreach (ListItem item in cblIndustry.Items) { if (item.Selected) { count++; if (count == 1) industryCode1 = item.Value; else if (count == 2) { industryCode2 = item.Value; break; } } } if (count != 2) ltrMessage.Text = "請選擇兩個行業。"; else { ErrorMessage errorMessage = TemplateMessage.SetIndustry(userName, industryCode1, industryCode2); ltrMessage.Text = string.Format("設置所屬行業{0}。{1}", errorMessage.IsSuccess ? "成功" : "失敗", errorMessage.IsSuccess ? "" : errorMessage.ToString()); } }
3 得到模板idspa
TemplateMessage類的GetId方法用於獲取模板id,該方法定義以下:.net
/// <summary> /// 獲取模板ID /// </summary> /// <param name="userName">公衆號</param> /// <param name="shortTemplateId">模板庫中模板的編號,有「TM**」和「OPENTMTM**」等形式</param> /// <param name="errorMessage">返回獲取是否成功</param> /// <returns>返回模板ID;若是獲取失敗,返回空字符串。</returns> public static string GetId(string userName, string shortTemplateId, out ErrorMessage errorMessage)
注意:(1)若是還沒有添加模板,該方法會先添加模板,而後返回模板id;(2)若是已經添加了模板,再次調用該方法,會返回一個新的不一樣於上次獲取到的模板id。code
得到模板id的示例:orm
/// <summary> /// 添加並模板id /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnGetTemplateId_Click(object sender, EventArgs e) { string userName = lbPublicAccount.SelectedValue; ErrorMessage errorMessage; string templateId = TemplateMessage.GetId(userName, txtTemplateIdShort.Text, out errorMessage); if (errorMessage.IsSuccess) ltrMessage.Text = string.Format("添加並獲取模板id成功。模板id:{0}", templateId); else ltrMessage.Text = string.Format("添加並獲取模板id失敗。{0}", errorMessage.ToString()); }
4 發送模板消息
TemplateMessage類的Send方法用於發送模板消息,該方法定義以下:
/// <summary> /// 發送模板消息 /// </summary> /// <param name="userName">公衆號</param> /// <param name="touser">接收消息的帳號</param> /// <param name="templateId">模板id</param> /// <param name="detailUrl">詳情地址</param> /// <param name="topColor">頂端顏色</param> /// <param name="data">數據</param> /// <param name="errorMessage">返回發送是否成功</param> /// <returns>返回消息id;若是發送失敗,返回-1。</returns> public static long Send(string userName, string touser, string templateId, string detailUrl, Color topColor, Tuple<string, string, Color>[] data, out ErrorMessage errorMessage)
其中,data參數爲Tuple類型,包含模板所用的數據,data.Item1爲數據鍵,data.Item2爲數據值,data.Item3爲顯示數據的顏色。
發送模板消息的示例:
/// <summary> /// 發送模板消息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnSend_Click(object sender, EventArgs e) { if (rblUser.SelectedIndex >= 0) { string userName = lbPublicAccount.SelectedValue; string openId = rblUser.SelectedValue; string templateId = "z8zHvTm2gpU0gZUBwA0dXibMO_VYy6iwJYgtW6qeyPg"; string title = txtTitle.Text; string name = txtUserName.Text; string time = DateTime.Now.ToString(); Tuple<string, string, Color>[] data = new Tuple<string, string, Color>[]{ new Tuple<string,string,Color>("title",title,Color.Blue), new Tuple<string,string,Color>("username",name,Color.Green), new Tuple<string,string,Color>("time",time,Color.Red) }; ErrorMessage errorMessage; long msgId = TemplateMessage.Send(userName, rblUser.SelectedValue, templateId, "", Color.Black, data, out errorMessage); if (errorMessage.IsSuccess) ltrMessage.Text = string.Format("發送模板消息成功。消息id:{0}", msgId); else ltrMessage.Text = string.Format("發送模板消息失敗。{0}", errorMessage); } }
5 接收推送模板消息發送結果事件
在發送模板消息以後,微信服務器會推送結果到公衆號的指定URL上,公衆號服務器會接收到一條RequestTemplateSendJobFinishMessage類型的請求消息。
RequestTemplateSendJobFinishMessage類有如下只讀屬性:
/// <summary> /// 獲取消息id /// </summary> public long MsgID { get; private set; } /// <summary> /// 獲取羣發消息的結果 /// </summary> public string Status { get; private set; } /// <summary> /// 獲取消息是否羣發成功 /// </summary> public TemplateMessageSendStatusEnum SendStatus { get { TemplateMessageSendStatusEnum status; if (Status == sendFailedUserBlock) status = TemplateMessageSendStatusEnum.UserBlock; else if (Status == sendFailedSystemFailed) status = TemplateMessageSendStatusEnum.SystemFailed; else status = TemplateMessageSendStatusEnum.Success; return status; } }