Exemplo n.º 1
0
        /// <summary>
        /// 根据OpenID列表群发【订阅号不可用,服务号认证后可用】
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="content">图文消息,语音,图片,视频:media_id; 文本:文本消息</param>
        /// <param name="type"></param>
        /// <param name="touser"></param>
        /// <returns>success:{"errcode":0,"errmsg":"send job submission success","msg_id":34182}</returns>
        public static dynamic ReplayOpenids(string access_token, string content, WeixinArtcleType type, IEnumerable <string> touser, string videoTitle, string videoDesc)
        {
            var url     = string.Format("https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token={0}", access_token);
            var client  = new HttpClient();
            var builder = new StringBuilder();

            builder.Append("{")
            .Append('"' + "touser" + '"' + ":")
            .Append("[");
            foreach (var t in touser)
            {
                builder.Append('"' + t + '"').Append(",");
            }
            builder.Append("],");

            switch (type)
            {
            case WeixinArtcleType.News:
                builder.Append('"' + "mpnews" + '"' + ":")
                .Append("{")
                .Append('"' + "media_id" + '"' + ":").Append(content)
                .Append("},")
                .Append('"' + "msgtype" + '"' + ":").Append("mpnews");
                break;

            case WeixinArtcleType.Text:
                builder.Append('"' + "text" + '"' + ":")
                .Append("{")
                .Append('"' + "content" + '"' + ":").Append(content)
                .Append("},")
                .Append('"' + "msgtype" + '"' + ":").Append("text");
                break;

            case WeixinArtcleType.Voice:
                builder.Append('"' + "voice" + '"' + ":")
                .Append("{")
                .Append('"' + "media_id" + '"' + ":").Append(content)
                .Append("},")
                .Append('"' + "msgtype" + '"' + ":").Append("voice");
                break;

            case WeixinArtcleType.Image:
                builder.Append('"' + "image" + '"' + ":")
                .Append("{")
                .Append('"' + "media_id" + '"' + ":").Append(content)
                .Append("},")
                .Append('"' + "msgtype" + '"' + ":").Append("image");
                break;

            case WeixinArtcleType.Video:
                builder.Append('"' + "video" + '"' + ":")
                .Append("{")
                .Append('"' + "media_id" + '"' + ":").Append(content).Append(",")
                .Append('"' + "title" + '"' + ":").Append(videoTitle).Append(",")
                .Append('"' + "description" + '"' + ":").Append(videoDesc)
                .Append("},")
                .Append('"' + "msgtype" + '"' + ":").Append("video");
                break;
            }
            builder.Append("}");
            var result = client.PostAsync(url, new StringContent(builder.ToString())).Result;

            return(DynamicJson.Parse(result.Content.ReadAsStringAsync().Result));
        }