当前位置: 首页 > news >正文

钉钉 Stream 模式SpringBoot接入配置与事件监听

Maven依赖:
        <!-- 钉钉Stream模块 用于推送和订阅消息 --><dependency><groupId>com.dingtalk.open</groupId><artifactId>app-stream-client</artifactId><version>1.3.6</version></dependency>
配置类:
package com.gkl.attendance.config;import com.dingtalk.open.app.api.GenericEventListener;
import com.dingtalk.open.app.api.OpenDingTalkClient;
import com.dingtalk.open.app.api.OpenDingTalkStreamClientBuilder;
import com.dingtalk.open.app.api.security.AuthClientCredential;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** 钉钉 Stream 模式客户端配置* 负责建立与钉钉的长连接,用于接收推送事件** @author cookie* @date 2025-08-22*/
@Slf4j
@Configuration
public class DingTalkStreamConfig {@Value("${dingtalk.appKey}")private String appKey;@Value("${dingtalk.appSecret}")private String appSecret;@Bean(destroyMethod = "stop")public OpenDingTalkClient dingTalkStreamClient(GenericEventListener eventListener) throws Exception {try {OpenDingTalkClient client = OpenDingTalkStreamClientBuilder.custom().credential(new AuthClientCredential(appKey, appSecret)).registerAllEventListener(eventListener).build();client.start();log.info("钉钉 Stream 客户端启动成功...");return client;} catch (Exception e) {log.error("钉钉 Stream 客户端启动失败", e);throw e;}}
}
监听器
package com.gkl.attendance.listener;import com.dingtalk.open.app.api.GenericEventListener;
import com.dingtalk.open.app.api.message.GenericOpenDingTalkEvent;
import com.dingtalk.open.app.stream.protocol.event.EventAckStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import shade.com.alibaba.fastjson2.JSONObject;/*** 钉钉事件监听器* 接收钉钉推送的事件消息** @author cookie* @date 2025-08-22*/
@Slf4j
@Component
public class DingTalkEventListener implements GenericEventListener {@Overridepublic EventAckStatus onEvent(GenericOpenDingTalkEvent event) {try {String eventId = event.getEventId();String eventType = event.getEventType();Long bornTime = event.getEventBornTime();JSONObject bizData = event.getData();log.info("接收到钉钉事件: eventId={}, type={}, bornTime={}, data={}",eventId, eventType, bornTime, bizData.toJSONString());// TODO: 根据不同事件类型做分发处理switch (eventType) {case "attendance_check_record":log.info("处理考勤事件...");break;case "user_add_org":log.info("处理新成员加入事件...");break;default:log.info("未处理的事件类型: {}", eventType);break;}return EventAckStatus.SUCCESS;} catch (Exception e) {log.error("处理钉钉事件失败", e);// 告诉钉钉稍后重试return EventAckStatus.LATER;}}
}
http://www.dtcms.com/a/344970.html

相关文章:

  • Maxscript如何清理3dMax场景?
  • react样式问题
  • git旧仓库迁移到新仓库
  • [系统架构设计师]安全架构设计理论与实践(十八)
  • Web3与AI语境下的审美积累:HAQQ品牌识别解析
  • 多人编程新方式:cpolar 让 OpenHands 远程开发更轻松
  • 区块链技术原理(17)-以太坊网络
  • SpringBoot中的条件注解
  • 常用三角函数公式推导体系
  • LLM应用场景能力边界趋势全览
  • 从系统修复到硬件检测的技术实测
  • [antv-x6] 文档链接
  • 08高级语言逻辑结构到汇编语言之逻辑结构转换 continue break 完结汇编按逻辑结构
  • RCE的CTF题目环境和做题复现第4集
  • 驱动(二)系统移植
  • 根据webpack设计原理手写一个简版webpack
  • 亚马逊广告优化新逻辑:从人工苦力到AI智能的进化之路
  • K8S的部署与常用管理
  • http请求有哪些?
  • 文件相关操作的函数和文件操作
  • 使用tensorRT8部署yolov5目标检测模型(1)
  • 深入解析 Docker 镜像构建与性能优化实践指南
  • SSM从入门到实战: 2.6 MyBatis缓存机制与性能优化
  • 双发 ARP 测试与实践:从原理到生产验证
  • PHP 函数的参数顺序,它们是随机的吗?
  • 数学建模论文注意点
  • 华盛顿大学GeoAI本土化实践:五大实验贯穿预测、检测、生成、推理与偏差审视
  • 碧海琴魂,孤独与纯粹的永恒绝唱——《海上钢琴师》鉴赏
  • 双摄工业相机镜头切换与同步曝光技术方案
  • 11,FreeRTOS延时函数