DingTalkUtil.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package jnpf.util.third;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.dingtalk.api.DefaultDingTalkClient;
  4. import com.dingtalk.api.DingTalkClient;
  5. import com.dingtalk.api.request.OapiGettokenRequest;
  6. import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
  7. import com.dingtalk.api.response.OapiGettokenResponse;
  8. import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
  9. import com.taobao.api.ApiException;
  10. import jnpf.util.RandomUtil;
  11. /**
  12. * 通过钉钉用户ID串进行发送消息,传入就是接收人的钉钉用户ID串
  13. *
  14. * @版本: V3.1.0
  15. * @版权: 引迈信息技术有限公司(https://www.jnpfsoft.com)
  16. * @作者: JNPF开发平台组
  17. * @日期: 2021/4/21 16:10
  18. */
  19. public class DingTalkUtil {
  20. /**
  21. * 钉钉发送消息的接口路径
  22. */
  23. public static final String SEND_MESSAGE = "https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2";
  24. /**
  25. * 钉钉获取TOKEN的接口路径
  26. */
  27. public static final String TOKEN = "https://oapi.dingtalk.com/gettoken";
  28. /**
  29. * 获取token
  30. * @param appkey
  31. * @param appsecret
  32. * @return
  33. */
  34. // public static String getToken (String appkey,String appsecret){
  35. // DefaultDingTalkClient client = new
  36. // DefaultDingTalkClient(TOKEN);
  37. // OapiGettokenRequest request = new OapiGettokenRequest();
  38. // request.setAppkey(appkey);
  39. // request.setAppsecret(appsecret);
  40. // request.setHttpMethod("GET");
  41. // try {
  42. // OapiGettokenResponse response = client.execute(request);
  43. //// LocalCacheClient.set("access_token", response.getAccessToken(),7200*1000);
  44. // return response.getAccessToken();
  45. // } catch (ApiException e) {
  46. // e.printStackTrace();
  47. // }
  48. // return null;
  49. // }
  50. /**
  51. * 获取token
  52. * @param appkey
  53. * @param appsecret
  54. * @return
  55. */
  56. public static JSONObject getAccessToken(String appkey, String appsecret){
  57. JSONObject retMsg = new JSONObject();
  58. retMsg.put("code",true);
  59. retMsg.put("error","");
  60. try{
  61. DingTalkClient client = new DefaultDingTalkClient(TOKEN);
  62. OapiGettokenRequest req = new OapiGettokenRequest();
  63. req.setAppkey(appkey);
  64. req.setAppsecret(appsecret);
  65. req.setHttpMethod("GET");
  66. OapiGettokenResponse rsp = client.execute(req);
  67. retMsg.put("access_token", rsp.getAccessToken());
  68. if (!rsp.isSuccess()) {
  69. retMsg.put("code", false);
  70. retMsg.put("error",rsp.getErrmsg());
  71. retMsg.put("access_token", "");
  72. }
  73. } catch (ApiException e) {
  74. retMsg.put("code", false);
  75. retMsg.put("error",e.toString());
  76. retMsg.put("access_token", "");
  77. }
  78. return retMsg;
  79. }
  80. /**
  81. * 给用户推送消息(文字消息)
  82. * @param appkey
  83. * @param appsecret
  84. * @param agentid
  85. * @param userIds
  86. * @param content
  87. * @return
  88. * 收到消息格式如下:
  89. * 发送的内容
  90. */
  91. public static JSONObject sendDingMessage(String appkey, String appsecret, String agentid, String userIds, String content){
  92. JSONObject retMsg = new JSONObject();
  93. DingTalkClient client = new DefaultDingTalkClient(SEND_MESSAGE);
  94. OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
  95. request.setUseridList(userIds);
  96. request.setAgentId(Long.parseLong(agentid));
  97. request.setToAllUser(false);
  98. OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
  99. msg.setMsgtype("text");
  100. msg.setText(new OapiMessageCorpconversationAsyncsendV2Request.Text());
  101. String randomCode = "随机验证码:"+ RandomUtil.uuId();
  102. msg.getText().setContent(content+randomCode);
  103. request.setMsg(msg);
  104. try {
  105. retMsg = getAccessToken(appkey,appsecret);
  106. if(retMsg.getBoolean("code")){
  107. // OapiMessageCorpconversationAsyncsendV2Response response = client.execute(request,getToken(appkey,appsecret));
  108. OapiMessageCorpconversationAsyncsendV2Response response = client.execute(request,retMsg.getString("access_token"));
  109. if(response.getErrcode()>0){
  110. retMsg.put("code",false);
  111. retMsg.put("error",response.getErrmsg());
  112. }else{
  113. retMsg.put("code",true);
  114. retMsg.put("error","");
  115. }
  116. }else{
  117. retMsg.put("code",false);
  118. retMsg.put("error","获取token失败:"+retMsg.getString("error"));
  119. }
  120. return retMsg;
  121. } catch (ApiException e) {
  122. retMsg.put("code",false);
  123. retMsg.put("error",e.toString());
  124. return retMsg;
  125. }
  126. }
  127. }