飞书机器人发文本消息

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);
        }
    }
相关推荐
2601_949816582 小时前
Spring Boot--@PathVariable、@RequestParam、@RequestBody
java·spring boot·后端
freewlt2 小时前
前端工程化性能监控体系建设:从0到1实战指南
前端
Mintopia2 小时前
别再一上来就分层:新手最容易做错的系统设计决定
前端
沐风。562 小时前
python
java·服务器·python
Csvn2 小时前
CDN 与缓存策略
前端
zmsofts2 小时前
java面试必问14:MySQL 索引类型:从基础到优化,面试官给你点赞
java·mysql·面试
Mintopia2 小时前
不用死磕高并发,也能扛住流量:简单实用的系统设计思路
前端
helx822 小时前
spring-ai 下载不了依赖spring-ai-openai-spring-boot-starter
java
SimonKing2 小时前
轻量级富文本编辑器Quill,保姆级教程,5分钟快速上手
java·后端·程序员