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)
其他参考官方文档: