第一步:群管理–> 智能群助手–> 添加机器人–> 自定义
第二步:通过加签的形式,需要记住加签的密码
第三步:获取webhook的access_token=***的值记录下来
#钉钉消息配置
#对应Webhook中的access_token值
ding.robot.token= ****
#对应安全设置->加签方式中的密钥
ding.robot.key=***
#手机号可以@到具体某人,以;分割
ding.robot.at=****
/**
* @Description: 钉钉消息管理
* @author: wanjun
*/
public interface IDingManager
{
/**
* @Description: 发送钉钉消息
* @param robotKey 对应Webhook中的access_token值
* @param robotToken 对应安全设置->加签方式中的密钥
* @param dingRobotAt 根据手机号可以@到具体某人,以;分割
* @param content 发送内容
* @return: void
*/
String sendDingMsg(String robotKey,String robotToken,String dingRobotAt,String content);
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import com.booway.cloud.commons.util.Func;
import com.booway.uums.dingrobot.IDingManager;
/**
* @Description: 钉钉消息管理
* @author: wanjun
*/
public class DingManager implements IDingManager
{
private static final Logger log = LoggerFactory.getLogger(DingManager.class);
private static RestTemplate restTemplate = new RestTemplate();
/**
* 通知人名单
*/
private List<String> mobileList = new ArrayList<String>();
@Override
public String sendDingMsg(String robotKey,String robotToken,String dingRobotAt,String content)
{
String timestamp = String.valueOf(System.currentTimeMillis());
String sign = Func.dingHmacSHA256(timestamp, robotKey);
// 钉钉机器人地址(配置机器人的webhook)
String dingUrl = "https://oapi.dingtalk.com/robot/send?access_token=" + robotToken + "×tamp=" + timestamp + "&sign=" + sign;
if (mobileList.isEmpty())
{
//填入手机号,可以具体@到某个人
if (!StringUtils.isEmpty(dingRobotAt))
{
String [] all = StringUtils.split(dingRobotAt, ";");
if (all != null)
{
mobileList.addAll(Arrays.asList(all));
}
}
}
//组装请求内容
Map<String, Object> reqStr = buildReqStr(content, mobileList.isEmpty(), mobileList);
return invokeGeneralApi(dingUrl, reqStr);
}
/**
* 组装请求报文
* @param content
* @return
*/
private Map<String, Object> buildReqStr(String content, boolean isAtAll, List<String> mobileList) {
//消息内容
Map<String, String> contentMap = new HashMap<String, String>();
contentMap.put("content", content);
//通知人
Map<String, Object> atMap = new HashMap<String, Object>();
//1.是否通知所有人
atMap.put("isAtAll", isAtAll);
//2.通知具体人的手机号码列表
atMap.put("atMobiles", mobileList);
Map<String, Object> reqMap = new HashMap<String, Object>();
reqMap.put("msgtype", "text");
reqMap.put("text", contentMap);
reqMap.put("at", atMap);
return reqMap;
}
private String invokeGeneralApi(String url, Map<String, Object> params) {
String body = "";
try {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(params, headers);
ResponseEntity<String> entity = restTemplate.postForEntity(url, httpEntity, String.class);
body = entity.getBody();
log.info("invokeGeneralApi()>>>[{}]", body);
} catch (RestClientException e) {
log.error("API调用错误,接口地址:[{}],请求参数:[{}]", url, params, e);
}
return body;
}
}
public class Func
{
/**
* 钉钉自定义机器人安全设置
* 把timestamp+"\n"+密钥当做签名字符串,使用HmacSHA256算法计算签名,然后进行Base64 encode,最后再把签名参数再进行urlEncode,得到最终的签名(需要使用UTF-8字符集)
* @param secret
* @return
*/
public static String dingHmacSHA256(String timestamp, String secret) {
try {
String stringToSign = timestamp + "\n" + secret;
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");
return sign;
} catch (Exception e) {
ERROR_LOG.error("dingHmacSHA256加密失败", e);
}
return null;
}
}
dingManager.sendDingMsg(robotKey, robotToken, robotAt, “截止到0点,发现以下账号登录异常(括号里面为异常次数):8”);