📧oa消息通知
大约 4 分钟
OA支持
- 飞书
- Text
- 钉钉
- Text
- Link
- Markdown
- 企业微信
- Text
- Markdown
- News
安装
maven引入
<dependency>
<groupId>org.dromara.sms4j</groupId>
<artifactId>sms4j-oa-core</artifactId>
<version> 最新版本跟随sms4j </version>
</dependency>
方式一 : 手动注册配置
参数(例如:dingding)
//***********************DingTalk-Test************************//
/**
* 填测试手机号
*/
private static final String DingTalkPHONE = "";
/**
* 填access_token
*/
private static final String DingTalkTOKENID = "";
/**
* 填secret
*/
private static final String DingTalkSIGN = "";
maven普通项目调用方式
public static void main(String[] args) {
// 需要手动注册工厂和线程池
// 这里是DingTalkFactory举例
ProviderFactoryHolder.registerFactory(DingTalkFactory.instance());
OaConfig oaConfig = new OaConfig();
oaConfig.setCorePoolSize(20);
oaConfig.setQueueCapacity(20);
oaConfig.setMaxPoolSize(20);
OaBeanFactory.setExecutor(oaConfig);
String key = "oaDingTalk";
DingTalkConfig dingTalkConfig = new DingTalkConfig();
dingTalkConfig.setConfigId(key);
dingTalkConfig.setSign(DingTalkSIGN);
dingTalkConfig.setTokenId(DingTalkTOKENID);
// 根据配置创建服务实例并注册
OaFactory.createAndRegisterOaSender(dingTalkConfig);
OaSender alarm = OaFactory.getSmsOaBlend(key);
Request request = new Request();
ArrayList<String> phones = new ArrayList<>();
phones.add(DingTalkPHONE);
// 支持通过手机号@
request.setPhoneList(phones);
// 支持@all
// request.setIsNoticeAll(true);
request.setContent("测试消息");
alarm.sender(request, MessageType.DING_TALK_TEXT);
}
springboot调用方式(例如:DingTalk)
Text
/**
* DingTalk的Text测试
*/
@Test
public void oaDingTalkText() {
String key = "oaDingTalk";
DingTalkConfig dingTalkConfig = new DingTalkConfig();
dingTalkConfig.setConfigId(key);
dingTalkConfig.setSign(DingTalkSIGN);
dingTalkConfig.setTokenId(DingTalkTOKENID);
// 根据配置创建服务实例并注册
OaFactory.createAndRegisterOaSender(dingTalkConfig);
OaSender alarm = OaFactory.getSmsOaBlend(key);
Request request = new Request();
ArrayList<String> phones = new ArrayList<>();
phones.add(DingTalkPHONE);
// 支持通过手机号@
request.setPhoneList(phones);
// 支持@all
// request.setIsNoticeAll(true);
request.setContent("测试消息");
alarm.sender(request, MessageType.DING_TALK_TEXT);
}
Markdown
/**
* DingTalk的Markdown测试
*/
@Test
public void oaDingTalkMarkdown() {
String key = "oaDingTalk";
DingTalkConfig dingTalkConfig = new DingTalkConfig();
dingTalkConfig.setConfigId(key);
dingTalkConfig.setSign(DingTalkSIGN);
dingTalkConfig.setTokenId(DingTalkTOKENID);
// 根据配置创建服务实例并注册
OaFactory.createAndRegisterOaSender(dingTalkConfig);
OaSender alarm = OaFactory.getSmsOaBlend(key);
Request request = new Request();
// 支持@all
request.setIsNoticeAll(true);
request.setContent("#### 杭州天气 @150XXXXXXXX \n > 9度,西北风1级,空气良89,相对温度73%\n > ![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png)\n > ###### 10点20分发布 [天气](https://www.dingtalk.com) \n");
request.setTitle("标题");
alarm.sender(request, MessageType.DING_TALK_MARKDOWN);
}
Link
/**
* DingTalk的Link测试
*/
@Test
public void oaDingTalkLink() {
String key = "oaDingTalk";
DingTalkConfig dingTalkConfig = new DingTalkConfig();
dingTalkConfig.setConfigId(key);
dingTalkConfig.setSign(DingTalkSIGN);
dingTalkConfig.setTokenId(DingTalkTOKENID);
// 根据配置创建服务实例并注册
OaFactory.createAndRegisterOaSender(dingTalkConfig);
OaSender alarm = OaFactory.getSmsOaBlend(key);
Request request = new Request();
request.setContent("这个即将发布的新版本,创始人xx称它为红树林。而在此之前,每当面临重大升级,产品经理们都会取一个应景的代号,这一次,为什么是红树林");
request.setTitle("点击跳转到钉钉");
request.setMessageUrl("https://www.dingtalk.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI");
request.setPicUrl("https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png");
alarm.sender(request, MessageType.DING_TALK_LINK);
}
⚠️ 注意: feishu、企业微信等和上面配置相同就不过多赘诉了。
优先级消息
/**
* 异步优先级尽可能优先级高的消息先发送,但是获取响应会受网络影响
*/
@Test
public void oaDingTalkAsyncByPriority() {
String key = "oaDingTalk";
DingTalkConfig dingTalkConfig = new DingTalkConfig();
dingTalkConfig.setConfigId(key);
dingTalkConfig.setSign(DingTalkSIGN);
dingTalkConfig.setTokenId(DingTalkTOKENID);
// 根据配置创建服务实例并注册
OaFactory.createAndRegisterOaSender(dingTalkConfig);
OaSender alarm = OaFactory.getSmsOaBlend(key);
CountDownLatch user = new CountDownLatch(1);
// 模拟10条不同优先级的消息
for (int i = 0; i < 3; i++) {
Random random = new Random();
new Thread(() -> {
int priority = random.nextInt(10);
try {
// 等待十个请求
System.out.println(priority + "等待");
user.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Request request = new Request();
ArrayList<String> phones = new ArrayList<>();
phones.add(DingTalkPHONE);
request.setPhoneList(phones);
request.setIsNoticeAll(false);
request.setPriority(priority);
//测试-1-TEXT
request.setContent("该消息优先级为" + priority);
// 执行异步消息
alarm.senderAsyncByPriority(request, MessageType.DING_TALK_TEXT);
System.out.println("优先级为" + priority + "的异步任务已提交");
}).start();
}
System.out.println("开始模拟");
user.countDown();
// 防止主线程挂掉
try {
Thread.sleep(2000L);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
异步消息
举例
// 异步不回调
alarm.senderAsync(request, MessageType.DING_TALK_LINK);
// 异步回调
alarm.senderAsync(request, MessageType.DING_TALK_LINK, smsResponse -> System.out.println("ConfigId为" + smsResponse.getOaConfigId() + "的异步任务发送成功"));
方式二 : yaml配置(springboot项目)
sms-oa:
config-type: yaml
oas:
oaDingTalkByYaml: # configId
isEnable: true # 表示该配置是否生效(默认生效,false表示不生效)
supplier: ding_ding # 厂商标识
tokenId: 您的accessKey # webhook 中的 access_token
sign: 您的sign # 安全设置在验签模式下才的秘钥,非验签模式没有此值
oaByteTalkByYaml: # configId
supplier: byte_talk # 厂商标识
tokenId: 您的accessKey # webhook 中的 token
sign: 您的sign # 安全设置在签名校验模式下才的秘钥,非验签模式没有此值
oaWeTalkByYaml:
supplier: we_talk # 厂商标识
tokenId: 您的sign # webhook 中的 key
core-pool-size: 20 # 核型线程数量
queue-capacity: 20 # 队列容量
max-pool-size: 20 # 最大线程池数量
@Test
public void oaDingTalkByYamlTest() {
String configId = "oaDingTalkByYaml";
OaSender alarm = OaFactory.getSmsOaBlend(configId);
Request request = new Request();
ArrayList<String> phones = new ArrayList<>();
phones.add(DingTalkPHONE);
// 支持通过手机号@
request.setPhoneList(phones);
// 支持@all
// request.setIsNoticeAll(true);
request.setContent("测试消息");
alarm.sender(request, MessageType.DING_TALK_TEXT);
}
特殊消息
WeTalk
news
@Test
public void oaWeTalkNews() {
String key = "oaWeTalk";
WeTalkConfig WeTalkConfig = new WeTalkConfig();
WeTalkConfig.setConfigId(key);
WeTalkConfig.setTokenId(WeTalkTOKENID);
// 根据配置创建服务实例并注册
OaFactory.createAndRegisterOaSender(WeTalkConfig);
OaSender alarm = OaFactory.getSmsOaBlend(key);
Request request = new Request();
ArrayList<WeTalkRequestArticle> articles = new ArrayList<>();
articles.add(new WeTalkRequestArticle("中秋节礼品领取", "今年中秋节公司有豪礼相送", "www.qq.com", "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png"));
request.setArticleList(articles);
alarm.sender(request, MessageType.WE_TALK_NEWS);
}