升級內容:新增根據標籤群發介面,重構原根據分組群發介面
參考微信文件:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1481187827_i0l21
說明:
之前 SDK 只提供了根據使用者組群發的方法:
1 GroupMessageApi.SendGroupMessageByGroupId();
由於根據分組和標籤群發兩種情況,邏輯基本一致,只有一個filter引數不同,因此我們建立了 BaseGroupMessageDataByFilter 基類,用於支援 GroupMessageByGroupId 和 GroupMessageByTagId。
BaseGroupMessageByFilter 程式碼如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Senparc.Weixin.MP.AdvancedAPIs.GroupMessage 8 { 9 /// <summary> 10 /// 根據篩選條件(GroupId、TagId)群發訊息資料的基類 11 /// </summary> 12 public abstract class BaseGroupMessageByFilter 13 { 14 public bool is_to_all { get; set; } 15 } 16 17 18 public class BaseGroupMessageDataByFilter 19 { 20 public BaseGroupMessageByFilter filter { get; set; } 21 22 public string msgtype { get; set; } 23 24 /// <summary> 25 /// 群發介面新增 send_ignore_reprint 引數,開發者可以對群發介面的 send_ignore_reprint 引數進行設定,指定待群發的文章被判定為轉載時,是否繼續群發。 26 /// 當 send_ignore_reprint 引數設定為1時,文章被判定為轉載時,且原創文允許轉載時,將繼續進行群發操作。 27 /// 當 send_ignore_reprint 引數設定為0時,文章被判定為轉載時,將停止群發操作。 28 /// send_ignore_reprint 預設為0。 29 /// </summary> 30 public int send_ignore_reprint { get; set; } 31 } 32 33 34 public class GroupMessageByFilter_MediaId 35 { 36 public string media_id { get; set; } 37 } 38 39 public class GroupMessageByFilter_Content 40 { 41 public string content { get; set; } 42 } 43 44 public class GroupMessageByFilter_WxCard 45 { 46 public string card_id { get; set; } 47 } 48 49 public class GroupMessageByFilter_VoiceData : BaseGroupMessageDataByFilter 50 { 51 public GroupMessageByGroupId_MediaId voice { get; set; } 52 } 53 54 public class GroupMessageByFilter_ImageData : BaseGroupMessageDataByFilter 55 { 56 public GroupMessageByGroupId_MediaId image { get; set; } 57 } 58 59 public class GroupMessageByFilter_TextData : BaseGroupMessageDataByFilter 60 { 61 public GroupMessageByGroupId_Content text { get; set; } 62 } 63 64 public class GroupMessageByFilter_MpNewsData : BaseGroupMessageDataByFilter 65 { 66 public GroupMessageByGroupId_MediaId mpnews { get; set; } 67 } 68 69 public class GroupMessageByFilter_MpVideoData : BaseGroupMessageDataByFilter 70 { 71 public GroupMessageByGroupId_MediaId mpvideo { get; set; } 72 } 73 74 public class GroupMessageByFilter_WxCardData : BaseGroupMessageDataByFilter 75 { 76 public GroupMessageByGroupId_WxCard wxcard { get; set; } 77 } 78 }
GroupMessageByTagId 程式碼如下:
namespace Senparc.Weixin.MP.AdvancedAPIs.GroupMessage { /// <summary> /// 根據 TagId 群發篩選 /// </summary> public class GroupMessageByTagId : BaseGroupMessageByFilter { public string tag_id { get; set; } } }
GroupMessageByGroupId 程式碼如下(包含已經棄用的程式碼),為了保障向下相容性,標記了[Obsolete]特性:
1 namespace Senparc.Weixin.MP.AdvancedAPIs.GroupMessage 2 { 3 /// <summary> 4 /// 根據GroupId群發篩選 5 /// </summary> 6 public class GroupMessageByGroupId : BaseGroupMessageByFilter 7 { 8 public string group_id { get; set; } 9 } 10 11 #region 已廢棄 12 13 [Obsolete("請使用GroupMessageByGroupId")] 14 public class GroupMessageByGroupId_GroupId : BaseGroupMessageByFilter 15 { 16 public string group_id { get; set; } 17 } 18 19 /// <summary> 20 /// 根據GroupId群發訊息篩選 21 /// </summary> 22 [Obsolete("請使用BaseGroupMessageDataByFilter")] 23 public class BaseGroupMessageDataByGroupId : BaseGroupMessageDataByFilter 24 { 25 26 } 27 28 [Obsolete("請使用GroupMessageByFilter_MediaId")] 29 public class GroupMessageByGroupId_MediaId 30 { 31 public string media_id { get; set; } 32 } 33 34 [Obsolete("請使用GroupMessageByFilter_MediaId")] 35 public class GroupMessageByGroupId_Content 36 { 37 public string content { get; set; } 38 } 39 40 [Obsolete("請使用GroupMessageByFilter_MediaId")] 41 public class GroupMessageByGroupId_WxCard 42 { 43 public string card_id { get; set; } 44 } 45 46 [Obsolete("請使用GroupMessageByFilter_MediaId")] 47 public class GroupMessageByGroupId_VoiceData : BaseGroupMessageDataByGroupId 48 { 49 public GroupMessageByGroupId_MediaId voice { get; set; } 50 } 51 52 [Obsolete("請使用GroupMessageByFilter_MediaId")] 53 public class GroupMessageByGroupId_ImageData : BaseGroupMessageDataByGroupId 54 { 55 public GroupMessageByGroupId_MediaId image { get; set; } 56 } 57 58 [Obsolete("請使用GroupMessageByFilter_MediaId")] 59 public class GroupMessageByGroupId_TextData : BaseGroupMessageDataByGroupId 60 { 61 public GroupMessageByGroupId_Content text { get; set; } 62 } 63 64 [Obsolete("請使用GroupMessageByFilter_MediaId")] 65 public class GroupMessageByGroupId_MpNewsData : BaseGroupMessageDataByGroupId 66 { 67 public GroupMessageByGroupId_MediaId mpnews { get; set; } 68 } 69 70 [Obsolete("請使用GroupMessageByFilter_MediaId")] 71 public class GroupMessageByGroupId_MpVideoData : BaseGroupMessageDataByGroupId 72 { 73 public GroupMessageByGroupId_MediaId mpvideo { get; set; } 74 } 75 76 [Obsolete("請使用GroupMessageByFilter_MediaId")] 77 public class GroupMessageByGroupId_WxCardData : BaseGroupMessageDataByGroupId 78 { 79 public GroupMessageByGroupId_WxCard wxcard { get; set; } 80 } 81 82 #endregion 83 84 }
在 GroupMessageApi 中的使用上同樣保持了向下相容,根據 GroupId 和 TagId 群發的介面分別為 SendGroupMessageByGroupId() 以及 SendGroupMessageByTagId(),底層共同呼叫同一個私有方法 SendGroupMessageByFilter() :
1 /// <summary> 2 /// 根據分組或標籤進行群發【訂閱號與服務號認證後均可用】 3 /// 4 /// 請注意: 5 /// 1、該介面暫時僅提供給已微信認證的服務號 6 /// 2、雖然開發者使用高階群發介面的每日呼叫限制為100次,但是使用者每月只能接收4條,請小心測試 7 /// 3、無論在公眾平臺網站上,還是使用介面群發,使用者每月只能接收4條群發訊息,多於4條的群發將對該使用者傳送失敗。 8 /// 4、群發視訊時需要先呼叫GetVideoMediaIdResult介面獲取專用的MediaId然後進行群發 9 /// 10 /// </summary> 11 /// <param name="accessTokenOrAppId">AccessToken或AppId(推薦使用AppId,需要先註冊)</param> 12 /// <param name="groupId">群發到的分組的group_id,參見使用者管理中使用者分組介面,若is_to_all值為true,可不填寫group_id;如果groupId和tagId同時填寫,優先使用groupId;groupId和tagId最多隻能使用一個</param> 13 /// <param name="tagId">群發到的標籤的tag_id,若is_to_all值為true,可不填寫tag_id;如果groupId和tagId同時填寫,優先使用groupId;groupId和tagId最多隻能使用一個</param> 14 /// <param name="value">群發媒體檔案時傳入mediaId,群發文字訊息時傳入content,群髮卡券時傳入cardId</param> 15 /// <param name="type"></param> 16 /// <param name="isToAll">用於設定是否向全部使用者傳送,值為true或false,選擇true該訊息群發給所有使用者,選擇false可根據group_id傳送給指定群組的使用者</param> 17 /// <param name="sendIgnoreReprint">待群發的文章被判定為轉載時,是否繼續群發</param> 18 /// <param name="timeOut">代理請求超時時間(毫秒)</param> 19 /// <returns></returns> 20 private static SendResult SendGroupMessageByFilter(string accessTokenOrAppId, string groupId, string tagId, string value, GroupMessageType type, bool isToAll = false, bool sendIgnoreReprint = false, int timeOut = Config.TIME_OUT) 21 { 22 return ApiHandlerWapper.TryCommonApi(accessToken => 23 { 24 string urlFormat = Config.ApiMpHost + "/cgi-bin/message/mass/sendall?access_token={0}"; 25 26 BaseGroupMessageDataByFilter baseData = null; 27 BaseGroupMessageByFilter filter = null; 28 if (groupId.IsNullOrEmpty()) 29 { 30 filter = new GroupMessageByGroupId() 31 { 32 group_id = groupId, 33 is_to_all = isToAll, 34 }; 35 } 36 else 37 { 38 filter = new GroupMessageByTagId() 39 { 40 tag_id = tagId, 41 is_to_all = isToAll, 42 }; 43 } 44 45 switch (type) 46 { 47 case GroupMessageType.image: 48 baseData = new GroupMessageByFilter_ImageData() 49 { 50 filter = filter, 51 image = new GroupMessageByGroupId_MediaId() 52 { 53 media_id = value 54 }, 55 msgtype = "image" 56 }; 57 break; 58 case GroupMessageType.voice: 59 baseData = new GroupMessageByFilter_VoiceData() 60 { 61 filter = filter, 62 voice = new GroupMessageByGroupId_MediaId() 63 { 64 media_id = value 65 }, 66 msgtype = "voice" 67 }; 68 break; 69 case GroupMessageType.mpnews: 70 baseData = new GroupMessageByFilter_MpNewsData() 71 { 72 filter = filter, 73 mpnews = new GroupMessageByGroupId_MediaId() 74 { 75 media_id = value 76 }, 77 msgtype = "mpnews" 78 }; 79 break; 80 case GroupMessageType.video: 81 baseData = new GroupMessageByFilter_MpVideoData() 82 { 83 filter = filter, 84 mpvideo = new GroupMessageByGroupId_MediaId() 85 { 86 media_id = value 87 }, 88 msgtype = "mpvideo" 89 }; 90 break; 91 case GroupMessageType.wxcard: 92 baseData = new GroupMessageByFilter_WxCardData() 93 { 94 filter = filter, 95 wxcard = new GroupMessageByGroupId_WxCard() 96 { 97 card_id = value 98 }, 99 msgtype = "wxcard" 100 }; 101 break; 102 case GroupMessageType.text: 103 baseData = new GroupMessageByFilter_TextData() 104 { 105 filter = filter, 106 text = new GroupMessageByGroupId_Content() 107 { 108 content = value 109 }, 110 msgtype = "text" 111 }; 112 break; 113 default: 114 throw new Exception("引數錯誤。"); 115 //break; 116 } 117 118 baseData.send_ignore_reprint = sendIgnoreReprint ? 0 : 1;//待群發的文章被判定為轉載時,是否繼續群發 119 120 return CommonJsonSend.Send<SendResult>(accessToken, urlFormat, baseData, timeOut: timeOut); 121 122 }, accessTokenOrAppId); 123 }
本次更新同時更新了對應的非同步方法。