Java推送釘釘應(yīng)用消息實(shí)現(xiàn)過(guò)程
前言:
本文的目的是通過(guò)手機(jī)號(hào)獲取釘釘成員的userid,實(shí)現(xiàn)釘釘應(yīng)用的消息推送。
一、創(chuàng)建釘釘應(yīng)用
登錄釘釘開放平臺(tái)
二、應(yīng)用相關(guān)憑證
需要獲取
Client ID (原 AppKey 和 SuiteKey)
Client Secret (原 AppSecret 和 SuiteSecret)
App ID
原企業(yè)內(nèi)部應(yīng)用AgentId
三、申請(qǐng)釘釘接口權(quán)限
需要先申請(qǐng)對(duì)應(yīng)的接口權(quán)限才能調(diào)用接口。
但是釘釘?shù)慕涌谔嗔?,一時(shí)半會(huì)也找不到對(duì)應(yīng)的接口,推薦直接全勾選。
四、釘釘官方接口
1、獲取token
請(qǐng)求方式:GET
參數(shù):appKey,appSecret,reqMethod
url:https://oapi.dingtalk.com/gettoken
2、根據(jù)手機(jī)號(hào)獲取用戶ID
參數(shù):access_token,mobileNum
url:https://oapi.dingtalk.com/topapi/v2/user/getbymobile
3、發(fā)送通知
參數(shù):access_token,msgType,content,userId
url:https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2
五、工具類
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.dingtalk.api.DefaultDingTalkClient; import com.dingtalk.api.DingTalkClient; import com.dingtalk.api.request.OapiGettokenRequest; import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request; import com.dingtalk.api.request.OapiV2UserGetbymobileRequest; import com.dingtalk.api.response.OapiGettokenResponse; import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response; import com.dingtalk.api.response.OapiV2UserGetbymobileResponse; import com.taobao.api.ApiException; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @Slf4j @Component public class DingTalkUtil { private static final String GET_TOKEN_URL = "https://oapi.dingtalk.com/gettoken"; private static final String GET_BY_MOBILE = "https://oapi.dingtalk.com/topapi/v2/user/getbymobile"; private static final String ASYNC_SEND_V2_URL = "https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2"; private static final String DEFAULT_APP_KEY = "XXX"; private static final String DEFAULT_APP_SECRET = "XXXX"; private static final Long DEFAULT_AGENT_ID = 123L; private static final String DEFAULT_REQUEST_METHOD = "GET"; private String appKey; private String appSecret; private Long agentId; private String reqMethod; public DingTalkUtil() { this(DEFAULT_APP_KEY, DEFAULT_APP_SECRET, DEFAULT_AGENT_ID, DEFAULT_REQUEST_METHOD); } public DingTalkUtil(String appKey, String appSecret, Long agentId, String reqMethod) { this.appKey = (appKey != null && !appKey.isEmpty()) ? appKey : DEFAULT_APP_KEY; this.appSecret = (appSecret != null && !appSecret.isEmpty()) ? appSecret : DEFAULT_APP_SECRET; this.agentId = (agentId != null) ? agentId : DEFAULT_AGENT_ID; this.reqMethod = (reqMethod != null && !reqMethod.isEmpty()) ? reqMethod : DEFAULT_REQUEST_METHOD; } // 獲取AccessToken public String getTokenResponse() throws ApiException { DingTalkClient client = new DefaultDingTalkClient(GET_TOKEN_URL); OapiGettokenRequest req = new OapiGettokenRequest(); req.setAppkey(appKey); req.setAppsecret(appSecret); req.setHttpMethod(reqMethod); OapiGettokenResponse rsp = client.execute(req); return rsp.getAccessToken(); } // 根據(jù)手機(jī)號(hào)獲取UserId public String getUserIdByMobile(String accessToken, String mobileNum) throws ApiException { DingTalkClient client = new DefaultDingTalkClient(GET_BY_MOBILE); OapiV2UserGetbymobileRequest req = new OapiV2UserGetbymobileRequest(); req.setMobile(mobileNum); OapiV2UserGetbymobileResponse rsp = client.execute(req, accessToken); JSONObject jsonObject = JSON.parseObject(rsp.getBody()); JSONObject result = jsonObject.getJSONObject("result"); String userid = result.getString("userid"); return userid; } // 發(fā)送工作通知消息 public boolean sendWorkNotice(String accessToken, String msgType, String content, List<String> userIds) throws ApiException { DingTalkClient client = new DefaultDingTalkClient(ASYNC_SEND_V2_URL); OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request(); req.setAgentId(agentId); req.setUseridList(String.join(",", userIds)); OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg(); msg.setMsgtype(msgType); OapiMessageCorpconversationAsyncsendV2Request.Text text = new OapiMessageCorpconversationAsyncsendV2Request.Text(); text.setContent(content); msg.setText(text); req.setMsg(msg); OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(req, accessToken); return rsp.isSuccess(); } public static class Builder { private String msgType = "text"; private String content; private List<String> mobiles = new ArrayList<>(); private List<String> userIds = new ArrayList<>(); public Builder setMsgType(String msgType) { this.msgType = msgType; return this; } public Builder setContent(String content) { this.content = content; return this; } public Builder addUserId(String userId) { this.mobiles.add(userId); return this; } public Builder addUserIds(List<String> userIds) { this.mobiles = userIds; return this; } public boolean send(DingTalkUtil utils) throws ApiException { // 獲取 accessToken String accessToken = utils.getTokenResponse(); // 根據(jù)手機(jī)獲取userId for (String mobileNum : mobiles) { String userId = utils.getUserIdByMobile(accessToken, mobileNum); userIds.add(userId); } // 調(diào)用 sendWorkNotice 發(fā)送消息 return utils.sendWorkNotice(accessToken, msgType, content, userIds); } } // 封裝的發(fā)送釘釘通知的方法 public boolean sendDingTalkNotification(List<String> phoneList, String message) throws ApiException { DingTalkUtil dingTalkUtil = new DingTalkUtil(); return new DingTalkUtil.Builder().setMsgType("text") .setContent(message) .addUserIds(phoneList) .send(dingTalkUtil); } // 內(nèi)部測(cè)試 public static void main(String[] args) throws ApiException { DingTalkUtil dingTalkUtil = new DingTalkUtil(); List<String> phoneList = Arrays.asList("phoneNum"); String message = "your message"; boolean result = dingTalkUtil.sendDingTalkNotification(phoneList, message); System.out.println("消息發(fā)送結(jié)果: " + result); } }
六、MAVEN引入
<dependency> <groupId>com.aliyun</groupId> <artifactId>dingtalk</artifactId> <version>1.5.24</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>alibaba-dingtalk-service-sdk</artifactId> <version>2.0.0</version> </dependency>
總結(jié)
到此這篇關(guān)于Java推送釘釘應(yīng)用消息實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Java推送釘釘應(yīng)用消息內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java訪問(wèn)者設(shè)計(jì)模式詳細(xì)講解
大多數(shù)情況下你不需要訪問(wèn)者模式,但當(dāng)一旦需要訪問(wèn)者模式時(shí),那就是真的需要它了,這是設(shè)計(jì)模式創(chuàng)始人的原話。可以看出應(yīng)用場(chǎng)景比較少,但需要它的時(shí)候是不可或缺的,這篇文章就開始學(xué)習(xí)最后一個(gè)設(shè)計(jì)模式——訪問(wèn)者模式2022-11-11基于OpenCv與JVM實(shí)現(xiàn)加載保存圖像功能(JAVA?圖像處理)
openCv有一個(gè)名imread的簡(jiǎn)單函數(shù),用于從文件中讀取圖像,本文給大家介紹JAVA?圖像處理基于OpenCv與JVM實(shí)現(xiàn)加載保存圖像功能,感興趣的朋友一起看看吧2022-01-01解決在微服務(wù)環(huán)境下遠(yuǎn)程調(diào)用feign和異步線程存在請(qǐng)求數(shù)據(jù)丟失問(wèn)題
這篇文章主要介紹了解決在微服務(wù)環(huán)境下遠(yuǎn)程調(diào)用feign和異步線程存在請(qǐng)求數(shù)據(jù)丟失問(wèn)題,主要包括無(wú)異步線程得情況下feign遠(yuǎn)程調(diào)用,異步情況下丟失上下文問(wèn)題,需要的朋友可以參考下2022-05-05Java kafka如何實(shí)現(xiàn)自定義分區(qū)類和攔截器
這篇文章主要介紹了Java kafka如何實(shí)現(xiàn)自定義分區(qū)類和攔截器,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06