微信公众号群发消息

1.首先登录微信公众号平台 https://mp.weixin.qq.com/

2.公众号文档 https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1445241432

3.如果没有账号可以申请测试号 但是测试号可能会权限不足

4.登录以后可以在接口权限查看 是否有群发权限 因为我用的公司服务号 所以权限都是有的

本文主要功能是将Pc系统的稿件文章推送给对应公众号

5.接下来就是获取到access_token 

access_token是公众号的全局唯一接口调用凭据 公众号调用各接口时都需使用access_token 有效期2小时 重复获取之前的token会失效

接口文档写的比较详细 

请求URL:https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET    GET请求方式

 //获取公众号全局token
    public String getToken() {
        String grant_type = "client_credential";// 授予形式
        String appid = "***************";  //应用ID
        String secret = "**********************";//密钥
        // 接口地址拼接参数
        String getTokenApi = "https://api.weixin.qq.com/cgi-bin/token?grant_type=" + grant_type + "&appid=" + appid
                + "&secret=" + secret;
        String tokenJsonStr = doGetPost(getTokenApi, "GET", null,"application/json");
        JSONObject tokenJson = JSONObject.parseObject(tokenJsonStr);
        String token = tokenJson.get("access_token").toString();
        System.out.println("获取到的TOKEN : " + token);
        return token;
    }

获取到token以后就可以调用群发消息的接口了

接口调用请求说明

http请求方式: POST https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=ACCESS_TOKEN

 text类型消息可以直接群发 

这里代码直接设定发送给所有用户,如果只发给指定用户可以调用用户管理中用户分组接口

 /***
     * 群发文本消息
     * @param
     */
    public Integer sendTxTMessage(String content) {
        String token = getToken();
        // 接口地址
        String sendMsgApi = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token="+token;
        //整体参数map
        Map<String, Object> paramMap = new HashMap<String, Object>();
        //相关map
        Map<String, Object> dataMap1 = new HashMap<String, Object>();
        Map<String, String> dataMap2 = new HashMap<String, String>();
        dataMap1.put("is_to_all",true);//用于设定是否向全部用户发送,值为true或false,选择true该消息群发给所有用户,选择false可根据tag_id发送给指定群组的用户
        dataMap1.put("tag_id",1);//群发到的标签的tag_id,参见用户管理中用户分组接口,若is_to_all值为true,可不填写tag_id
        dataMap2.put("content",content);//要推送的内容
        paramMap.put("filter",dataMap1);//用于设定图文消息的接收者
        paramMap.put("text", dataMap2);//文本内容
        paramMap.put("msgtype","text");//群发的消息类型,图文消息为mpnews,文本消息为text,语音为voice,音乐为music,图片为image,视频为video,卡券为wxcard
        String back = doGetPost(sendMsgApi,"POST",paramMap,"application/json");
        System.out.println("群发返回:"+back);
        JSONObject jsonObject =JSONObject.parseObject(back);//String转JSONObject,
        Integer re=jsonObject.getInteger("errcode");
        return re;
    }

其他类型消息需要先上传消息素材 

接口调用请求说明

http请求方式: POST https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=ACCESS_TOKEN

/**
     * 上传图文消息素材【订阅号与服务号认证后均可用】
     * @param articles
     * @return
     */
    public String weixinUploadNews(List<WeixinArticle> articles) {
        String token = getToken();
        String url = "https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token="+token;
//        Map<String, Object> paramMap1 = new HashMap<String, Object>();
        List<Map<String, Object>> list = new ArrayList<>();
        for (WeixinArticle article : articles){
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("thumb_media_id", article.getThumb_media_id());
            paramMap.put("author", article.getAuthor());
            paramMap.put("title", article.getTitle());
            paramMap.put("content_source_url", article.getContent_source_url());
            paramMap.put("content", article.getContent());
            paramMap.put("digest", article.getDigest());
            paramMap.put("show_cover_pic", article.getShow_cover_pic());
            list.add(paramMap);
        }
        Map<String, Object> paramMap2 = new HashMap<String, Object>();
        paramMap2.put("articles",list);
        String response = doGetPost(url,"POST",paramMap2,"application/json");
        JSONObject json = JSONObject.parseObject(response);
        String mediaId = json.getString("media_id");
        return mediaId;
    }

然后就是可以群发图集消息了

/***
     * 群发图集消息
     * @param
     */
    public Integer sendAtlas(String[] media_id) {
        String token = getToken();
        String sendMsgApi = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token="+token;
        //整体参数map
        Map<String, Object> paramMap = new HashMap<String, Object>();
        //相关map
        Map<String, Object> dataMap1 = new HashMap<String, Object>();
        Map<String, String[]> dataMap2 = new HashMap<>();
        dataMap1.put("is_to_all",true);//用于设定是否向全部用户发送,值为true或false,选择true该消息群发给所有用户,
        paramMap.put("filter",dataMap1);//用于设定图文消息的接收者
        dataMap2.put("media_ids",media_id);
        paramMap.put("images", dataMap2);//图集
        paramMap.put("msgtype","image");//群发的消息类型,图片为image
        paramMap.put("send_ignore_reprint", "1");
        String back = doGetPost(sendMsgApi,"POST",paramMap,"application/json");
        System.out.println("群发返回:"+back);
        JSONObject jsonObject =JSONObject.parseObject(back);//String转JSONObject,
        Integer re=jsonObject.getInteger("errcode");
        return re;
    }

群发图文消息的时候需要 将图片上传到微信 取到微信路径 才能被解析展示要不然就没办法展示图片

/***
     * 群发图文消息
     * @param
     */
    public Integer sendTxTNews(String media_id) {
        String token = getToken(d);
        String sendMsgApi = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token="+token;
        //整体参数map
        Map<String, Object> paramMap = new HashMap<String, Object>();
        //相关map
        Map<String, Object> dataMap1 = new HashMap<String, Object>();
        Map<String, String> dataMap2 = new HashMap<String, String>();
        dataMap1.put("is_to_all",true);//用于设定是否向全部用户发送,值为true或false,选择true该消息群发给所有用户,
        paramMap.put("filter",dataMap1);//用于设定图文消息的接收者
        dataMap2.put("media_id",media_id);
        paramMap.put("mpnews", dataMap2);//图文内容
        paramMap.put("msgtype","mpnews");//群发的消息类型,图文消息为mpnews
        paramMap.put("send_ignore_reprint", "1");
        String back = doGetPost(sendMsgApi,"POST",paramMap,"application/json");
        System.out.println("群发返回:"+back);
        JSONObject jsonObject =JSONObject.parseObject(back);//String转JSONObject,
        Integer re=jsonObject.getInteger("errcode");
        return re;
    }

群发音频,视频都是同样方法

/***
     * 群发音频
     * @param
     */
    public Integer sendVoices(String media_id) {
        String token = getToken();
        String sendMsgApi = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token="+token;
        //整体参数map
        Map<String, Object> paramMap = new HashMap<String, Object>();
        //相关map
        Map<String, Object> dataMap1 = new HashMap<String, Object>();
        Map<String, String> dataMap2 = new HashMap<String, String>();
        dataMap1.put("is_to_all",true);//用于设定是否向全部用户发送,值为true或false,选择true该消息群发给所有用户,
        paramMap.put("filter",dataMap1);//用于设定图文消息的接收者
        dataMap2.put("media_id",media_id);
        paramMap.put("voice", dataMap2);//音频内容
        paramMap.put("msgtype","voice");//群发的消息类型,语音/音频为voice
        paramMap.put("send_ignore_reprint", "1");
        String back = doGetPost(sendMsgApi,"POST",paramMap,"application/json");
        System.out.println("群发返回:"+back);
        JSONObject jsonObject =JSONObject.parseObject(back);//String转JSONObject,
        Integer re=jsonObject.getInteger("errcode");
        return re;
    }

 视频我测试时候一直返回-1 查询文档说是系统繁忙 百度说是因为视频需要审核 对了由于我们公司是服务号 所以只支持一个月群发4次 如果你们是订阅号可以一天一次的 因为4次机会被我用完了 所以群发视频的欢迎大家留言指导

/***
     * 群发视频
     * @param
     */
    public Integer sendVideo(String media_id) {
        String token = getToken();
        String sendMsgApi = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token="+token;
        //整体参数map
        Map<String, Object> paramMap = new HashMap<String, Object>();
        //相关map
        Map<String, Object> dataMap1 = new HashMap<String, Object>();
        Map<String, String> dataMap2 = new HashMap<String, String>();
        dataMap1.put("is_to_all",true);//用于设定是否向全部用户发送,值为true或false,选择true该消息群发给所有用户,
        paramMap.put("filter",dataMap1);//用于设定图文消息的接收者
        dataMap2.put("media_id",media_id);
        paramMap.put("mpvideo", dataMap2);//视频内容
        paramMap.put("msgtype","mpvideo");//群发的消息类型,视频为mpvideo
        paramMap.put("send_ignore_reprint", "1");
        String back = doGetPost(sendMsgApi,"POST",paramMap,"application/json");
        System.out.println("群发返回:"+back);
        JSONObject jsonObject =JSONObject.parseObject(back);//String转JSONObject,
        Integer re=jsonObject.getInteger("errcode");
        return re;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值