引入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方法就可以使用了!