企业微信消息推送

//推送至微信端
        //获取token
        Admin admin = adminService.getCurrent();
        Crop co=cropService.findByAdmin(admin.getId());
        StringBuffer str = new StringBuffer();
        str.append("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=");
        str.append(co.getCropId());
        str.append("&corpsecret=");
        co.setSecret(co.getSecret());
        str.append(co.getSecret());
        String token=null;
        try{
        JSONObject jsontoken = JsonUtils.toJSONObject(HttpUtil.getInvoke(str.toString()));
        int errcode=jsontoken.getInt("errcode");
        if(errcode!=0){
            addFlashMessage(redirectAttributes, Message.error("cropid或secret不正确"));
            return "redirect:list_condition.jhtml?id=" + articleCategoryId;
        }
          token = jsontoken.getString("access_token");
        }catch(JSONException e){
            addFlashMessage(redirectAttributes, Message.error("cropid或secret不正确"));
            return "redirect:list_condition.jhtml?id=" + articleCategoryId;
        }
        //上传临时素材
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
        JSONObject jsonupload=WXUpload.upload(token, "image", basePath + "/resources/admin/images/youlanshan.jpg");
        int errcode1=jsonupload.getInt("errcode");
        if(errcode1!=0){
            addFlashMessage(redirectAttributes, Message.error("上传临时素材失败"));
            return "redirect:list_condition.jhtml?id=" + articleCategoryId;
        }
        String mediaId=jsonupload.getString("media_id");
        //推送内容
        String msgurl="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="
              + token;
        JSONObject jsonre=new JSONObject();
        jsonre.put("touser""wxs-hsd");
        jsonre.put("msgtype""mpnews");
        if(co.getAdmin()==9){
            jsonre.put("agentid", 1000003);
        }else{
            jsonre.put("agentid", 0); 
        }
          
        JSONObject json11=new JSONObject();
        json11.put("title", article.getTitle());
        json11.put("author", article.getAuthor());
        json11.put("thumb_media_id", mediaId);
        json11.put("content",article.getContent());
        JSONArray ja=new JSONArray();
        ja.add(json11);
        JSONObject json1=new JSONObject();
        json1.put("articles", ja);
        jsonre.put("mpnews", json1);
        JSONObject jsonback=JsonUtils.toJSONObject(HttpUtil
                    .post(msgurl,jsonre.toString()));
        System.out.println(jsonback);
        addFlashMessage(redirectAttributes, Message.success("推送成功"));


图文消息(mpnews)(企业微信api)

mpnews类型的图文消息,跟普通的图文消息一致,唯一的差异是图文内容存储在企业微信。
多次发送mpnews,会被认为是不同的图文,阅读、点赞的统计会被分开计算。

请求示例:

 
{
   "touser" : "UserID1|UserID2|UserID3",
   "toparty" : "PartyID1 | PartyID2",
   "totag": "TagID1 | TagID2",
   "msgtype" : "mpnews",
   "agentid" : 1,
   "mpnews" : {
       "articles":[
           {
               "title": "Title", 
               "thumb_media_id": "MEDIA_ID",
               "author": "Author",
               "content_source_url": "URL",
               "content": "Content",
               "digest": "Digest description"
            }
       ]
   },
   "safe":0
}

参数说明:

参数是否必须说明
touser成员ID列表(消息接收者,多个接收者用‘|’分隔,最多支持1000个)。特殊情况:指定为@all,则向关注该企业应用的全部成员发送
toparty部门ID列表,多个接收者用‘|’分隔,最多支持100个。当touser为@all时忽略本参数
totag标签ID列表,多个接收者用‘|’分隔,最多支持100个。当touser为@all时忽略本参数
msgtype消息类型,此时固定为:news
agentid企业应用的id,整型。可在应用的设置页面查看
articles图文消息,一个图文消息支持1到8条图文
title标题,不超过128个字节,超过会自动截断
thumb_media_id图文消息缩略图的media_id, 可以在上传多媒体文件接口中获得。此处thumb_media_id即上传接口返回的media_id
author图文消息的作者,不超过64个字节
content_source_url图文消息点击“阅读原文”之后的页面链接
content图文消息的内容,支持html标签,不超过666 K个字节
digest图文消息的描述,不超过512个字节,超过会自动截断
safe表示是否是保密消息,0表示否,1表示是,默认0
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值