feat:first commit

This commit is contained in:
xiang
2025-07-17 22:45:09 +08:00
commit 2acfac4341
15 changed files with 698 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
<?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.0-SNAPSHOT</version>
</parent>
<artifactId>xservice-third-part</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>
</dependencies>
</project>

View File

@@ -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;
}
}

View File

@@ -0,0 +1,111 @@
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.response.OapiChatSendResponse;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.xiang.xservice.basic.xservice.dingTalk.enums.DingTalkUrlEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@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";
/**
* 发送消息到企业群
* @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();
}
}