Ejemplo n.º 1
0
        /// <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));
        }
Ejemplo n.º 2
0
        /// <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);
        }
Ejemplo n.º 3
0
        /// <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));
        }
Ejemplo n.º 4
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));
        }