飞书推送文件给指定用户

首先要先把文件上传到飞书服务器,获取文件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);
        }



    }
相关推荐
刘广睿18 天前
影栈 vs 飞书/Notion:用通用工具搭短视频素材库,半年踩坑记
飞书·notion·短视频素材库·素材管理效率·创作工作流
13线20 天前
为什么要合并飞书和豆包
人工智能·飞书·火山引擎·豆包
倔强的小石头_21 天前
把 Mac mini 变成远程 AI 工作台:OpenClaw + 飞书 + cpolar 实战
飞书·mac·openclaw
gf132111123 天前
【python_发送飞书卡片消息_直接组装JSON格式发送卡片】
python·json·飞书
shandianchengzi24 天前
【工具】飞书多维表格插件|附件批量导出并自定义名称的免费插件
飞书
kongxinniaoren24 天前
已经开学了,大学生还能做近视手术吗?有哪些需要注意的?
飞书
我是Superman丶25 天前
飞书文档下载
飞书
云雀衔光1 个月前
多个 MCP Server 怎么编排:数据库 / Redis / Git / 飞书一把梭
java·数据库·人工智能·redis·git·语言模型·飞书
RestCloud1 个月前
飞书多维表格落地新思路:ETL做数据导入,豆包做业务智能分析
数据仓库·飞书·etl·数据传输·api对接·豆包
PM老周1 个月前
团队知识库软件哪个好用?ONES、Confluence、Notion、飞书、语雀等主流工具对比
飞书·confluence·ones·知识库管理