网站后台安全性,陕西省安康市建行 网站,思明区建设局官网站,做网站一般字号要做多少Java实现 钉钉群聊机器人定时发送消息功能 钉钉群聊准备工作钉钉发起群聊创建项目群打开钉钉群聊设置打开机器人管理选择Webhook机器人添加机器人安全设置保存Webhook地址#xff08;重点是token#xff09; 项目代码实现添加依赖启动类添加定时任务启动扫描编写调度任务定义… Java实现 钉钉群聊机器人定时发送消息功能 钉钉群聊准备工作钉钉发起群聊创建项目群打开钉钉群聊设置打开机器人管理选择Webhook机器人添加机器人安全设置保存Webhook地址重点是token 项目代码实现添加依赖启动类添加定时任务启动扫描编写调度任务定义接口实现接口 机器人作为一种独立的应用能力在钉钉中扮演着重要角色。只需进行简单的设置机器人就能够在单聊场景或群聊场景中发送消息通知或者提供与用户的交互式服务。利用机器人可以有效地将业务信息和任务融入钉钉的聊天环境中从而加速工作流程和团队协作。 钉钉群聊准备工作
钉钉发起群聊
打开钉钉发起群聊
创建项目群 打开钉钉群聊设置 打开机器人管理
选择添加机器人
选择Webhook机器人
自定义-通过Webhook接入自定义服务
添加机器人
设置机器人名字
安全设置
此处有三个选项可以参考钉钉的说明文档我选择的是自定义关键字 触发自定义关键字需要在推送text内容中包含有自己设置的关键词才能发送成功完成后点击保存
保存Webhook地址重点是token
请求地址是固定的https://oapi.dingtalk.com/robot/send 这里着重记录access_token这是每个机器人唯一的 到这里钉钉群聊准备工作就完成了
项目代码实现
因为我们的功能需求是需要每天定时将项目前一天的核心信息汇总通过群聊机器人发送到钉钉群聊当中所以这里主要通过两个工具来辅助实现的
1.SpringBoot框架自带的调度任务 2.钉钉阿里提供的sdk工具包
添加依赖
pom.xml 文件添加以下依赖项 !--钉钉消息通知相关--dependencygroupIdcom.aliyun/groupIdartifactIdalibaba-dingtalk-service-sdk/artifactIdversion2.0.0/version/dependency启动类添加定时任务启动扫描
需要在主应用程序类或配置类上添加 EnableScheduling 注解以启用调度功能。 这块内容在之前的文章中有 【SpringBoot 调度任务】挺简单的
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;SpringBootApplication
Slf4j
EnableScheduling
public class BizApplication {public static void main(String[] args) {log.info(start to run...);SpringApplication.run(BizApplication.class, args);log.info(started finish);}
}编写调度任务
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;/*** 订单调度器** author: 高克莱* date: 2024/12/18*/
Slf4j
Component
public class InfoScheduler {/*** 加锁等待时间秒*/public static final long LOCK_WAIT_TIME 0L;/*** 加锁超时时间秒*/public static final long LOCK_LEASE_TIME 60L;Autowiredprivate RedisLock redisLock;Autowiredprivate DingtalkClient dingtalkClient;/*** 每天上午9点信息汇总*/Scheduled(cron 0 0 9 * * ?)public void infoSummary(){RLock lock null;try {// 加锁lock redisLock.getLock(Scheduler:infoSummary);lock.tryLock(LOCK_WAIT_TIME, LOCK_LEASE_TIME, TimeUnit.SECONDS);// 加锁失败if (!lock.isLocked()) {// 不处理return;}//根据自己的业务需求整理好信息内容StringBuilder msgContent new StringBuilder(); //机器人发送信息dingtalkClient.sendInfoSms(msgContent.toString());}catch (Throwable e){log.error(RefundScheduler infoSummaryerror, e);} finally {redisLock.releaseLock(lock);}}}
定义接口
/*** 钉钉群聊机器人消息通知客户端** author: 高克莱* date: 2024/12/18*/
public interface DingtalkClient {/*** 钉钉群聊机器人发送每日信息* param msgContent*/void senInfoSms(String msgContent);
}实现接口
钉钉功能实现部分可以直接打开【钉钉开放平台】搜索 “自定义机器人发送群聊消息”
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import com.sunrise.integration.client.DingtalkClient;
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;/*** 钉钉群聊机器人消息通知客户端实现** author: 高克莱* date: 2024/12/18*/
Component
public class DingtalkClientIpml implements DingtalkClient {//webhook.url 为 https://oapi.dingtalk.com/robot/sendValue(${dingtalk.webhook.url})private String WEBHOOK_URL;//webhook.access_token 为 自己保存的access_token前面步骤提过钉钉群聊准备工作-保存Webhook地址重点是tokenValue(${dingtalk.webhook.access_token})private String CUSTOM_ROBOT_TOKEN;private DingTalkClient client;Overridepublic void senInfoSms(String msgContent) {try {client new DefaultDingTalkClient(WEBHOOK_URL);OapiRobotSendRequest req new OapiRobotSendRequest();//定义文本内容OapiRobotSendRequest.Text text new OapiRobotSendRequest.Text();msgContent 信息通知\n msgContent;text.setContent(msgContent);//设置消息类型 文本消息req.setMsgtype(text);req.setText(text);//定义 对象 群聊机器人可以群成员
// OapiRobotSendRequest.At at new OapiRobotSendRequest.At();
// at.setAtUserIds(Arrays.asList(xxxxxxx));//at指定用户的userIduserId需要在钉钉后台管理中查询
// at.setIsAtAll(true);//at所有人
// req.setAt(at);OapiRobotSendResponse rsp client.execute(req, CUSTOM_ROBOT_TOKEN);} catch (ApiException e) {e.printStackTrace();throw new RuntimeException();}}
} 调用频率限制 由于消息发送太频繁会严重影响群的使用体验因此自定义机器人发送消息的频率限制如下 每个机器人每分钟最多发送20条消息到群里如果超过20条会限流10分钟 效果