调用钉钉API发送消息通知给个人或部门

 

先让公司行政将你设置为管理员:

需要拥有的权限为:工作台管理,开发者权限。

登录钉钉管理后台:

https://oa.dingtalk.com

首先,在开发者账号管理中添加ip白名单

https://open-dev.dingtalk.com/

开发者信息》开发者账号管理:本企业开发授权:编辑

 

调用钉钉API发送消息通知给个人或部门

 

调用钉钉API发送消息通知给个人或部门

调用钉钉API发送消息通知给个人或部门

 

然后创建应用

调用钉钉API发送消息通知给个人或部门

 

调用钉钉API发送消息通知给个人或部门

选择工作台》自建应用

先创建应用

 

 

 

调用钉钉API发送消息通知给个人或部门

 

调用钉钉API发送消息通知给个人或部门

完成后查看信息

调用钉钉API发送消息通知给个人或部门

 

申请好后可通过下面项目进行测试

https://github.com/opendingtalk/eapp-corp-project

需要将Constant.java文件中的参数写完整

发送HTTP请求的方法:

private static String httpsRequest(String requestUrl, String requestMethod, String outputStr) throws Exception {
          HttpsURLConnection conn = null;
          BufferedReader bufferedReader = null;
          try {
              URL url = new URL(requestUrl);
              conn = (HttpsURLConnection) url.openConnection();
              conn.setDoOutput(true);
              conn.setDoInput(true);
              conn.setUseCaches(false);
              conn.setRequestMethod(requestMethod);
              conn.setRequestProperty("content-type", "application/json");
              if (null != outputStr) {
                   OutputStream outputStream = conn.getOutputStream();
                   outputStream.write(outputStr.getBytes("utf-8"));
                   outputStream.close();
              }
              bufferedReader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
              String str = null;
              StringBuffer buffer = new StringBuffer();
              while ((str = bufferedReader.readLine()) != null) {
                   buffer.append(str);
              }
              return buffer.toString();
          } catch (Exception e) {
              throw e;
          } finally {
              if (conn != null) {
                   conn.disconnect();
              }
              if (bufferedReader != null) {
                   try {
                        bufferedReader.close();
                   } catch (IOException e) {
 
                   }
              }
          }
     }

main测试方法:

 

    //测试前需先将白名单
    public static void main(String[] args) throws Exception {
        //发送消息
        String content = "{"
                + "\"touser\": \"\","//发送用户ID,多个用,分割
                + "\"toparty\": \"\","//发送部门ID,多个用,分割
                + "\"agentid\": \""+Constant.AGENTID+"\","
                + "\"msgtype\": \"text\","
                + "\"text\": {\"content\": \"大家好,这是部门消息测试,只针对技术部,收到忽略\"}"
                + "}";
        String url = "https://oapi.dingtalk.com/message/send?access_token="+AccessTokenUtil.getToken();
        String rt = httpsRequest(url, "GET", content);
        System.out.println(rt);
    }

 

然后在工作通知就会收到通知了:

调用钉钉API发送消息通知给个人或部门

 

更多信息可观看钉钉开发文档:

https://open-doc.dingtalk.com/microapp/serverapi2