飞书机器人发文本消息

SDK

1、导入依赖

java 复制代码
<dependency>
  <groupId>com.larksuite.oapi</groupId>
  <artifactId>oapi-sdk</artifactId>
  <version>2.5.3</version>
</dependency>

2、开通权限

https://open.feishu.cn/document/faq/trouble-shooting/how-to-fix-the-99991672-error

java 复制代码
/**
     * 发送飞书文本消息
     *
     * @param receiveId     接收者ID(如 open_id、chat_id、email 等)
     * @param receiveIdType ID类型:open_id | chat_id | email | user_id
     * @param text          消息文本内容
     */
    public void sendFeiShuTextMessage(String receiveId, String receiveIdType, String text) {
        try {
            // 1. 构造消息内容 JSON:{"text":"xxx"}
            Map<String, String> content = new HashMap<>();
            content.put("text", text);
            String contentJson = JSONUtil.toJsonStr(content);

            // 2. 构建请求体
            CreateMessageReq req = CreateMessageReq.newBuilder().receiveIdType(receiveIdType)
                    .createMessageReqBody(CreateMessageReqBody.newBuilder()
                            .receiveId(receiveId)
                            .msgType("text")
                            .content(contentJson)
                            .uuid("uuid"))
                            .build())
                    .build();

            // 3. 调用 SDK 发送
            CreateMessageResp resp = feishuClient.im().v1().message().create(req);

            // 4. 处理响应
            if (resp.success()) {
                log.info("飞书文本消息发送成功, messageId: {}", resp.getData().getMessageId());
            } else {
                log.error("飞书消息发送失败 | code: {} | msg: {} | logId: {}",
                        resp.getCode(), resp.getMsg(), resp.getRequestId());
                throw new RuntimeException("飞书消息发送失败: " + resp.getMsg());
            }
        } catch (Exception e) {
            log.error("发送飞书消息异常", e);
            throw new RuntimeException("发送飞书消息异常", e);
        }
    }

webHook

1、新创建个飞书群,添加人员和机器人

2、获得webhook地址

复制代码
https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxx
java 复制代码
 /**
     * 发送飞书文本消息
     *
     * @param text 消息文本内容
     */
    public void sendFeiShuTextMessage(String text) {
        try {
            if (StringUtils.isBlank(feishuWebhookUrl)) {
                log.warn("飞书webhook地址未配置,无法发送消息");
                return;
            }

            // 构建飞书消息payload
            JSONObject payload = new JSONObject();

            // 设置消息类型为文本
            payload.set("msg_type", "text");

            // 构建消息内容
            JSONObject textContent = new JSONObject();
            textContent.set("text", text);
            payload.set("content", textContent);

            log.info("发送飞书消息,webhook地址:{}, 消息内容:{}", feishuWebhookUrl, payload.toStringPretty());

            // 调用飞书webhook接口
            String response = HttpUtil.post(feishuWebhookUrl, payload.toString());
            log.info("飞书消息发送成功,响应结果:{}", response);

            JSONObject responseObj = JSONUtil.parseObj(response);
            Integer code = responseObj.getInt("code");
            if (code != null && code != 0) {
                log.error("飞书消息发送失败,错误代码:{}, 错误信息:{}", code, responseObj.getStr("msg"));
            }
        } catch (Exception e) {
            log.error("发送飞书消息异常,消息内容:{}", text, e);
        }
    }
相关推荐
艾利克斯冰4 分钟前
Java 设计模式-行为型模式(更新中)
java·开发语言·设计模式
倒霉蛋小马17 分钟前
Java新特性:record关键字
java·开发语言
我叫黑大帅28 分钟前
前端如何竖屏固定视口背景
前端·javascript·面试
abcy07121331 分钟前
python pandas csv异步后台清洗前端优先返回成功信息
前端·python·pandas
折哥的程序人生 · 物流技术专研33 分钟前
《Java 100 天进阶之路》第95篇:消息队列基础(RocketMQ/Kafka)(2026版)
java·面试·kafka·rocketmq·java-rocketmq·求职招聘
budingxiaomoli39 分钟前
Spring日志
java·开发语言
IT空门:门主43 分钟前
Spring 注入三剑客:@Resource、@Autowired、@RequiredArgsConstructor 到底该用哪个?
java·后端·spring
IT_陈寒1 小时前
Vite这个坑我帮你踩了,动态导入居然这样才生效
前端·人工智能·后端
swipe1 小时前
Mem0 x Agent 实战系列:分层记忆 + 三路召回,搭建真正可用的长期记忆层
前端·javascript·面试
鹤鸣的日常1 小时前
前端运行时动态环境变量方案
前端·react.js·docker·前端框架·vue·gitlab