.net微信公衆號開發——羣發消息

   本文將介紹微信公衆號開發中用於羣發消息的類MassMessage,包括:(1)MassMessage類;(2)羣發;(3)刪除;(4)預覽;(5)查詢發送狀態;(6)接收推送羣髮結果事件。
    源代碼地址:http://git.oschina.net/xrwang2/xrwang.weixin.PublicAccount/tree/master/PublicAccount/MassMessage
    演示地址:http://www.xrwang.net/Example/MassMessage.aspxgit

1 MassMessage類
    MassMessage靜態類封裝了羣發消息相關的方法,以下表:服務器

做用 方法名
羣發 Send
刪除 Delete
預覽 Preview
查詢發送狀態 GetStatus

 

2 羣發
    公衆號能夠按用戶分組羣發消息,也能夠按用戶的OpenId列表來羣發羣發消息。
2.1 按用戶分組羣發消息
    方法定義以下:微信

複製代碼
/// <summary> /// 根據分組羣發消息 /// </summary> /// <param name="userName">公衆號</param> /// <param name="isToAll">是否羣發給全部用戶</param> /// <param name="groupId">若是羣發給全部用戶,忽略該參數;不然羣發給該組中的用戶</param> /// <param name="messageType">羣發消息類型</param> /// <param name="mediaIdOrContent">多媒體id或者文本內容</param> /// <param name="errorMessage">返回發送是否成功</param> /// <returns>若是發送成功,返回消息ID;不然,返回-1。</returns> public static long Send(string userName, bool isToAll, string groupId, MassMessageTypeEnum messageType, string mediaIdOrContent, out ErrorMessage errorMessage)
複製代碼

    使用示例以下:ide

    /// <summary> /// 按分組羣發消息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnSendToGroup_Click(object sender, EventArgs e) { if (rblGroup.SelectedIndex >= 0) { string userName = lbPublicAccount.SelectedValue; ErrorMessage errorMessage; bool isToAll = string.IsNullOrWhiteSpace(rblGroup.SelectedValue); string groupId = isToAll ? "" : rblGroup.SelectedValue; string content = txtContent.Text; long msgId = MassMessage.Send(userName, isToAll, groupId, MassMessageTypeEnum.text, content, out errorMessage); if (errorMessage.IsSuccess) { ltrMessage.Text = "羣發消息成功。"; rblMassMessage.Items.Add(new ListItem(string.Format("id:{0},text:{1}", msgId, content), msgId.ToString())); } else ltrMessage.Text = string.Format("羣發消息失敗。{0}", errorMessage); } }
按用戶分組羣發消息示例

 

2.2 按OpenId列表羣發this

    方法定義以下:spa

複製代碼
/// <summary> /// 根據OpenId列表羣發消息 /// </summary> /// <param name="userName">公衆號</param> /// <param name="tousers">OpenId列表</param> /// <param name="messageType">羣發消息類型</param> /// <param name="mediaIdOrContent">多媒體id或者文本內容</param> /// <param name="errorMessage">返回發送是否成功</param> /// <returns>若是發送成功,返回消息ID;不然,返回-1。</returns> public static long Send(string userName, IEnumerable<string> tousers, MassMessageTypeEnum messageType, string mediaIdOrContent, out ErrorMessage errorMessage)
複製代碼

    使用示例以下:.net

    /// <summary> /// 按用戶羣發消息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnSendToUsers_Click(object sender, EventArgs e) { if (cblUser.SelectedIndex >= 0) { string userName = lbPublicAccount.SelectedValue; ErrorMessage errorMessage; List<string> openIds = new List<string>(); foreach (ListItem item in cblUser.Items) { if (item.Selected) openIds.Add(item.Value); } string content = txtContent.Text; long msgId = MassMessage.Send(userName, openIds, MassMessageTypeEnum.text, content, out errorMessage); if (errorMessage.IsSuccess) { ltrMessage.Text = "羣發消息成功。"; rblMassMessage.Items.Add(new ListItem(string.Format("id:{0},text:{1}", msgId, content), msgId.ToString())); } else ltrMessage.Text = string.Format("羣發消息失敗。{0}", errorMessage); } }
按用戶OpenId列表羣發消息示例

 

3 刪除羣發
    刪除已羣發消息的方法定義以下:3d

複製代碼
/// <summary> /// 刪除羣發消息。 /// 注:只能刪除圖文消息和視頻消息。 /// </summary> /// <param name="userName">公衆號</param> /// <param name="messageId">消息id</param> /// <returns>返回刪除是否成功</returns> public static ErrorMessage Delete(string userName, long messageId)
複製代碼

    使用示例以下:code

    /// <summary> /// 刪除羣發消息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnDelete_Click(object sender, EventArgs e) { if (rblMassMessage.SelectedIndex >= 0) { string userName = lbPublicAccount.SelectedValue; long msgId = long.Parse(rblMassMessage.SelectedValue); ErrorMessage errorMessage = MassMessage.Delete(userName, msgId); if (errorMessage.IsSuccess) { ltrMessage.Text = "刪除消息成功。"; rblMassMessage.Items.Remove(rblMassMessage.SelectedItem); } else ltrMessage.Text = string.Format("刪除消息失敗。{0}", errorMessage); } }
刪除羣發的示例

 

4 預覽羣發
    預覽羣發消息的方法定義以下:orm

複製代碼
/// <summary> /// 預覽羣發消息 /// </summary> /// <param name="userName">公衆號</param> /// <param name="touser">OpenId</param> /// <param name="messageType">羣發消息類型</param> /// <param name="mediaIdOrContent">多媒體id或者文本內容</param> /// <param name="errorMessage">返回發送是否成功</param> /// <returns>若是發送成功,返回消息ID;不然,返回-1。</returns> public static long Preview(string userName, string touser, MassMessageTypeEnum messageType, string mediaIdOrContent, out ErrorMessage errorMessage)
複製代碼

    使用示例以下:

    /// <summary> /// 預覽羣發消息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnPreview_Click(object sender, EventArgs e) { if (cblUser.SelectedIndex >= 0) { string userName = lbPublicAccount.SelectedValue; ErrorMessage errorMessage; string openId = cblUser.SelectedValue; string content = txtContent.Text; long msgId = MassMessage.Preview(userName, openId, MassMessageTypeEnum.text, content, out errorMessage); if (errorMessage.IsSuccess) { ltrMessage.Text = "預覽消息成功。"; rblMassMessage.Items.Add(new ListItem(string.Format("id:{0},text:{1}", msgId, content), msgId.ToString())); } else ltrMessage.Text = string.Format("預覽消息失敗。{0}", errorMessage); } }
預覽羣發的示例

 

5 查詢羣發消息發送狀態
    查詢羣發消息發送狀態的方法定義以下:

複製代碼
/// <summary> /// 查詢羣發消息的發送狀態 /// </summary> /// <param name="userName">公衆號</param> /// <param name="messageId">消息id</param> /// <param name="errorMessage">返回查詢是否成功</param> /// <returns>返回消息是否發送成功</returns> public static bool GetStatus(string userName, long messageId, out ErrorMessage errorMessage)
複製代碼

    使用示例以下:

    /// <summary> /// 查詢羣發消息狀態 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnGetStatus_Click(object sender, EventArgs e) { if (rblMassMessage.SelectedIndex >= 0) { string userName = lbPublicAccount.SelectedValue; ErrorMessage errorMessage; long msgId = long.Parse(rblMassMessage.SelectedValue); bool success = MassMessage.GetStatus(userName, msgId, out errorMessage); if (errorMessage.IsSuccess) ltrMessage.Text = string.Format("消息羣發{0}。", success ? "成功" : "失敗"); else ltrMessage.Text = string.Format("獲取消息羣髮狀態失敗。{0}", errorMessage); } }
查詢羣發發送狀態的示例

 

6 接收推送羣髮結果事件
    羣發消息以後,微信服務器會推送羣發的結果到公衆號的指定URL上,公衆號服務器會接收到一條RequestMassSendJobFinishMessage類型的請求消息。
    RequestMassSendJobFinishMessage類有如下只讀屬性:

複製代碼
        /// <summary> /// 獲取消息id /// </summary> public long MsgID { get; private set; } /// <summary> /// 獲取羣發消息的結果 /// </summary> public string Status { get; private set; } /// <summary> /// 獲取用戶總數 /// </summary> public int TotalCount { get; private set; } /// <summary> /// 獲取過濾後待發送的用戶數 /// </summary> public int FilterCount { get; private set; } /// <summary> /// 獲取發送成功的用戶數 /// </summary> public int SentCount { get; private set; } /// <summary> /// 獲取發送失敗的用戶數 /// </summary> public int ErrorCount { get; private set; } /// <summary> /// 獲取消息是否羣發成功 /// </summary> public bool SendSuccess { get { return Status == sendSuccess; } } /// <summary> /// 獲取發送失敗的緣由 /// </summary> public string ErrorReason { get { string reason = string.Empty; if (Status == sendSuccess) reason = "發送成功"; else if (Status == sendFailed) reason = "發送失敗"; else if (errorDict.ContainsKey(Status)) reason = errorDict[Status]; return reason; } }
複製代碼

 

感謝您看完本文,但願對您有所幫助。

相關文章
相關標籤/搜索