Esempio n. 1
0
        /// <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</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>
        public static async Task <SendResult> SendGroupMessageByGroupIdAsync(string accessTokenOrAppId, string groupId, string value, GroupMessageType type, bool isToAll = false, bool sendIgnoreReprint = false, int timeOut = Config.TIME_OUT)
        {
            return(await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
            {
                const string urlFormat = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token={0}";

                BaseGroupMessageDataByGroupId baseData = null;
                var filter = new GroupMessageByGroupId_GroupId()
                {
                    group_id = groupId,
                    is_to_all = isToAll
                };
                switch (type)
                {
                case GroupMessageType.image:
                    baseData = new GroupMessageByGroupId_ImageData()
                    {
                        filter = filter,
                        image = new GroupMessageByGroupId_MediaId()
                        {
                            media_id = value
                        },
                        msgtype = "image"
                    };
                    break;

                case GroupMessageType.voice:
                    baseData = new GroupMessageByGroupId_VoiceData()
                    {
                        filter = filter,
                        voice = new GroupMessageByGroupId_MediaId()
                        {
                            media_id = value
                        },
                        msgtype = "voice"
                    };
                    break;

                case GroupMessageType.mpnews:
                    baseData = new GroupMessageByGroupId_MpNewsData()
                    {
                        filter = filter,
                        mpnews = new GroupMessageByGroupId_MediaId()
                        {
                            media_id = value
                        },
                        msgtype = "mpnews"
                    };
                    break;

                case GroupMessageType.video:
                    baseData = new GroupMessageByGroupId_MpVideoData()
                    {
                        filter = filter,
                        mpvideo = new GroupMessageByGroupId_MediaId()
                        {
                            media_id = value
                        },
                        msgtype = "mpvideo"
                    };
                    break;

                case GroupMessageType.wxcard:
                    baseData = new GroupMessageByGroupId_WxCardData()
                    {
                        filter = filter,
                        wxcard = new GroupMessageByGroupId_WxCard()
                        {
                            card_id = value
                        },
                        msgtype = "wxcard"
                    };
                    break;

                case GroupMessageType.text:
                    baseData = new GroupMessageByGroupId_TextData()
                    {
                        filter = filter,
                        text = new GroupMessageByGroupId_Content()
                        {
                            content = value
                        },
                        msgtype = "text"
                    };
                    break;

                default:
                    throw new Exception("参数错误。");
                    //break;
                }

                baseData.send_ignore_reprint = sendIgnoreReprint ? 0 : 1;//待群发的文章被判定为转载时,是否继续群发

                return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync <SendResult>(accessToken, urlFormat, baseData, timeOut: timeOut);
            }, accessTokenOrAppId));
        }
Esempio n. 2
0
        /// <summary>
        /// 根据分组进行群发【订阅号与服务号认证后均可用】
        ///
        /// 请注意:
        /// 1、该接口暂时仅提供给已微信认证的服务号
        /// 2、虽然开发者使用高级群发接口的每日调用限制为100次,但是用户每月只能接收4条,请小心测试
        /// 3、无论在公众平台网站上,还是使用接口群发,用户每月只能接收4条群发消息,多于4条的群发将对该用户发送失败。
        /// 4、群发视频时需要先调用GetVideoMediaIdResult接口获取专用的MediaId然后进行群发
        ///
        /// </summary>
        /// <param name="accessTokenOrAppId"></param>
        /// <param name="groupId">群发到的分组的group_id,参加用户管理中用户分组接口,若is_to_all值为true,可不填写group_id</param>
        /// <param name="value">群发媒体文件时传入mediaId,群发文本消息时传入content,群发卡券时传入cardId</param>
        /// <param name="type"></param>
        /// <param name="isToAll">用于设定是否向全部用户发送,值为true或false,选择true该消息群发给所有用户,选择false可根据group_id发送给指定群组的用户</param>
        /// <param name="timeOut">代理请求超时时间(毫秒)</param>
        /// <returns></returns>
        public static SendResult SendGroupMessageByGroupId(string accessTokenOrAppId, string groupId, string value, GroupMessageType type, bool isToAll = false, int timeOut = Config.TIME_OUT)
        {
            return(ApiHandlerWapper.TryCommonApi(accessToken =>
            {
                const string urlFormat = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token={0}";

                BaseGroupMessageDataByGroupId baseData = null;
                switch (type)
                {
                case GroupMessageType.image:
                    baseData = new GroupMessageByGroupId_ImageData()
                    {
                        filter = new GroupMessageByGroupId_GroupId()
                        {
                            group_id = groupId,
                            is_to_all = isToAll
                        },
                        image = new GroupMessageByGroupId_MediaId()
                        {
                            media_id = value
                        },
                        msgtype = "image"
                    };
                    break;

                case GroupMessageType.voice:
                    baseData = new GroupMessageByGroupId_VoiceData()
                    {
                        filter = new GroupMessageByGroupId_GroupId()
                        {
                            group_id = groupId,
                            is_to_all = isToAll
                        },
                        voice = new GroupMessageByGroupId_MediaId()
                        {
                            media_id = value
                        },
                        msgtype = "voice"
                    };
                    break;

                case GroupMessageType.mpnews:
                    baseData = new GroupMessageByGroupId_MpNewsData()
                    {
                        filter = new GroupMessageByGroupId_GroupId()
                        {
                            group_id = groupId,
                            is_to_all = isToAll
                        },
                        mpnews = new GroupMessageByGroupId_MediaId()
                        {
                            media_id = value
                        },
                        msgtype = "mpnews"
                    };
                    break;

                case GroupMessageType.video:
                    baseData = new GroupMessageByGroupId_MpVideoData()
                    {
                        filter = new GroupMessageByGroupId_GroupId()
                        {
                            group_id = groupId,
                            is_to_all = isToAll
                        },
                        mpvideo = new GroupMessageByGroupId_MediaId()
                        {
                            media_id = value
                        },
                        msgtype = "mpvideo"
                    };
                    break;

                case GroupMessageType.wxcard:
                    baseData = new GroupMessageByGroupId_WxCardData()
                    {
                        filter = new GroupMessageByGroupId_GroupId()
                        {
                            group_id = groupId,
                            is_to_all = isToAll
                        },
                        wxcard = new GroupMessageByGroupId_WxCard()
                        {
                            card_id = value
                        },
                        msgtype = "wxcard"
                    };
                    break;

                case GroupMessageType.text:
                    baseData = new GroupMessageByGroupId_TextData()
                    {
                        filter = new GroupMessageByGroupId_GroupId()
                        {
                            group_id = groupId,
                            is_to_all = isToAll
                        },
                        text = new GroupMessageByGroupId_Content()
                        {
                            content = value
                        },
                        msgtype = "text"
                    };
                    break;

                default:
                    throw new Exception("参数错误。");
                    break;
                }

                return CommonJsonSend.Send <SendResult>(accessToken, urlFormat, baseData, timeOut: timeOut);
            }, accessTokenOrAppId));
        }
Esempio n. 3
0
        /// <summary>
        /// 根据分组进行群发【订阅号与服务号认证后均可用】
        ///
        /// 请注意:
        /// 1、该接口暂时仅提供给已微信认证的服务号
        /// 2、虽然开发者使用高级群发接口的每日调用限制为100次,但是用户每月只能接收4条,请小心测试
        /// 3、无论在公众平台网站上,还是使用接口群发,用户每月只能接收4条群发消息,多于4条的群发将对该用户发送失败。
        ///
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="groupId">群发到的分组的group_id,参加用户管理中用户分组接口,若is_to_all值为true,可不填写group_id</param>
        /// <param name="mediaId">用于群发的消息的media_id</param>
        /// <param name="type"></param>
        /// <param name="isToAll">用于设定是否向全部用户发送,值为true或false,选择true该消息群发给所有用户,选择false可根据group_id发送给指定群组的用户</param>
        /// <returns></returns>
        public static SendResult SendGroupMessageByGroupId(string accessToken, string groupId, string mediaId, GroupMessageType type, bool isToAll = false)
        {
            const string urlFormat = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token={0}";

            BaseGroupMessageDataByGroupId baseData = null;

            switch (type)
            {
            case GroupMessageType.image:
                baseData = new GroupMessageByGroupId_ImageData()
                {
                    filter = new GroupMessageByGroupId_GroupId()
                    {
                        group_id  = groupId,
                        is_to_all = isToAll
                    },
                    image = new GroupMessageByGroupId_MediaId()
                    {
                        media_id = mediaId
                    },
                    msgtype = "image"
                };
                break;

            case GroupMessageType.voice:
                baseData = new GroupMessageByGroupId_VoiceData()
                {
                    filter = new GroupMessageByGroupId_GroupId()
                    {
                        group_id  = groupId,
                        is_to_all = isToAll
                    },
                    voice = new GroupMessageByGroupId_MediaId()
                    {
                        media_id = mediaId
                    },
                    msgtype = "voice"
                };
                break;

            case GroupMessageType.mpnews:
                baseData = new GroupMessageByGroupId_MpNewsData()
                {
                    filter = new GroupMessageByGroupId_GroupId()
                    {
                        group_id  = groupId,
                        is_to_all = isToAll
                    },
                    mpnews = new GroupMessageByGroupId_MediaId()
                    {
                        media_id = mediaId
                    },
                    msgtype = "mpnews"
                };
                break;

            case GroupMessageType.video:
                baseData = new GroupMessageByGroupId_MpVideoData()
                {
                    filter = new GroupMessageByGroupId_GroupId()
                    {
                        group_id  = groupId,
                        is_to_all = isToAll
                    },
                    mpvideo = new GroupMessageByGroupId_MediaId()
                    {
                        media_id = mediaId
                    },
                    msgtype = "mpvideo"
                };
                break;

            case GroupMessageType.text:
                throw new Exception("发送文本信息请使用SendTextGroupMessageByGroupId方法。");
                break;

            default:
                throw new Exception("参数错误。");
                break;
            }

            return(CommonJsonSend.Send <SendResult>(accessToken, urlFormat, baseData));
        }