飞书推送文件给指定用户

首先要先把文件上传到飞书服务器,获取文件key 。然后调用消息发送API进行文件推送

java 复制代码
        // 上传文件
        String fileKey = uploadFileToFeishu();
        // 将文件推送给用户列表
        sendFileToFeishuUserId(fileKey,userList);
java 复制代码
    /**
     *  上传文件到飞书云端
     * @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();
    }
java 复制代码
    /**
     * 发送文件给飞书的指定用户
     * @param fileKey   文件key
     * @param userList  用户列表
     * @throws Exception    异常
     */
    private void sendFileToFeishuUserId(String fileKey, List<String> 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);
        }



    }
相关推荐
皮皮虾❀1 天前
企业协同办公平台深度对比:钉钉、飞书、企微的区别与钉钉服务商典铭云赛推荐
钉钉·飞书·企业微信
杰瑞学AI2 天前
一个回答需要10分钟:飞书问答机器人踩坑实录——纯Agent自由检索,差点让我们的机器人“难产”
人工智能·机器人·prompt·transformer·飞书·ai-native
极客猴子5 天前
AI会议记录工具横评:科会通、通义听悟、飞书妙记的自动整理效果有什么区别?
人工智能·飞书
数聚天成DeepSData6 天前
内网多人在线填表自动汇总系统搭建:飞书钉钉与开源Baserow、NocoDB方案
开源·钉钉·飞书
qq_349447956 天前
OpenClaw连接飞书
飞书
梦想的颜色8 天前
飞书 CLI + Skill 完全硬核指南|让 AI Agent 全自动操控飞书(消息 / 文档 / 日历 / 多维表)
人工智能·飞书·办公自动化·飞书 skill·飞书全自动工作流·agent 工具集成
有点小帅得平哥哥8 天前
基于飞书生态的 AI 智能体
人工智能·飞书
老王IT11 天前
解决飞书占用C盘空间过大的问题
飞书
不爱记笔记13 天前
音视频转笔记工具横评2026,通义听悟、Ai好记、NotebookLM 实测对比
人工智能·笔记·ai·音视频·飞书·obsidian
gtstore15 天前
飞书仓库管理系统技术选型:从手工台账到系统化管理的演进路径
飞书·仓库管理系统·飞书仓库管理·saas选型·飞书仓库管理系统选型