如何使用java实现钉钉机器人消息推送功能
创始人
2024-12-24 19:43:00
0次

先建个钉钉群,并加好机器人

此时,机器人已经添加完毕,接下来编写我们连接机器人小哥的代码

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;//通知具体人的手机号码列表List mobileList = Lists.newArrayList();

增加通知人号码列表(注:isAtAll和mobileList 不能同时生效)

//是否通知所有人boolean isAtAll = false;//通知具体人的手机号码列表List mobileList = 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(); }

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...