feat:添加机器人发送钉钉消息
This commit is contained in:
@@ -37,6 +37,12 @@
|
|||||||
<artifactId>alidns20150109</artifactId>
|
<artifactId>alidns20150109</artifactId>
|
||||||
<version>3.4.7</version>
|
<version>3.4.7</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-codec</groupId>
|
||||||
|
<artifactId>commons-codec</artifactId>
|
||||||
|
<version>1.11</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
@@ -6,13 +6,22 @@ import com.dingtalk.api.DingTalkClient;
|
|||||||
import com.dingtalk.api.request.OapiChatSendRequest;
|
import com.dingtalk.api.request.OapiChatSendRequest;
|
||||||
import com.dingtalk.api.request.OapiGettokenRequest;
|
import com.dingtalk.api.request.OapiGettokenRequest;
|
||||||
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
|
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
|
||||||
|
import com.dingtalk.api.request.OapiRobotSendRequest;
|
||||||
import com.dingtalk.api.response.OapiChatSendResponse;
|
import com.dingtalk.api.response.OapiChatSendResponse;
|
||||||
import com.dingtalk.api.response.OapiGettokenResponse;
|
import com.dingtalk.api.response.OapiGettokenResponse;
|
||||||
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
|
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
|
||||||
|
import com.dingtalk.api.response.OapiRobotSendResponse;
|
||||||
import com.xiang.xservice.basic.xservice.dingTalk.enums.DingTalkUrlEnum;
|
import com.xiang.xservice.basic.xservice.dingTalk.enums.DingTalkUrlEnum;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Service;
|
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
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
public class DingTalkService {
|
public class DingTalkService {
|
||||||
@@ -30,7 +39,6 @@ public class DingTalkService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 组织ID
|
* 组织ID
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
private static final String CORP_ID = "dingf2c4425cd179a26ef2c783f7214b6d69";
|
private static final String CORP_ID = "dingf2c4425cd179a26ef2c783f7214b6d69";
|
||||||
|
|
||||||
@@ -44,12 +52,55 @@ public class DingTalkService {
|
|||||||
|
|
||||||
private static final String MSG_TYPE = "text";
|
private static final String MSG_TYPE = "text";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自定义机器人token
|
||||||
|
*/
|
||||||
|
private static final String CUSTOM_ROBOT_TOKEN = "4709b708d961846e0aee523c5abc3b67e8fa424ee292501d85efd4e504f15a8b";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 密钥
|
||||||
|
*/
|
||||||
|
private static final String SECRET = "SEC768ed578c0fb31a9aec84b1c1db4f195f5aca203985bbb9d549e23e41c8874d1";
|
||||||
|
|
||||||
|
|
||||||
|
public static 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
System.out.println(DingTalkService.sendRobotMessage("机器人测试"));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送消息到企业群
|
* 发送消息到企业群
|
||||||
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public String sendChatMessage(String chatId, String message) throws Exception{
|
public String sendChatMessage(String chatId, String message) throws Exception {
|
||||||
String token = getToken();
|
String token = getToken();
|
||||||
DefaultDingTalkClient client = new DefaultDingTalkClient(DingTalkUrlEnum.DING_TALK_CHAR_MESSAGE.getUrl());
|
DefaultDingTalkClient client = new DefaultDingTalkClient(DingTalkUrlEnum.DING_TALK_CHAR_MESSAGE.getUrl());
|
||||||
OapiChatSendRequest req = new OapiChatSendRequest();
|
OapiChatSendRequest req = new OapiChatSendRequest();
|
||||||
@@ -66,7 +117,8 @@ public class DingTalkService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 异步发送工作同志--文本类型
|
* 异步发送个人工作通知--文本类型
|
||||||
|
*
|
||||||
* @param userId
|
* @param userId
|
||||||
* @param message
|
* @param message
|
||||||
* @return
|
* @return
|
||||||
|
|||||||
Reference in New Issue
Block a user