引入pom:
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dingtalk</artifactId>
<version>1.1.64</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibaba-dingtalk-service-sdk</artifactId>
<version>1.0.1</version>
</dependency>
提供工具类:
@Slf4j @Component public class DingtalkUtils { /** * 使用 Token 初始化账号Client * * @return Client * @throws Exception */ public static com.aliyun.dingtalkoauth2_1_0.Client createAuthClient() throws Exception { Config config = new Config(); config.protocol = "https"; config.regionId = "central"; return new com.aliyun.dingtalkoauth2_1_0.Client(config); } public static GetAccessTokenResponse getAccessToken(String appKey, String appSecret) throws Exception { com.aliyun.dingtalkoauth2_1_0.Client client = createAuthClient(); GetAccessTokenRequest getAccessTokenRequest = new GetAccessTokenRequest() .setAppKey(appKey) .setAppSecret(appSecret); GetAccessTokenResponse accessTokenResponse = null; try { accessTokenResponse = client.getAccessToken(getAccessTokenRequest); } catch (TeaException err) { if (!Common.empty(err.code) && !Common.empty(err.message)) { // err 中含有 code 和 message 属性,可帮助开发定位问题 log.error("获取钉钉accessToken异常 code:{} , message :{}", err.code, err.message); } } catch (Exception _err) { TeaException err = new TeaException(_err.getMessage(), _err); if (!Common.empty(err.code) && !Common.empty(err.message)) { // err 中含有 code 和 message 属性,可帮助开发定位问题 log.error("获取钉钉accessToken异常 code:{} , message :{}", err.code, err.message); } } return accessTokenResponse; } /** * 根据userid获取用户详情 * * @param userId * @param accessToken * @return * @throws Exception */ public static String getUserIndoByUserId(String userId, String accessToken) throws Exception { DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get"); OapiV2UserGetRequest req = new OapiV2UserGetRequest(); req.setUserid(userId); OapiV2UserGetResponse rsp = client.execute(req, accessToken); return rsp.getResult().getUnionid(); } /** * 发送消息 **/ public static void messageCorpConversationAsyncSend(OapiMessageCorpconversationAsyncsendV2Request req, String accessToken) { DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2"); try { client.execute(req, accessToken); } catch (TeaException err) { if (!Common.empty(err.code) && !Common.empty(err.message)) { // err 中含有 code 和 message 属性,可帮助开发定位问题 log.error("发送钉钉通知消息异常 code:{} , message :{}", err.code, err.message); } } catch (Exception _err) { TeaException err = new TeaException(_err.getMessage(), _err); if (!Common.empty(err.code) && !Common.empty(err.message)) { // err 中含有 code 和 message 属性,可帮助开发定位问题 log.error("发送钉钉通知消息异常 code:{} , message :{}", err.code, err.message); } } } }
发送消息的实现类:
/** * @ClassName: 钉钉消息通知实现类 * @Description: 钉钉消息通知实现类 **/ @Service public class ParkingDingTalkServiceImpl implements ParkingDingTalkService {
private static String APP_KEY = "you appkey"; //你的appkey private static String APP_SECRET = ""; //你的appsecret private static Long AGENT_ID = 1221; @Autowired private StringRedisTemplate redisTemplate;/** * @param userIds 接收人的userid * @param msg 消息体 * @Exception 异常 * 发送钉钉消息 */ @Override public void sendDingTalkMessage(String userIds,OapiMessageCorpconversationAsyncsendV2Request.Msg msg) throws Exception { String accessToken = getAccessToken(); OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request(); request.setAgentId(AGENT_ID); request.setUseridList(userIds); request.setMsg(msg); DingtalkUtils.messageCorpConversationAsyncSend(request, accessToken); } /** * 获取getAccessToken * * @return * @throws Exception */ private String getAccessToken() throws Exception { String accessToken = redisTemplate.opsForValue().get(Constants.SMP_DINGTALK_ACCESS_TOKEN); if (StringUtils.isNotEmpty(accessToken)) { return accessToken; } GetAccessTokenResponse accessTokenRes = DingtalkUtils.getAccessToken(APP_KEY, APP_SECRET); accessToken = accessTokenRes.getBody().getAccessToken(); Long expireIn = accessTokenRes.getBody().getExpireIn(); redisTemplate.opsForValue().set(Constants.SMP_DINGTALK_ACCESS_TOKEN, accessToken, expireIn, TimeUnit.SECONDS); return accessToken; } }
提供service:
public interface ParkingDingTalkService { /** * 发送钉钉消息 * @param userIds 用户ids,多个用户用','分割 * @param msg 消息体 * @throws Exception 抛出异常正常执行 */ public void sendDingTalkMessage(String userIds, OapiMessageCorpconversationAsyncsendV2Request.Msg msg) throws Exception; }
直接调用sevice方法就可以使用了!