C# 调用微信群发接口

复制代码
   //新增永久图片素材
    public string add_matrial(string accesstoke,string picurl,out string errmsg)
    {
        string result = string.Empty;
        errmsg = "";
        string addMatrialAPI = string.Format("https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={0}&type=image",accesstoke);
        WebClient wc = new WebClient();
        wc.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko");
        byte[] bs = wc.DownloadData(picurl);
        MemoryStream ms = new MemoryStream(bs);
        string res = PostMultipart(addMatrialAPI,
                    new Dictionary<string, object>() {
                            { "media", new FormFile() { Name = "image1.jpg", ContentType = "image/jpeg", Stream = ms } },
                    });

        JObject jobject= JsonConvert.DeserializeObject<JObject>(res);
        if(jobject!=null)
        {
            if(jobject["errcode"]!=null&&jobject["errcode"].ToString()!="0")
            {
                errmsg = jobject["errmsg"].ToString();
            }
            else
            {
                result=jobject["media_id"].ToString();
            }
        }
        return result;
    }
    //新增草稿
    public string add_draft(string accesstoken,string thumb_media_id,string title,string content,out string errmsg)
    {
        string add_draft_url = string.Format("https://api.weixin.qq.com/cgi-bin/draft/add?access_token={0}", accesstoken);
        int code= 200;
        errmsg = "";
        string result = string.Empty;
        Dictionary<string, List<dynamic>> dic = new Dictionary<string, List<dynamic>>();
        dic.Add("articles", new List<dynamic>{ new
            {
            title=title,
            content=content,
            thumb_media_id=thumb_media_id
            } });
        string parastr = JsonConvert.SerializeObject(dic);
        string res= HttpHelper.PostJSON(add_draft_url, parastr,Encoding.UTF8,out  code);
        JObject jobject= JsonConvert.DeserializeObject<JObject>(res);
        if(jobject!=null)
        {
            if(jobject["errcode"]!=null&&jobject["errcode"].ToString()!="0")
            {
                errmsg = jobject["errmsg"].ToString();
            }
            else
            {
                result=jobject["media_id"].ToString();
            }
        }
        return result;
    }
    //调用群发接口
    public string sendall(string accesstoken,string media_id,out string errmsg )
    {
        string add_draft_url = string.Format("https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token={0}", accesstoken);
        int code= 200;
        errmsg = "";
        string result = string.Empty;
        dynamic para =new
        {
            filter=new
            {
                is_to_all=true
            },
            mpnews=new
            {
                media_id = media_id
            },
            msgtype = "mpnews",
            send_ignore_reprint = 1
        };

        string parastr = JsonConvert.SerializeObject(para);
        string res= HttpHelper.PostJSON(add_draft_url, parastr,Encoding.UTF8,out  code);
        JObject jobject= JsonConvert.DeserializeObject<JObject>(res);
        if(jobject!=null)
        {
            if(jobject["errcode"]!=null&&jobject["errcode"].ToString()!="0")
            {
                errmsg = jobject["errmsg"].ToString();
            }
            else
            {
                result=jobject["msg_data_id"].ToString();
            }
        }
        return result;
    }
复制代码

 

posted @   极客船长  阅读(354)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示