HttpClient实现调用飞书"更新应用发送的消息"接口和获取tenant_access_token接口的实现,实现更新飞书消息卡片
本篇内容主要是更新飞书消息卡片时参考。
@PostMapping("/feishu")
public JSONObject feishu(@RequestBody JSONObject jsonParam) {
Object challenge = jsonParam.get("challenge");
if(challenge != null && challenge != ""){
JSONObject challengeObject=new JSONObject();
challengeObject.put("challenge",challenge);
return challengeObject;
}else{
JSONObject resultBody = new JSONObject();
JSONObject card = new JSONObject();
JSONObject header = new JSONObject();
card.put("header", header);
JSONObject titleJO = new JSONObject();
header.put("title", titleJO);
titleJO.put("tag", "plain_text");
titleJO.put("content", "测试回馈信息");
JSONArray elements = new JSONArray();
card.put("elements", elements);
JSONObject action = new JSONObject();
elements.add(action);
action.put("tag", "action");
JSONArray actions = new JSONArray();
action.put("actions", actions);
//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓消息卡片中增加同意和驳回按钮↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
JSONObject approveButtton = new JSONObject();
actions.add(approveButtton);
approveButtton.put("tag", "button");
JSONObject approveButttonText = new JSONObject();
approveButtton.put("text", approveButttonText);
approveButttonText.put("tag", "plain_text");
approveButttonText.put("content", "同意");
approveButtton.put("type","approve");
JSONObject approveVal = new JSONObject();
approveButtton.put("value",approveVal);
approveVal.put("chosen","approve");
JSONObject declineButtton = new JSONObject();
actions.add(declineButtton);
declineButtton.put("tag", "button");
JSONObject declineButttonText = new JSONObject();
declineButtton.put("text", declineButttonText);
declineButttonText.put("tag", "plain_text");
declineButttonText.put("content", "驳回");
declineButtton.put("type","approve");
JSONObject declineVal = new JSONObject();
declineButtton.put("value",declineVal);
declineVal.put("chosen","approve");
//交互元素布局,窄版样式默认纵向排列
//使用 bisected 为二等分布局,每行两列交互元素
//使用 trisection 为三等分布局,每行三列交互元素
//使用 flow 为流式布局元素会按自身大小横向排列并在空间不够的时候折行
action.put("layout","flow");
//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑消息卡片中增加同意和驳回按钮↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
// resultBody.putAll(card);
String msgContent="{\n" +
" \"config\": {\n" +
" \"wide_screen_mode\": true\n" +
" },\n" +
" \"elements\": [\n" +
" {\n" +
" \"fields\": [\n" +
" {\n" +
" \"is_short\": true,\n" +
" \"text\": {\n" +
" \"content\": \"**申请人:**\\n孙宝龙\",\n" +
" \"tag\": \"lark_md\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"is_short\": true,\n" +
" \"text\": {\n" +
" \"content\": \"**休假类型:**\\n年假\",\n" +
" \"tag\": \"lark_md\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"is_short\": false,\n" +
" \"text\": {\n" +
" \"content\": \"\",\n" +
" \"tag\": \"lark_md\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"is_short\": true,\n" +
" \"text\": {\n" +
" \"content\": \"**时间:**\\n2020-4-8 至 2020-4-10(共3天)\",\n" +
" \"tag\": \"lark_md\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"is_short\": true,\n" +
" \"text\": {\n" +
" \"content\": \"**备注:**\\n因家中有急事,需往返老家,故请假\",\n" +
" \"tag\": \"lark_md\"\n" +
" }\n" +
" }\n" +
" ],\n" +
" \"tag\": \"div\"\n" +
" },\n" +
" {\n" +
" \"tag\": \"hr\"\n" +
" },\n" +
" {\n" +
" \"actions\": [\n" +
" {\n" +
" \"tag\": \"button\",\n" +
" \"text\": {\n" +
" \"content\": \"批准\",\n" +
" \"tag\": \"plain_text\"\n" +
" },\n" +
" \"type\": \"primary\",\n" +
" \"value\": {\n" +
" \"chosen\": \"approve\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"tag\": \"button\",\n" +
" \"text\": {\n" +
" \"content\": \"拒绝\",\n" +
" \"tag\": \"plain_text\"\n" +
" },\n" +
" \"type\": \"danger\",\n" +
" \"value\": {\n" +
" \"chosen\": \"decline\"\n" +
" }\n" +
" }\n" +
" ],\n" +
" \"layout\": \"bisected\",\n" +
" \"tag\": \"action\"\n" +
" }\n" +
" ],\n" +
" \"header\": {\n" +
" \"template\": \"blue\",\n" +
" \"title\": {\n" +
" \"content\": \"你有一个休假申请待审批\",\n" +
" \"tag\": \"plain_text\"\n" +
" }\n" +
" }\n" +
"}";
JSONObject jsonObject = JSONObject.parseObject(msgContent);
// resultBody.putAll(jsonObject);
// resultBody.putAll(card);
//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓获取tenant_access_token↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
CloseableHttpClient getTenantTokenHttpClient = HttpClients.createDefault();
HttpPost getTenantTokenHttpPost = new HttpPost("https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal");
Header[] getTenantTokenheaders = {
new BasicHeader("Content-Type","application/json; charset=utf-8")
};
getTenantTokenHttpPost.setHeaders(getTenantTokenheaders);
JSONObject getTenantTokenBody = new JSONObject();
getTenantTokenBody.put("app_id","飞书应用中的app_id值");
getTenantTokenBody.put("app_secret","飞书应用中的app_secret值");
String jsonPrarms = getTenantTokenBody.toJSONString();
HttpEntity entityParam = new StringEntity(jsonPrarms, ContentType.create("application/json", "UTF-8")); //这里的“application/json” 可以更换因为本人是传的json参数所以用的这个
getTenantTokenHttpPost.setEntity(entityParam);
String tenant_access_token = null;
try {
CloseableHttpResponse tenantToken = getTenantTokenHttpClient.execute(getTenantTokenHttpPost);
HttpEntity httpEntity = tenantToken.getEntity();
/**
* 推荐消耗实体内容的方式是使用它的 getContent()方法或 writeTo(OutputStream)方法,除非
* 响应实体源自可靠的HTTP服务器和已经长度限制。
*/
InputStream inputStream = httpEntity.getContent();
//<1>创建字节数组输出流,用来输出读取到的内容
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//<2>创建缓存大小
byte[] buffer = new byte[1024]; // 1KB
//每次读取到内容的长度
int len = -1;
//<3>开始读取输入流中的内容
while ((len = inputStream.read(buffer)) != -1) { //当等于-1说明没有数据可以读取了
baos.write(buffer, 0, len); //把读取到的内容写到输出流中
}
//<4> 把字节数组转换为字符串
String content = baos.toString();
//<5>关闭输入流和输出流
inputStream.close();
baos.close();
JSONObject result = JSONObject.parseObject(content);
tenant_access_token = result.getString("tenant_access_token");
System.out.println("111");
} catch (IOException e) {
e.printStackTrace();
}
//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑获取tenant_access_token↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓更新应用发送的消息↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
String open_message_id = jsonParam.getString("open_message_id");
// String token = jsonParam.getString("token");
String token = tenant_access_token;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPatch httpPatch = new HttpPatch();
String URL1= "https://open.feishu.cn/open-apis/im/v1/messages/"+open_message_id;
try {
URI uri = new URI(URL1);
httpPatch.setURI(uri);
} catch (URISyntaxException e) {
e.printStackTrace();
}
Header[] headers = {
new BasicHeader("Authorization", "Bearer "+token),
new BasicHeader("Content-Type","application/json; charset=utf-8")
};
httpPatch.setHeaders(headers);
List list = new LinkedList<>();
BasicNameValuePair param1 = new BasicNameValuePair("content", msgContent);
list.add(param1);
JSONObject body = new JSONObject();
body.put("content",msgContent);
String jsonPrarmBody = body.toJSONString();
HttpEntity entityParamBody = new StringEntity(jsonPrarmBody, ContentType.create("application/json", "UTF-8")); //这里的“application/json” 可以更换因为本人是传的json参数所以用的这个
httpPatch.setEntity(entityParamBody);
try {
CloseableHttpResponse execute = httpClient.execute(httpPatch);
System.out.println("222");
} catch (IOException e) {
e.printStackTrace();
}
//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑更新应用发送的消息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
return resultBody;
}
}
msgContent:就是要更新的消息卡片,可以到飞书的消息卡片搭建工具中调好样式然后直接复制,
消息卡片搭建工具:https://open.feishu.cn/tool/cardbuilder?from=howtoguide