如何使用java实现钉钉机器人消息推送功能
发表于:2025-11-11 作者:千家信息网编辑
千家信息网最后更新 2025年11月11日,小编给大家分享一下如何使用java实现钉钉机器人消息推送功能,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!先建个钉钉群,并
千家信息网最后更新 2025年11月11日如何使用java实现钉钉机器人消息推送功能
小编给大家分享一下如何使用java实现钉钉机器人消息推送功能,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
先建个钉钉群,并加好机器人
此时,机器人已经添加完毕,接下来编写我们连接机器人小哥的代码
import com.alibaba.fastjson.JSON;import com.google.common.collect.Lists;import com.google.common.collect.Maps;import java.util.List;import java.util.Map;/** * @author yanghao * @version DingTalkTest.java, v 0.1 2019-03-29 11:36 */public class DingTalkTest { public static void main(String[] args){ try { //钉钉机器人地址(配置机器人的webhook) String dingUrl = "https://oapi.dingtalk.com/robot/send?access_token=............"; //是否通知所有人 boolean isAtAll = false; //通知具体人的手机号码列表 List mobileList = Lists.newArrayList(); //钉钉机器人消息内容 String content = "小哥,你好!"; //组装请求内容 String reqStr = buildReqStr(content, isAtAll, mobileList); //推送消息(http请求) String result = HttpUtil.postJson(dingUrl, reqStr); System.out.println("result == " + result); }catch (Exception e){ e.printStackTrace(); } } /** * 组装请求报文 * @param content * @return */ private static String buildReqStr(String content, boolean isAtAll, List mobileList) { //消息内容 Map contentMap = Maps.newHashMap(); contentMap.put("content", content); //通知人 Map atMap = Maps.newHashMap(); //1.是否通知所有人 atMap.put("isAtAll", isAtAll); //2.通知具体人的手机号码列表 atMap.put("atMobiles", mobileList); Map reqMap = Maps.newHashMap(); reqMap.put("msgtype", "text"); reqMap.put("text", contentMap); reqMap.put("at", atMap); return JSON.toJSONString(reqMap); }} 运行结果如下:
result == {"errmsg":"ok","errcode":0}
钉钉群显示消息:
ok,简单的消息推送,这就完成了!
我们再来测试一下通知所有人和通知具体人
将isAtAll更改为true
//是否通知所有人boolean isAtAll = true;//通知具体人的手机号码列表ListmobileList = Lists.newArrayList();
增加通知人号码列表(注:isAtAll和mobileList 不能同时生效)
//是否通知所有人boolean isAtAll = false;//通知具体人的手机号码列表ListmobileList = Lists.newArrayList();mobileList.add("182********");
再来测试一下特殊符号
换行标识符
/** * 换行标识符 */private static final String NEWLINE = "\n";//钉钉机器人消息内容//String content = "小哥,你好!";StringBuffer sb = new StringBuffer();sb.append("小哥,你好!") .append(NEWLINE) .append("看会书");String content = sb.toString();emoji图片
先获取emoji图片的unicode编码
编写代码如下:
/** * 苹果unicode编码 */private static final String APPLE = "\ud83c\udf4e";//钉钉机器人消息内容//String content = "小哥,你好!";StringBuffer sb = new StringBuffer();sb.append("小哥,你好!") .append(NEWLINE) .append("看会书") .append(NEWLINE) .append("吃个").append(APPLE);String content = sb.toString();通常在我们的项目中,作为一些告警加入,方便且实用很有意思的钉钉机器人,很多实用技巧,可以深入去探索一波!
更新于2019-12-05
很多小伙伴留言咨询http请求,这边给大家2个http请求代码
1. maven项目
添加依赖
cn.hutool hutool-all 4.0.12
http请求代码
private static final int timeout = 10000; public static String postJson(String url, String reqStr) { String body = null; try { body = HttpRequest.post(url).body(reqStr).timeout(timeout).execute().body(); } catch (Exception e) { e.printStackTrace(); } return body;}2. 非maven项目
添加jar包httpclient-xxx.jarcommons-logging-xxx.jar
http请求代码
public static String postJson(String url, String body) { // 创建Httpclient对象 CloseableHttpClient httpClient = createCustomClient(); CloseableHttpResponse response = null; String resultString = null; try { // 创建Http Post请求 HttpPost httpPost = new HttpPost(url); httpPost.addHeader("Content-Type", "application/json"); if (body != null) { httpPost.setEntity(new StringEntity(body, "utf-8")); } // 执行http请求 response = httpClient.execute(httpPost); resultString = EntityUtils.toString(response.getEntity(), "utf-8"); } catch (Exception e) { e.printStackTrace(); } finally { try { if (response != null) { response.close(); } } catch (Exception e) { e.printStackTrace(); } } return resultString; } public static CloseableHttpClient createCustomClient() { RequestConfig defaultRequestConfig = RequestConfig.custom() .setSocketTimeout(120 * 1000) .setConnectTimeout(120 * 1000) .setConnectionRequestTimeout(120 * 1000) .setStaleConnectionCheckEnabled(true) .build(); return HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build(); }以上是"如何使用java实现钉钉机器人消息推送功能"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
机器
机器人
消息
内容
小哥
代码
你好
号码
所有人
推送
手机
手机号码
篇文章
项目
功能
实用
图片
标识
标识符
编码
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
中国网络安全法自实施
数据库系统工作原理
centos mc服务器
信息对抗与网络安全txt下载
服务器是否属于低泄射设备
查看服务器内存使用率百分比
未转变者怎么服务器ip
计算机网络技术自考难吗
邮件服务器名称是什么
为什么租游戏服务器好
服务器网卡能用万能驱动吗
华为服务器专用以太网卡家用
flume采集到数据库
金蝶服务器登陆
国产数据库应用技术研究
广州net软件开发大概要多少钱
数据库如何多备份
数据库查询招收有学生学院
桓台模具管理软件开发公司
余杭融材料数据库
自建服务器可以吗
网络安全 操作题
魔兽数据库-德拉诺
广州摩天互联网科技有限公司
软件开发会议主题
计算机软件开发高中选课
数据库管理系统通常提供什么功能
管理软件开发采用哪些技术
应聘网络安全公司
php数组写数据库中