/// <summary>
        /// 根据OpenId进行群发
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="mediaId">用于群发的消息的media_id</param>
        /// <param name="type"></param>
        /// <param name="openIds">openId字符串数组</param>
        /// 注意mediaId和content不可同时为空
        /// <returns></returns>
        public static SendResult SendGroupMessageByOpenId(string accessToken, GroupMessageType type, string mediaId, params string[] openIds)
        {
            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 = mediaId
                    },
                    msgtype = "image"
                };
                break;

            case GroupMessageType.voice:
                baseData = new GroupMessageByOpenId_VoiceData()
                {
                    touser = openIds,
                    voice  = new GroupMessageByOpenId_MediaId()
                    {
                        media_id = mediaId
                    },
                    msgtype = "voice"
                };
                break;

            case GroupMessageType.mpnews:
                baseData = new GroupMessageByOpenId_MpNewsData()
                {
                    touser = openIds,
                    mpnews = new GroupMessageByOpenId_MediaId()
                    {
                        media_id = mediaId
                    },
                    msgtype = "mpnews"
                };
                break;

            case GroupMessageType.video:
                throw new Exception("发送视频信息请使用SendVideoGroupMessageByOpenId方法。");
                break;

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

            default:
                throw new Exception("参数错误。");
                break;
            }
            return(CommonJsonSend.Send <SendResult>(accessToken, urlFormat, baseData));
        }
예제 #2
0
        /// <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));
        }
예제 #3
0
        /// <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));
        }