[升级说明] Senparc.Weixin.MP v14.8.11 (微信群发接口调整)

   升级内容:添加根据标签群发接口,重构原根据分组群发接口

   参考微信文档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         }

 

    本次更新同时更新了对应的异步方法。

posted on 2017-12-18 13:08  SZW  阅读(934)  评论(0编辑  收藏  举报