public class AliDingDingMessagePush {标签:return,String,private,sign,static,消息,顶顶,推送,throws From: https://www.cnblogs.com/Husir-boky/p/17202640.html
private static final String MAC = "HmacSHA256";
private static final String CHARSET = "UTF-8";
private static final String TIMESTAMP = "timestamp";
private static final String SIGN = "sign";
private static final String WEBHOOT = "https://oapi.dingtalk.com/robot/send?access_token=";
private static final String TOKEN = "*****************************************************************";
private static final String SECRET = "*****************************************************************";
private static Logger log = LoggerFactory.getLogger(AliDingDingMessagePush.class);
/**
* 消息推送
*/
public static void messaagePush(String title, String message) {
OapiRobotSendRequest.Actioncard actioncard = new OapiRobotSendRequest.Actioncard();
actioncard.setTitle(title);
actioncard.setText(message);
robotPushActionCardMessage(actioncard);
}
/**
* 机器人推送消息
*
* @param actioncard
* @return
*/
private static OapiRobotSendResponse robotPushActionCardMessage(OapiRobotSendRequest.Actioncard actioncard) {
DingTalkClient client = null;
try {
client = getDingTalkClient(WEBHOOT, TOKEN, SECRET);
OapiRobotSendRequest request = new OapiRobotSendRequest();
request.setMsgtype("actionCard");
request.setActionCard(actioncard);
OapiRobotSendResponse response = client.execute(request);
if (ObjectUtil.isNotEmpty(response) && response.isSuccess()) {
log.info("钉钉消息发送成功");
} else {
log.info("钉钉消息发送成功");
}
return response;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ApiException e) {
e.printStackTrace();
}
return null;
}
/**
* 获取 DingTalkClient
*
* @param webHoot
* @param token
* @param secret
* @return
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws UnsupportedEncodingException
*/
private static DingTalkClient getDingTalkClient(String webHoot, String token, String secret) throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {
Map<String, String> sign = sign(secret);
return new DefaultDingTalkClient(webHoot + token + ("×tamp=" + sign.get("timestamp") + "&sign=" + sign.get("sign")));
}
/**
* 获取sing
*
* @param secret
* @return
* @throws NoSuchAlgorithmException
* @throws UnsupportedEncodingException
* @throws InvalidKeyException
*/
private static Map<String, String> sign(String secret) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
Long timeMillis = System.currentTimeMillis();
String stringToSing = timeMillis + "\n" + secret;
Mac mac = Mac.getInstance(MAC);
mac.init(new SecretKeySpec(secret.getBytes(CHARSET), MAC));
byte[] singData = mac.doFinal(stringToSing.getBytes(CHARSET));
String sign = URLEncoder.encode(new String(Base64.encodeBase64(singData), CHARSET));
Map<String, String> map = new HashMap<>();
map.put(TIMESTAMP, timeMillis.toString());
map.put(SIGN, sign);
return map;
}
}