飞书推送文件给指定用户

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



    }
相关推荐
倔强的小石头_19 小时前
把 Mac mini 变成远程 AI 工作台:OpenClaw + 飞书 + cpolar 实战
飞书·mac·openclaw
gf13211113 天前
【python_发送飞书卡片消息_直接组装JSON格式发送卡片】
python·json·飞书
shandianchengzi4 天前
【工具】飞书多维表格插件|附件批量导出并自定义名称的免费插件
飞书
kongxinniaoren4 天前
已经开学了,大学生还能做近视手术吗?有哪些需要注意的?
飞书
我是Superman丶5 天前
飞书文档下载
飞书
云雀衔光7 天前
多个 MCP Server 怎么编排:数据库 / Redis / Git / 飞书一把梭
java·数据库·人工智能·redis·git·语言模型·飞书
RestCloud7 天前
飞书多维表格落地新思路:ETL做数据导入,豆包做业务智能分析
数据仓库·飞书·etl·数据传输·api对接·豆包
PM老周8 天前
团队知识库软件哪个好用?ONES、Confluence、Notion、飞书、语雀等主流工具对比
飞书·confluence·ones·知识库管理
码农阿豪9 天前
我把 OpenClaw 装进旧安卓手机后,又折腾了 3 天:飞书、ADB、SSH 全部打通
android·智能手机·飞书
ValueHD13 天前
融合会议终端部署复杂吗?IT 部门需要做什么?
音视频·飞书·视频编解码·腾讯会议