飞书推送文件给指定用户

张开发
2026/6/8 4:10:33 15 分钟阅读
飞书推送文件给指定用户
首先要先把文件上传到飞书服务器获取文件key。然后调用消息发送API进行文件推送// 上传文件 String fileKey uploadFileToFeishu(); // 将文件推送给用户列表 sendFileToFeishuUserId(fileKey,userList);/** * 上传文件到飞书云端 * return * throws Exception */ private String uploadFileToFeishu() throws Exception { // 构建client Client client Client.newBuilder(xxx, xxxxx).build(); // 创建请求对象 File file new File(user_info.xlsx); //这里填本地文件路径。 CreateFileReq req CreateFileReq.newBuilder() .createFileReqBody(CreateFileReqBody.newBuilder() .fileType(xls) .fileName(文件名称XXX.xlsx) .duration(3000) .file(file) .build()) .build(); // 发起请求 CreateFileResp resp client.im().v1().file().create(req); // 处理服务端错误 if (!resp.success()) { System.out.println(String.format(code:%s,msg:%s,reqId:%s, resp:%s, resp.getCode(), resp.getMsg(), resp.getRequestId(), Jsons.createGSON(true, false).toJson(JsonParser.parseString(new String(resp.getRawResponse().getBody(), StandardCharsets.UTF_8))))); return null; } // 业务数据处理 // System.out.println(Jsons.DEFAULT.toJson(resp.getData())); return resp.getData().getFileKey(); }/** * 发送文件给飞书的指定用户 * param fileKey 文件key * param userList 用户列表 * throws Exception 异常 */ private void sendFileToFeishuUserId(String fileKey, ListString userList) throws Exception { // 构建client Client client Client.newBuilder(xxx, xxx).build(); for(String userId : userList) { // 创建请求对象 CreateMessageReq req CreateMessageReq.newBuilder() .receiveIdType(user_id) .createMessageReqBody(CreateMessageReqBody.newBuilder() .receiveId(userId) .msgType(file) .content({\file_key\:\ fileKey \}) .build()) .build(); // 发起请求 client.im().v1().message().create(req); } }

更多文章