企业微信推送消息提醒

package cn.iocoder.yudao.module.bpm.service.message.util;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Map;

public class SendMessageToWX {


    public static void main(String[] args) throws JsonProcessingException {
        //post2();
        post3();
    }


    /**
     * 文本消息
     * @throws JsonProcessingException
     */
    public static void post2() throws JsonProcessingException {
//        JSONObject param = JSONUtil.createObj();
//        param.put("StartTime", "2023-06-07 09:42:02");
//        param.put("EndTime", "2023-06-07 09:42:02");
//        param.put("StartIndex", 0);
//        param.put("PageSize", 30);
//        String result = HttpUtil.post("http://localhost:8080/hutool/test", param.toString());
        String corpid="xxxxx";
        String corpsecret="xxxxxxxx";

        String jsonStr = HttpUtil.get("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpid + "&corpsecret=" + corpsecret);
       ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> map =  mapper.readValue(jsonStr,Map.class);
        String newToken = (String) map.get("access_token");

        JSONObject param = JSONUtil.createObj();
        param.put("touser", "@all");//指定用户(向关注该企业应用的全部成员发送)
        param.put("msgtype", "text");//消息类型
        param.put("agentid", "1000053");//pqcp微信小程序id
        param.put("safe", "0");
        JSONObject textparam = JSONUtil.createObj();
        textparam.put("content", "你有待办任务\n<a href=\"http://pqcp.zacmotor.com/process-instance/detail?id=a567e9f4-b98a-11ee-82dc-0e7a15fd3b3a&taskDefinitionKey=Activity_1o27ahr&key=bpm_bir\">详情</a>");
        param.put("text", textparam);
        System.out.println("---------------param--------");
        System.out.println(param);
        String jsonData = mapper.writeValueAsString(param);
        String resultJson =  HttpUtil.post("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + newToken,jsonData);
         Map<String, Object> resultJsonMap = mapper.readValue(resultJson, Map.class);
        Integer errcode = (Integer) resultJsonMap.get("errcode");


        if (errcode != 0) {

            String newjsonStr = HttpUtil.get("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpid + "&corpsecret=" + corpsecret);
            Map<String, Object> newmap =  mapper.readValue(newjsonStr,Map.class);
             newToken = (String) newmap.get("access_token");
             //重新推送
              HttpUtil.post("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + newToken,jsonData);
        }


    }


    /**
     * 卡片消息
     * @throws JsonProcessingException
     */
    public static void post3() throws JsonProcessingException {
//        JSONObject param = JSONUtil.createObj();
//        param.put("StartTime", "2023-06-07 09:42:02");
//        param.put("EndTime", "2023-06-07 09:42:02");
//        param.put("StartIndex", 0);
//        param.put("PageSize", 30);
//        String result = HttpUtil.post("http://localhost:8080/hutool/test", param.toString());
        String corpid="xxxxxxxx";//企业微信id 
        String corpsecret="xxxxxxx";//应用id 

        String jsonStr = HttpUtil.get("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpid + "&corpsecret=" + corpsecret);
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> map =  mapper.readValue(jsonStr,Map.class);
        String newToken = (String) map.get("access_token");

        JSONObject param = JSONUtil.createObj();
        param.put("touser", "20948");//指定用户(向关注该企业应用的全部成员发送)
        param.put("msgtype", "textcard");//消息类型
        param.put("agentid", "1000053");//pqcp微信小程序id
        param.put("safe", "0");
        JSONObject textcardparam = JSONUtil.createObj();
        textcardparam.put("title","你有待办任务");
        textcardparam.put("description","\n<div class=\"gray\">2024年1月31日</div> <div class=\"normal\">恭喜你抽中iPhone 7一台,领奖码:xxxx</div><div class=\"highlight\">请于2016年10月10日前联系行政同事领取</div>");
        textcardparam.put("url","http://pqcp.zacmotor.com/process-instance/detail?id=a567e9f4-b98a-11ee-82dc-0e7a15fd3b3a&taskDefinitionKey=Activity_1o27ahr&key=bpm_bi");
       // textcardparam.put("url","https://img1.baidu.com/it/u=1699929707,733321099&fm=253&fmt=auto&app=120&f=JPEG?w=1422&h=800");
        textcardparam.put("btntxt","更多");
        param.put("textcard", textcardparam);
        System.out.println("---------------param--------");
        System.out.println(param);
        String jsonData = mapper.writeValueAsString(param);
        String resultJson =  HttpUtil.post("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + newToken,jsonData);
        Map<String, Object> resultJsonMap = mapper.readValue(resultJson, Map.class);
        Integer errcode = (Integer) resultJsonMap.get("errcode");


        if (errcode != 0) {

            String newjsonStr = HttpUtil.get("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpid + "&corpsecret=" + corpsecret);
            Map<String, Object> newmap =  mapper.readValue(newjsonStr,Map.class);
            newToken = (String) newmap.get("access_token");
            //重新推送
            HttpUtil.post("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + newToken,jsonData);
        }


    }


}

corpid:企业ID

corpsecret:(企业微信创建应用Secret)

其他参考官方文档:

发送应用消息 - 接口文档 - 企业微信开发者中心

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值