/// <summary> /// 根据OpenId进行群发 /// </summary> /// <param name="accessTokenOrAppId"></param> /// <param name="value">群发媒体文件时传入mediaId,群发文本消息时传入content,群发卡券时传入cardId</param> /// <param name="type"></param> /// <param name="openIds">openId字符串数组</param> /// <param name="timeOut">代理请求超时时间(毫秒)</param> /// <returns></returns> public static SendResult SendGroupMessageByOpenId(string accessTokenOrAppId, GroupMessageType type, string value, int timeOut = Config.TIME_OUT, params string[] openIds) { return(ApiHandlerWapper.TryCommonApi(accessToken => { const string urlFormat = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token={0}"; BaseGroupMessageDataByOpenId baseData = null; switch (type) { case GroupMessageType.image: baseData = new GroupMessageByOpenId_ImageData() { touser = openIds, image = new GroupMessageByOpenId_MediaId() { media_id = value }, msgtype = "image" }; break; case GroupMessageType.voice: baseData = new GroupMessageByOpenId_VoiceData() { touser = openIds, voice = new GroupMessageByOpenId_MediaId() { media_id = value }, msgtype = "voice" }; break; case GroupMessageType.mpnews: baseData = new GroupMessageByOpenId_MpNewsData() { touser = openIds, mpnews = new GroupMessageByOpenId_MediaId() { media_id = value }, msgtype = "mpnews" }; break; case GroupMessageType.wxcard: baseData = new GroupMessageByOpenId_WxCardData() { touser = openIds, wxcard = new GroupMessageByOpenId_WxCard() { card_id = value }, msgtype = "wxcard" }; break; case GroupMessageType.video: throw new Exception("发送视频信息请使用SendVideoGroupMessageByOpenId方法。"); break; case GroupMessageType.text: baseData = new GroupMessageByOpenId_TextData() { touser = openIds, text = new GroupMessageByOpenId_Content() { content = value }, msgtype = "text" }; break; default: throw new Exception("参数错误。"); break; } return CommonJsonSend.Send <SendResult>(accessToken, urlFormat, baseData, timeOut: timeOut); }, accessTokenOrAppId)); }
/// <summary> /// 【异步方法】根据OpenId进行群发【订阅号不可用,服务号认证后可用】 /// </summary> /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param> /// <param name="value">群发媒体文件时传入mediaId,群发文本消息时传入content,群发卡券时传入cardId</param> /// <param name="type"></param> /// <param name="openIds">openId字符串数组</param> /// <param name="clientmsgid">开发者侧群发msgid,长度限制64字节,如不填,则后台默认以群发范围和群发内容的摘要值做为clientmsgid</param> /// <param name="timeOut">代理请求超时时间(毫秒)</param> /// <returns></returns> public static async Task <SendResult> SendGroupMessageByOpenIdAsync(string accessTokenOrAppId, GroupMessageType type, string value, string clientmsgid = null, int timeOut = Config.TIME_OUT, params string[] openIds) { return(await ApiHandlerWapper.TryCommonApiAsync(async accessToken => { string urlFormat = Config.ApiMpHost + "/cgi-bin/message/mass/send?access_token={0}"; BaseGroupMessageDataByOpenId baseData = null; switch (type) { case GroupMessageType.image: baseData = new GroupMessageByOpenId_ImageData() { touser = openIds, image = new GroupMessageByOpenId_MediaId() { media_id = value }, msgtype = "image" }; break; case GroupMessageType.voice: baseData = new GroupMessageByOpenId_VoiceData() { touser = openIds, voice = new GroupMessageByOpenId_MediaId() { media_id = value }, msgtype = "voice" }; break; case GroupMessageType.mpnews: baseData = new GroupMessageByOpenId_MpNewsData() { touser = openIds, mpnews = new GroupMessageByOpenId_MediaId() { media_id = value }, msgtype = "mpnews" }; break; case GroupMessageType.wxcard: baseData = new GroupMessageByOpenId_WxCardData() { touser = openIds, wxcard = new GroupMessageByOpenId_WxCard() { card_id = value }, msgtype = "wxcard" }; break; case GroupMessageType.video: throw new Exception("发送视频信息请使用SendVideoGroupMessageByOpenId方法。"); //break; case GroupMessageType.text: baseData = new GroupMessageByOpenId_TextData() { touser = openIds, text = new GroupMessageByOpenId_Content() { content = value }, msgtype = "text" }; break; default: throw new Exception("参数错误。"); //break; } baseData.clientmsgid = clientmsgid; return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync <SendResult>(accessToken, urlFormat, baseData, timeOut: timeOut); }, accessTokenOrAppId)); }
/// <summary> /// 根据OpenId进行群发 /// </summary> /// <param name="value">群发媒体文件时传入mediaId,群发文本消息时传入content,群发卡券时传入cardId</param> /// <param name="type"></param> /// <param name="openIds">openId字符串数组</param> /// <param name="timeOut">代理请求超时时间(毫秒)</param> /// <returns></returns> public SendResult SendGroupMessageByOpenId(GroupMessageType type, string value, int timeOut = Config.TIME_OUT, params string[] openIds) { var accessToken = _api.GetAccessToken(); string url = string.Format("https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token={0}", accessToken); BaseGroupMessageDataByOpenId baseData = null; switch (type) { case GroupMessageType.image: baseData = new GroupMessageByOpenId_ImageData() { touser = openIds, image = new GroupMessageByOpenId_MediaId() { media_id = value }, msgtype = "image" }; break; case GroupMessageType.voice: baseData = new GroupMessageByOpenId_VoiceData() { touser = openIds, voice = new GroupMessageByOpenId_MediaId() { media_id = value }, msgtype = "voice" }; break; case GroupMessageType.mpnews: baseData = new GroupMessageByOpenId_MpNewsData() { touser = openIds, mpnews = new GroupMessageByOpenId_MediaId() { media_id = value }, msgtype = "mpnews" }; break; case GroupMessageType.wxcard: baseData = new GroupMessageByOpenId_WxCardData() { touser = openIds, wxcard = new GroupMessageByOpenId_WxCard() { card_id = value }, msgtype = "wxcard" }; break; case GroupMessageType.video: throw new Exception("发送视频信息请使用SendVideoGroupMessageByOpenId方法。"); break; case GroupMessageType.text: baseData = new GroupMessageByOpenId_TextData() { touser = openIds, text = new GroupMessageByOpenId_Content() { content = value }, msgtype = "text" }; break; default: throw new Exception("参数错误。"); } return(Post <SendResult>(url, baseData, timeOut)); }