作者:王先榮
本文介紹微信公眾號中的模板消息,包括以下內容:(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.cs
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——主行業。
設置所屬行業的示例:

/// <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 獲得模板id
TemplateMessage類的GetId方法用於獲取模板id,該方法定義如下:
/// <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。
獲得模板id的示例:

/// <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; } }
感謝您看完本文,希望對您有所幫助。