飞书推送文件给指定用户

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



    }
相关推荐
gf132111118 小时前
飞书查询记录指令的用法
飞书
gf13211111 天前
python_更新飞书多维表格的单项关联字段
数据库·python·飞书
情绪总是阴雨天~1 天前
OpenClaw 中文版从零到一:安装、配置飞书机器人与实战全指南
机器人·飞书
Michaelwubo2 天前
OpenClaw(Clawdbot)附飞书对接教程
飞书
熊文豪2 天前
ToDesk vs 飞书/企微/Zoom:多人同时控制一台电脑,谁更好用?
飞书·企业微信
GalenZhang8884 天前
OpenClaw 配置多个飞书账号实战指南
前端·chrome·飞书·openclaw
郭龙飞9805 天前
OpenClaw 飞书机器人搭建指南 远程 AI 操控电脑配置
人工智能·windows·机器人·飞书
AC赳赳老秦6 天前
OpenClaw与飞书多维表格联动:自动同步工作数据、生成统计图表,实现高效管理
java·数据库·python·信息可视化·飞书·deepseek·openclaw
2601_955781986 天前
飞书远程控机:OpenClaw+AI机器人配置全攻略
人工智能·机器人·飞书·open claw部署
Teable任意门互动7 天前
AI原生开源多维表格有哪些?主流开源多维表格对比解析
数据库·开源·excel·钉钉·飞书·开源软件·ai-native