fix:pom版本控制
This commit is contained in:
48
xservice-message-starter/pom.xml
Normal file
48
xservice-message-starter/pom.xml
Normal file
@@ -0,0 +1,48 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.xiang</groupId>
|
||||
<artifactId>xservice-basic</artifactId>
|
||||
<version>1.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>xservice-message-starter</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.xiang</groupId>
|
||||
<artifactId>xservice-common</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 钉钉jar包 -->
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>alibaba-dingtalk-service-sdk</artifactId>
|
||||
<version>2.0.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- aliyun dns解析 -->
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>alidns20150109</artifactId>
|
||||
<version>3.4.7</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
<version>1.11</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.xiang.xservice.basic.xservice.dingTalk.enums;
|
||||
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public enum DingTalkUrlEnum {
|
||||
/**
|
||||
* 钉钉接口枚举
|
||||
*/
|
||||
DING_TALK_GET_ENTERPRISE_INTER_TOKEN("https://oapi.dingtalk.com/gettoken", "获取企业内部应用Token"),
|
||||
DING_TALK_ASYNC_SEND_MESSAGE("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2", "异步发送工作通知"),
|
||||
DING_TALK_CHAR_MESSAGE("https://oapi.dingtalk.com/chat/send", "发送消息到企业群旧版SDK"),
|
||||
;
|
||||
|
||||
|
||||
|
||||
final String url;
|
||||
|
||||
final String desc;
|
||||
|
||||
DingTalkUrlEnum(String url, String desc) {
|
||||
this.url = url;
|
||||
this.desc = desc;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
package com.xiang.xservice.basic.xservice.dingTalk.service;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.dingtalk.api.DefaultDingTalkClient;
|
||||
import com.dingtalk.api.DingTalkClient;
|
||||
import com.dingtalk.api.request.OapiChatSendRequest;
|
||||
import com.dingtalk.api.request.OapiGettokenRequest;
|
||||
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
|
||||
import com.dingtalk.api.request.OapiRobotSendRequest;
|
||||
import com.dingtalk.api.response.OapiChatSendResponse;
|
||||
import com.dingtalk.api.response.OapiGettokenResponse;
|
||||
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
|
||||
import com.dingtalk.api.response.OapiRobotSendResponse;
|
||||
import com.xiang.xservice.basic.xservice.dingTalk.enums.DingTalkUrlEnum;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class DingTalkService {
|
||||
|
||||
/**
|
||||
* 应用凭证id
|
||||
* https://open-dev.dingtalk.com/fe/ai?hash=%23%2Fapp%2F3fa4c9a7-27f2-4d6f-bbe7-41d2a17b5c11%2Fbaseinfo#/app/3fa4c9a7-27f2-4d6f-bbe7-41d2a17b5c11/baseinfo
|
||||
*/
|
||||
private static final String APP_ID = "3fa4c9a7-27f2-4d6f-bbe7-41d2a17b5c11";
|
||||
|
||||
/**
|
||||
* 原企业内部应用AgentID
|
||||
*/
|
||||
private static final String AGENT_ID = "3829551658";
|
||||
|
||||
/**
|
||||
* 组织ID
|
||||
*/
|
||||
private static final String CORP_ID = "dingf2c4425cd179a26ef2c783f7214b6d69";
|
||||
|
||||
private static final String CLIENT_ID = "dingcc9fikz1c0e5wb9v";
|
||||
|
||||
private static final String CLIENT_SECRET = "wyapsH6y8P1K_wuTPKGKwG0mquj1uth9Dxn6HcRpta3sh8Syukl0C8nOmR1PeBzs";
|
||||
|
||||
private static final String GRANT_TYPE = "client_credentials";
|
||||
|
||||
private static final String USER_ID = "450841600726084717";
|
||||
|
||||
private static final String MSG_TYPE = "text";
|
||||
|
||||
/**
|
||||
* 自定义机器人token
|
||||
*/
|
||||
private static final String CUSTOM_ROBOT_TOKEN = "4709b708d961846e0aee523c5abc3b67e8fa424ee292501d85efd4e504f15a8b";
|
||||
|
||||
/**
|
||||
* 密钥
|
||||
*/
|
||||
private static final String SECRET = "SEC768ed578c0fb31a9aec84b1c1db4f195f5aca203985bbb9d549e23e41c8874d1";
|
||||
|
||||
/**
|
||||
* 发送机器人消息
|
||||
* @param msg
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public String sendRobotMessage(String msg) throws Exception {
|
||||
Long timestamp = System.currentTimeMillis();
|
||||
String stringToSign = timestamp + "\n" + SECRET;
|
||||
Mac mac = Mac.getInstance("HmacSHA256");
|
||||
mac.init(new SecretKeySpec(SECRET.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
|
||||
byte[] signData = mac.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8));
|
||||
String sign = URLEncoder.encode(Base64.getEncoder().encodeToString(signData), StandardCharsets.UTF_8);
|
||||
|
||||
//sign字段和timestamp字段必须拼接到请求URL上,否则会出现 310000 的错误信息
|
||||
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send?sign=" + sign + "×tamp=" + timestamp);
|
||||
OapiRobotSendRequest req = new OapiRobotSendRequest();
|
||||
/**
|
||||
* 发送文本消息
|
||||
*/
|
||||
//定义文本内容
|
||||
OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
|
||||
text.setContent(msg);
|
||||
//定义 @ 对象
|
||||
OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
|
||||
at.setAtUserIds(List.of(USER_ID));
|
||||
//设置消息类型
|
||||
req.setMsgtype("text");
|
||||
req.setText(text);
|
||||
req.setAt(at);
|
||||
OapiRobotSendResponse rsp = client.execute(req, CUSTOM_ROBOT_TOKEN);
|
||||
return rsp.getBody();
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送消息到企业群
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String sendChatMessage(String chatId, String message) throws Exception {
|
||||
String token = getToken();
|
||||
DefaultDingTalkClient client = new DefaultDingTalkClient(DingTalkUrlEnum.DING_TALK_CHAR_MESSAGE.getUrl());
|
||||
OapiChatSendRequest req = new OapiChatSendRequest();
|
||||
req.setChatid(chatId);
|
||||
OapiChatSendRequest.Msg msg = new OapiChatSendRequest.Msg();
|
||||
OapiChatSendRequest.Text text = new OapiChatSendRequest.Text();
|
||||
text.setContent(message);
|
||||
msg.setText(text);
|
||||
msg.setMsgtype("text");
|
||||
req.setMsg(msg);
|
||||
OapiChatSendResponse rsp = client.execute(req, token);
|
||||
log.info("[DingTalk] send chat message, req:{}, token:{}, response:{}", JSONObject.toJSONString(req), token, JSONObject.toJSONString(rsp));
|
||||
return rsp.getMessageId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 异步发送个人工作通知--文本类型
|
||||
*
|
||||
* @param userId
|
||||
* @param message
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public String asyncSendMessage(String userId, String message) throws Exception {
|
||||
String token = getToken();
|
||||
DingTalkClient client = new DefaultDingTalkClient(DingTalkUrlEnum.DING_TALK_ASYNC_SEND_MESSAGE.getUrl());
|
||||
OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request();
|
||||
req.setAgentId(Long.parseLong(AGENT_ID));
|
||||
req.setUseridList(userId);
|
||||
req.setToAllUser(false);
|
||||
OapiMessageCorpconversationAsyncsendV2Request.Msg obj1 = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
|
||||
obj1.setMsgtype(MSG_TYPE);
|
||||
OapiMessageCorpconversationAsyncsendV2Request.Text obj2 = new OapiMessageCorpconversationAsyncsendV2Request.Text();
|
||||
obj2.setContent(message);
|
||||
obj1.setText(obj2);
|
||||
req.setMsg(obj1);
|
||||
log.info("send Ding Talk message, userId:{}, message:{}", userId, message);
|
||||
OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(req, token);
|
||||
log.info("send Ding Talk message response, taskId:{}", JSONObject.toJSONString(rsp));
|
||||
return rsp.getTaskId().toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取企业内部应用token
|
||||
*
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public String getToken() throws Exception {
|
||||
DingTalkClient client = new DefaultDingTalkClient(DingTalkUrlEnum.DING_TALK_GET_ENTERPRISE_INTER_TOKEN.getUrl());
|
||||
OapiGettokenRequest req = new OapiGettokenRequest();
|
||||
req.setAppkey(CLIENT_ID);
|
||||
req.setAppsecret(CLIENT_SECRET);
|
||||
req.setHttpMethod("GET");
|
||||
OapiGettokenResponse rsp = client.execute(req);
|
||||
return rsp.getAccessToken();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user