您现在的位置是:首页 >其他 >java实现钉钉自定义机器人发送消息网站首页其他
java实现钉钉自定义机器人发送消息
简介java实现钉钉自定义机器人发送消息
钉钉作为现在很多企业的移动办公平台,具有很多很强大的功能,可以帮助我们更加及时的发现问题,解决问题,今天我们做一个java实现钉钉自定义机器发送消息的功能。
首先,先放出官方文档地址:https://open.dingtalk.com/document/orgapp/custom-robot-access
接下来,先创建一个钉钉的群,打开钉钉群,在群的右上角点击齿轮图标 - 群设置,

再打开的群设置按钮,点击机器人,选择机器人 - 添加机器人 - 自定义机器人

添加机器人

自定义机器人
进入到机器人设置页面

设置完成之后,会生成一个webhook地址,复制该地址出来。

然后点击完成,群内自定义机器人就添加完成了。
机器人添加完成之后,我们需要在代码中实现对机器人发送消息的功能。
在java代码中,我们可以创建一个钉钉发送消息的utils类,代码如下
public class DingdingTalkUtils {
public static String url = "webhook地址"; //生成机器人申城webhook地址
public static String keyWords = "安全设置-关键字内容"
public static String access__secret = "安全设置-加签"; //安全设置 - 加签方式下生成的秘钥
public static String sendKeyWordMessage(String json) {
try {
if (!StringUtils.contains(json, keyWords)) {
log.error("使用了钉钉群发送消息但是没有关键词 :{} >>> {} 将无法发送消息,直接退回", json, keyWords);
return null;
}
Map<String, String> tmpMap = new HashMap<>();
tmpMap.put("content", json);
Map<String, Object> contentMap = new HashMap<>();
contentMap.put("msgtype", "text");
contentMap.put("text", tmpMap);
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json; charset=utf-8");
headers.put("Accept", "application/json");
return HttpSupport.makeFastConnect().doPostBody(url, JsonUtil.obj2Json(contentMap), headers).result();
} catch (Exception e) {
log.error("发送钉钉消息出现异常:{}", e.getMessage());
}
return null;
}
public static String sendEncryptMessage(String json, String secretKey) {
try {
String url = getUrl(secretKey);
Map<String, String> tmpMap = new HashMap<>();
tmpMap.put("content", json);
Map<String, Object> contentMap = new HashMap<>();
contentMap.put("msgtype", "text");
contentMap.put("text", tmpMap);
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json; charset=utf-8");
headers.put("Accept", "application/json");
return HttpSupport.makeFastConnect().doPostBody(url, JsonUtil.obj2Json(contentMap), headers).result();
} catch (Exception e) {
log.error("发送钉钉消息出现异常:{}", e.getMessage());
}
return null;
}
private static String getUrl(String secretKey) throws Exception {
String result = url;
long timestamp = System.currentTimeMillis();
String stringToSign = timestamp + "
" + secretKey;
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
byte[] signData = mac.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8));
String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");
result += "×tamp=" + timestamp + "&sign=" + sign;
return result;
}
}
上面的代码提供了通过关键字匹配发送,以及加签方式发送消息的两种方法,其中
contentMap.put("msgtype", "text");
这里面指定了发送的消息以何种形式展现,目前钉钉提供了五种展现消息的形式:
-
文本 (text)
-
链接 (link)
-
markdown(markdown)
-
ActionCard
-
FeedCard
另外,如果引入钉钉的sdk,还能实现@功能,有兴趣的同学可以看一下官方文档,实现钉钉机器人发送消息的功能并不难实现,相信难不倒大家,不过还是希望这篇文章能对大家有一点点的帮助,好了,今天的分享就到这里了,感谢大家!
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。





U8W/U8W-Mini使用与常见问题解决
QT多线程的5种用法,通过使用线程解决UI主界面的耗时操作代码,防止界面卡死。...
stm32使用HAL库配置串口中断收发数据(保姆级教程)
分享几个国内免费的ChatGPT镜像网址(亲测有效)
Allegro16.6差分等长设置及走线总结