feat:钉钉消息优化2.0版本
This commit is contained in:
@@ -12,6 +12,7 @@ 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.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@@ -46,53 +47,40 @@ public class DingTalkService {
|
||||
|
||||
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";
|
||||
private static final String MSG_TYPE_TEXT = "text";
|
||||
|
||||
/**
|
||||
* 自定义机器人token
|
||||
*/
|
||||
private static final String CUSTOM_ROBOT_TOKEN = "4709b708d961846e0aee523c5abc3b67e8fa424ee292501d85efd4e504f15a8b";
|
||||
|
||||
/**
|
||||
* 密钥
|
||||
*/
|
||||
private static final String SECRET = "SEC768ed578c0fb31a9aec84b1c1db4f195f5aca203985bbb9d549e23e41c8874d1";
|
||||
|
||||
/**
|
||||
* 发送机器人消息
|
||||
* @param msg
|
||||
* 发送机器人消息到指定的群
|
||||
* @param robotSecret 机器人密钥
|
||||
* @param robotToken 机器人token
|
||||
* @param userIds @对象
|
||||
* @param msg 消息内容
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public String sendRobotMessage(String msg) throws Exception {
|
||||
public String sendRobotMessage(String robotSecret, String robotToken, List<String> userIds, String msg) throws Exception {
|
||||
Long timestamp = System.currentTimeMillis();
|
||||
String stringToSign = timestamp + "\n" + SECRET;
|
||||
String stringToSign = timestamp + "\n" + robotSecret;
|
||||
Mac mac = Mac.getInstance("HmacSHA256");
|
||||
mac.init(new SecretKeySpec(SECRET.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
|
||||
mac.init(new SecretKeySpec(robotSecret.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));
|
||||
at.setAtUserIds(userIds);
|
||||
//设置消息类型
|
||||
req.setMsgtype("text");
|
||||
req.setMsgtype(MSG_TYPE_TEXT);
|
||||
req.setText(text);
|
||||
req.setAt(at);
|
||||
OapiRobotSendResponse rsp = client.execute(req, CUSTOM_ROBOT_TOKEN);
|
||||
OapiRobotSendResponse rsp = client.execute(req, robotToken);
|
||||
return rsp.getBody();
|
||||
}
|
||||
|
||||
@@ -110,7 +98,7 @@ public class DingTalkService {
|
||||
OapiChatSendRequest.Text text = new OapiChatSendRequest.Text();
|
||||
text.setContent(message);
|
||||
msg.setText(text);
|
||||
msg.setMsgtype("text");
|
||||
msg.setMsgtype(MSG_TYPE_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));
|
||||
@@ -133,7 +121,7 @@ public class DingTalkService {
|
||||
req.setUseridList(userId);
|
||||
req.setToAllUser(false);
|
||||
OapiMessageCorpconversationAsyncsendV2Request.Msg obj1 = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
|
||||
obj1.setMsgtype(MSG_TYPE);
|
||||
obj1.setMsgtype(MSG_TYPE_TEXT);
|
||||
OapiMessageCorpconversationAsyncsendV2Request.Text obj2 = new OapiMessageCorpconversationAsyncsendV2Request.Text();
|
||||
obj2.setContent(message);
|
||||
obj1.setText(obj2);
|
||||
|
||||
Reference in New Issue
Block a user