/// <summary>
        /// 发送群组消息。
        /// </summary>
        /// <param name="filter">群组的筛选信息。</param>
        /// <param name="message">群组消息。</param>
        /// <returns>发送结果。</returns>
        public GroupSendResult SendByGroup(GroupFilter filter, GroupMessage message)
        {
            object filterObj;

            if (filter.GroupId.HasValue)
            {
                filterObj = new { is_to_all = false, group_id = filter.GroupId.Value };
            }
            else
            {
                filterObj = new { is_to_all = true };
            }

            return(SendMessage(message, obj =>
            {
                obj["filter"] = JObject.Parse(JsonConvert.SerializeObject(filterObj));
            }));
        }
        /// <summary>
        /// 发送群组消息。
        /// </summary>
        /// <param name="filter">群组的筛选信息。</param>
        /// <param name="message">群组消息。</param>
        public void SendByGroup(GroupFilter filter, GroupMessage message)
        {
            var url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=" + _accountModel.GetAccessToken();

            object filterObj;

            if (filter.GroupId.HasValue)
            {
                filterObj = new { is_to_all = false, group_id = filter.GroupId.Value };
            }
            else
            {
                filterObj = new { is_to_all = true };
            }

            string msgtype;

            switch (message.Type)
            {
                case GroupMessageType.News:
                    msgtype = "mpnews";
                    break;

                case GroupMessageType.Card:
                    msgtype = "wxcard";
                    break;

                case GroupMessageType.Image:
                    msgtype = "image";
                    break;

                case GroupMessageType.Music:
                    msgtype = "music";
                    break;

                case GroupMessageType.Text:
                    msgtype = "text";
                    break;

                case GroupMessageType.Video:
                    msgtype = "mpvideo";
                    var videoMessage = (GroupMessageVideo)message;
                    var uploadVideoUrl = "https://file.api.weixin.qq.com/cgi-bin/media/uploadvideo?access_token=" + _accountModel.GetAccessToken();
                    var result = WeiXinHttpHelper.PostString(uploadVideoUrl, new
                    {
                        media_id = videoMessage.MediaId,
                        title = videoMessage.Title,
                        description = videoMessage.Description
                    });
                    videoMessage.MediaId = JObject.Parse(result)["media_id"].Value<string>();
                    break;

                case GroupMessageType.Voice:
                    msgtype = "voice";
                    break;

                default:
                    throw new NotSupportedException("不支持的消息类型:" + message.Type);
            }

            var postJson = JsonConvert.SerializeObject(new
            {
                filter = filterObj,
                msgtype,
            });
            var postObj = JObject.Parse(postJson);
            postObj[msgtype] = JObject.Parse(JsonConvert.SerializeObject(message));

            WeiXinHttpHelper.Post(url, postObj.ToString());
        }
Beispiel #3
0
        /// <summary>
        /// 发送群组消息。
        /// </summary>
        /// <param name="filter">群组的筛选信息。</param>
        /// <param name="message">群组消息。</param>
        public void SendByGroup(GroupFilter filter, GroupMessage message)
        {
            var url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=" + _accountModel.GetAccessToken();

            object filterObj;

            if (filter.GroupId.HasValue)
            {
                filterObj = new { is_to_all = false, group_id = filter.GroupId.Value };
            }
            else
            {
                filterObj = new { is_to_all = true };
            }

            string msgtype;

            switch (message.Type)
            {
            case GroupMessageType.News:
                msgtype = "mpnews";
                break;

            case GroupMessageType.Card:
                msgtype = "wxcard";
                break;

            case GroupMessageType.Image:
                msgtype = "image";
                break;

            case GroupMessageType.Music:
                msgtype = "music";
                break;

            case GroupMessageType.Text:
                msgtype = "text";
                break;

            case GroupMessageType.Video:
                msgtype = "mpvideo";
                var videoMessage   = (GroupMessageVideo)message;
                var uploadVideoUrl = "https://file.api.weixin.qq.com/cgi-bin/media/uploadvideo?access_token=" + _accountModel.GetAccessToken();
                var result         = WeiXinHttpHelper.PostString(uploadVideoUrl, new
                {
                    media_id    = videoMessage.MediaId,
                    title       = videoMessage.Title,
                    description = videoMessage.Description
                });
                videoMessage.MediaId = JObject.Parse(result)["media_id"].Value <string>();
                break;

            case GroupMessageType.Voice:
                msgtype = "voice";
                break;

            default:
                throw new NotSupportedException("不支持的消息类型:" + message.Type);
            }

            var postJson = JsonConvert.SerializeObject(new
            {
                filter = filterObj,
                msgtype,
            });
            var postObj = JObject.Parse(postJson);

            postObj[msgtype] = JObject.Parse(JsonConvert.SerializeObject(message));

            WeiXinHttpHelper.Post(url, postObj.ToString());
        }