java调用钉钉api接口,实现机器人自动发送消息并艾特个人
前言
整个项目打包( 包括用java实现邮件发送,smtp服务器):
链接: https://pan.baidu.com/s/1yjzsDVV4LlbOqx7VvpVuKQ?pwd=1kn8 提取码: 1kn8
–来自百度网盘超级会员v5的分享
用java实现邮件发送,smtp服务器,Blog跳转:https://gryffinbit.top/2022/08/17/%E7%94%A8java%E5%AE%9E%E7%8E%B0%E9%82%AE%E4%BB%B6%E5%8F%91%E9%80%81%EF%BC%8Csmtp%E6%9C%8D%E5%8A%A1%E5%99%A8/
获取钉钉开发资格
登陆钉钉管理后台
钉钉管理后台- 钉钉统一身份认证
注册企业资格的时候忘了截图了。。。总之步骤登陆管理后台,它会告知你没有管理员身份的账号,你选择注册一个,按照流程提示注册完了之后就有权限了。这时候钉钉就会生成一个群。在这个群里添加机器人。
点群的设置,选择群助手,添加机器人
添加自定义机器人
设定参数
安全设定,一共有三个,根据需要勾选。比如选择了定制的关键词,那么发送消息的时候,只有当消息中存在这个关键词,才能发送成功
机器人添加成功后,会生成一个webhook,将它复制保存,代码中需要用到它
代码
DingNotice.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| package main.controller;
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;
public class DingNotice { public static void main(String[] args){
String dingDingToken="https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxx"; Map<String,Object> json = new HashMap(); Map<String,Object> text = new HashMap();
Map<String,List<String>> phone = new HashMap(); String [] mobile = {}; List mob = new ArrayList<String>(); mob.add("186xxxxxxx"); json.put("msgtype","text"); text.put("content","文字内容"); phone.put("atMobiles", mob); json.put("text",text); json.put("at", phone); String response = DingNoticeTest.sendPostByMap(dingDingToken, json); System.out.println("响应结果:"+response);
} }
|
DingNoticeTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| package main.controller;
import com.alibaba.fastjson.JSON; import java.io.*; import java.net.URL; import java.net.URLConnection; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry;
public class DingNoticeTest {
public static String sendPostByMap(String url, Map<String, Object> mapParam) { Map<String, String> headParam = new HashMap(); headParam.put("Content-type", "application/json;charset=UTF-8"); return sendPost(url, mapParam, headParam); }
public static String sendPost(String url, Map<String, Object> param, Map<String, String> headParam) { PrintWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); URLConnection conn = realUrl.openConnection(); conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Fiddler");
if (headParam != null) {
for (Entry<String, String> entry : headParam.entrySet()) { conn.setRequestProperty(entry.getKey(), entry.getValue()); } } conn.setDoOutput(true); conn.setDoInput(true); out = new PrintWriter(conn.getOutputStream()); out.print(JSON.toJSONString(param)); out.flush(); in = new BufferedReader( new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) {
e.printStackTrace(); } finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return result; } }
|
结果
参考文章
https://www.cnblogs.com/barrywxx/p/10716593.html#/publishEapp