升级内容:添加根据标签群发接口,重构原根据分组群发接口
参考微信文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1481187827_i0l21
说明:
之前 SDK 只提供了根据用户组群发的方法:
GroupMessageApi.SendGroupMessageByGroupId();
由于根据分组和标签群发两种情况,逻辑基本一致,只有一个filter参数不同,因此我们创建了 BaseGroupMessageDataByFilter 基类,用于支持 GroupMessageByGroupId 和 GroupMessageByTagId。
BaseGroupMessageByFilter 代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Senparc.Weixin.MP.AdvancedAPIs.GroupMessage
{
/// <summary>
/// 根据筛选条件(GroupId、TagId)群发消息数据的基类
/// </summary>
public abstract class BaseGroupMessageByFilter
{
public bool is_to_all { get; set; }
} public class BaseGroupMessageDataByFilter
{
public BaseGroupMessageByFilter filter { get; set; } public string msgtype { get; set; } /// <summary>
/// 群发接口新增 send_ignore_reprint 参数,开发者可以对群发接口的 send_ignore_reprint 参数进行设置,指定待群发的文章被判定为转载时,是否继续群发。
/// 当 send_ignore_reprint 参数设置为1时,文章被判定为转载时,且原创文允许转载时,将继续进行群发操作。
/// 当 send_ignore_reprint 参数设置为0时,文章被判定为转载时,将停止群发操作。
/// send_ignore_reprint 默认为0。
/// </summary>
public int send_ignore_reprint { get; set; }
} public class GroupMessageByFilter_MediaId
{
public string media_id { get; set; }
} public class GroupMessageByFilter_Content
{
public string content { get; set; }
} public class GroupMessageByFilter_WxCard
{
public string card_id { get; set; }
} public class GroupMessageByFilter_VoiceData : BaseGroupMessageDataByFilter
{
public GroupMessageByGroupId_MediaId voice { get; set; }
} public class GroupMessageByFilter_ImageData : BaseGroupMessageDataByFilter
{
public GroupMessageByGroupId_MediaId image { get; set; }
} public class GroupMessageByFilter_TextData : BaseGroupMessageDataByFilter
{
public GroupMessageByGroupId_Content text { get; set; }
} public class GroupMessageByFilter_MpNewsData : BaseGroupMessageDataByFilter
{
public GroupMessageByGroupId_MediaId mpnews { get; set; }
} public class GroupMessageByFilter_MpVideoData : BaseGroupMessageDataByFilter
{
public GroupMessageByGroupId_MediaId mpvideo { get; set; }
} public class GroupMessageByFilter_WxCardData : BaseGroupMessageDataByFilter
{
public GroupMessageByGroupId_WxCard wxcard { get; set; }
}
}
GroupMessageByTagId 代码如下:
namespace Senparc.Weixin.MP.AdvancedAPIs.GroupMessage
{
/// <summary>
/// 根据 TagId 群发筛选
/// </summary>
public class GroupMessageByTagId : BaseGroupMessageByFilter
{
public string tag_id { get; set; }
}
}
GroupMessageByGroupId 代码如下(包含已经弃用的代码),为了保障向下兼容性,标记了[Obsolete]特性:
namespace Senparc.Weixin.MP.AdvancedAPIs.GroupMessage
{
/// <summary>
/// 根据GroupId群发筛选
/// </summary>
public class GroupMessageByGroupId : BaseGroupMessageByFilter
{
public string group_id { get; set; }
} #region 已废弃 [Obsolete("请使用GroupMessageByGroupId")]
public class GroupMessageByGroupId_GroupId : BaseGroupMessageByFilter
{
public string group_id { get; set; }
} /// <summary>
/// 根据GroupId群发消息筛选
/// </summary>
[Obsolete("请使用BaseGroupMessageDataByFilter")]
public class BaseGroupMessageDataByGroupId : BaseGroupMessageDataByFilter
{ } [Obsolete("请使用GroupMessageByFilter_MediaId")]
public class GroupMessageByGroupId_MediaId
{
public string media_id { get; set; }
} [Obsolete("请使用GroupMessageByFilter_MediaId")]
public class GroupMessageByGroupId_Content
{
public string content { get; set; }
} [Obsolete("请使用GroupMessageByFilter_MediaId")]
public class GroupMessageByGroupId_WxCard
{
public string card_id { get; set; }
} [Obsolete("请使用GroupMessageByFilter_MediaId")]
public class GroupMessageByGroupId_VoiceData : BaseGroupMessageDataByGroupId
{
public GroupMessageByGroupId_MediaId voice { get; set; }
} [Obsolete("请使用GroupMessageByFilter_MediaId")]
public class GroupMessageByGroupId_ImageData : BaseGroupMessageDataByGroupId
{
public GroupMessageByGroupId_MediaId image { get; set; }
} [Obsolete("请使用GroupMessageByFilter_MediaId")]
public class GroupMessageByGroupId_TextData : BaseGroupMessageDataByGroupId
{
public GroupMessageByGroupId_Content text { get; set; }
} [Obsolete("请使用GroupMessageByFilter_MediaId")]
public class GroupMessageByGroupId_MpNewsData : BaseGroupMessageDataByGroupId
{
public GroupMessageByGroupId_MediaId mpnews { get; set; }
} [Obsolete("请使用GroupMessageByFilter_MediaId")]
public class GroupMessageByGroupId_MpVideoData : BaseGroupMessageDataByGroupId
{
public GroupMessageByGroupId_MediaId mpvideo { get; set; }
} [Obsolete("请使用GroupMessageByFilter_MediaId")]
public class GroupMessageByGroupId_WxCardData : BaseGroupMessageDataByGroupId
{
public GroupMessageByGroupId_WxCard wxcard { get; set; }
} #endregion }
在 GroupMessageApi 中的使用上同样保持了向下兼容,根据 GroupId 和 TagId 群发的接口分别为 SendGroupMessageByGroupId() 以及 SendGroupMessageByTagId(),底层共同调用同一个私有方法 SendGroupMessageByFilter() :
/// <summary>
/// 根据分组或标签进行群发【订阅号与服务号认证后均可用】
///
/// 请注意:
/// 1、该接口暂时仅提供给已微信认证的服务号
/// 2、虽然开发者使用高级群发接口的每日调用限制为100次,但是用户每月只能接收4条,请小心测试
/// 3、无论在公众平台网站上,还是使用接口群发,用户每月只能接收4条群发消息,多于4条的群发将对该用户发送失败。
/// 4、群发视频时需要先调用GetVideoMediaIdResult接口获取专用的MediaId然后进行群发
///
/// </summary>
/// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
/// <param name="groupId">群发到的分组的group_id,参见用户管理中用户分组接口,若is_to_all值为true,可不填写group_id;如果groupId和tagId同时填写,优先使用groupId;groupId和tagId最多只能使用一个</param>
/// <param name="tagId">群发到的标签的tag_id,若is_to_all值为true,可不填写tag_id;如果groupId和tagId同时填写,优先使用groupId;groupId和tagId最多只能使用一个</param>
/// <param name="value">群发媒体文件时传入mediaId,群发文本消息时传入content,群发卡券时传入cardId</param>
/// <param name="type"></param>
/// <param name="isToAll">用于设定是否向全部用户发送,值为true或false,选择true该消息群发给所有用户,选择false可根据group_id发送给指定群组的用户</param>
/// <param name="sendIgnoreReprint">待群发的文章被判定为转载时,是否继续群发</param>
/// <param name="timeOut">代理请求超时时间(毫秒)</param>
/// <returns></returns>
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)
{
return ApiHandlerWapper.TryCommonApi(accessToken =>
{
string urlFormat = Config.ApiMpHost + "/cgi-bin/message/mass/sendall?access_token={0}"; BaseGroupMessageDataByFilter baseData = null;
BaseGroupMessageByFilter filter = null;
if (groupId.IsNullOrEmpty())
{
filter = new GroupMessageByGroupId()
{
group_id = groupId,
is_to_all = isToAll,
};
}
else
{
filter = new GroupMessageByTagId()
{
tag_id = tagId,
is_to_all = isToAll,
};
} switch (type)
{
case GroupMessageType.image:
baseData = new GroupMessageByFilter_ImageData()
{
filter = filter,
image = new GroupMessageByGroupId_MediaId()
{
media_id = value
},
msgtype = "image"
};
break;
case GroupMessageType.voice:
baseData = new GroupMessageByFilter_VoiceData()
{
filter = filter,
voice = new GroupMessageByGroupId_MediaId()
{
media_id = value
},
msgtype = "voice"
};
break;
case GroupMessageType.mpnews:
baseData = new GroupMessageByFilter_MpNewsData()
{
filter = filter,
mpnews = new GroupMessageByGroupId_MediaId()
{
media_id = value
},
msgtype = "mpnews"
};
break;
case GroupMessageType.video:
baseData = new GroupMessageByFilter_MpVideoData()
{
filter = filter,
mpvideo = new GroupMessageByGroupId_MediaId()
{
media_id = value
},
msgtype = "mpvideo"
};
break;
case GroupMessageType.wxcard:
baseData = new GroupMessageByFilter_WxCardData()
{
filter = filter,
wxcard = new GroupMessageByGroupId_WxCard()
{
card_id = value
},
msgtype = "wxcard"
};
break;
case GroupMessageType.text:
baseData = new GroupMessageByFilter_TextData()
{
filter = filter,
text = new GroupMessageByGroupId_Content()
{
content = value
},
msgtype = "text"
};
break;
default:
throw new Exception("参数错误。");
//break;
} baseData.send_ignore_reprint = sendIgnoreReprint ? : ;//待群发的文章被判定为转载时,是否继续群发 return CommonJsonSend.Send<SendResult>(accessToken, urlFormat, baseData, timeOut: timeOut); }, accessTokenOrAppId);
}
本次更新同时更新了对应的异步方法。