在钉钉群里创建自定义机器人(群设置-机器人-添加机器人),选择自定义机器人
安全设置有三种,本文章使用加签(secret)方式 ,其他方式可参考官方文档:获取自定义机器人 Webhook 地址 - 钉钉开放平台
添加成功后有一个Webhook,也就是机器人的发送消息的url。
主要需要Webhook,和 secret密钥。
发送方法直接调用sendDingTalkMsg()方法,一个是发送给全体成员,一个是根据手机号发送给具体人。
import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.springframework.util.CollectionUtils;import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;/*** 钉钉-工具类*/
@Slf4j
public class DingTalkUtil {/*** 钉钉机器人发消息地址(配置机器人的webhook)*/private String webhook;/*** 密钥*/private String secret;public DingTalkUtil(String webhook, String secret) {this.webhook = webhook;this.secret = secret;}/*** 签名计算*/private String sign(Long timestamp , String secret) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {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");System.out.println(sign);return sign;}/*** 通知具体人** @param content 通知内容* @param mobileList 通知具体人的手机号码列表*/public void sendDingTalkMsg(List<String> mobileList, String content) {sendDingTalkMsg(false, mobileList, content);}/*** 通知所有人** @param content 通知内容*/public void sendDingTalkMsg(String content) {sendDingTalkMsg(true, null, content);}/*** 发送消息** @param content 通知内容* @param isAtAll 是否@所有人* @param mobileList 通知具体人的手机号码列表*/private void sendDingTalkMsg(boolean isAtAll, List<String> mobileList, String content) {try {Long timestamp = System.currentTimeMillis();String sign = sign(timestamp,secret);String msg_url = webhook + "×tamp=" + timestamp + "&sign=" + sign;if (!CollectionUtils.isEmpty(mobileList)) {isAtAll = false;}//消息内容Map<String, String> contentMap = new HashMap<>();contentMap.put("content", content);//通知人Map<String, Object> atMap = new HashMap<>();//1.是否通知所有人atMap.put("isAtAll", isAtAll);//2.通知具体人的手机号码列表atMap.put("atMobiles", mobileList);Map<String, Object> parameter = new HashMap<>();parameter.put("msgtype", "text");parameter.put("at", atMap);parameter.put("text", contentMap);//推送消息(http请求)String result = HttpUtils.post(msg_url, JSON.toJSONString(parameter));log.info("发送钉钉消息-result:{}", result);} catch (Exception e) {log.error("发送钉钉消息异常", e);}}}
http请求工具类
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;import java.io.Closeable;
import java.nio.charset.Charset;
import java.util.Map;/*** http请求-工具类*/
@Slf4j
public class HttpUtils {private static final String UTF_8 = "utf-8";private static RequestConfig requestConfig;static {requestConfig = RequestConfig.custom().setConnectTimeout(1000)//连接超时时间.setSocketTimeout(3000)//读超时时间.build();}/*** post请求(无参/参数在url上)*/public static String post(String url) throws Exception {return post(url, "");}/*** post请求*/public static String post(String url, String body) throws Exception {CloseableHttpClient httpClient = null;CloseableHttpResponse httpResponse = null;try {httpClient = HttpClients.createDefault();HttpPost httpPost = new HttpPost(url);if (StringUtils.isNotBlank(body)) {StringEntity se = new StringEntity(body, Charset.forName(UTF_8));se.setContentType("text/json");se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));httpPost.setEntity(se);}httpPost.setConfig(requestConfig);httpPost.setHeader("Content-Type", "application/json;charset=utf-8");httpResponse = httpClient.execute(httpPost);if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {return EntityUtils.toString(httpResponse.getEntity(), UTF_8);} else {log.error("post请求其它服务失败,statusCode:{}", httpResponse.getStatusLine().getStatusCode());}} catch (Exception e) {log.error("post请求其它服务异常", e);throw e;} finally {close(httpClient);close(httpResponse);}return null;}/*** post请求*/public static String post(String url, Map<String, String> parameter) throws Exception {return post(url, JSON.toJSONString(parameter));}/*** 关闭流*/private static <T extends Closeable> void close(T io) {if (io == null) {return;}try {io.close();} catch (Exception e) {log.error(e.getMessage(), e);}}
}