1. 程式人生 > 其它 >HttpClient實現呼叫飛書"更新應用傳送的訊息"介面和獲取tenant_access_token介面的實現,實現更新飛書訊息卡片

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<NameValuePair> 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