栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java实现钉钉机器人消息推送的示例代码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;


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();

 }

 }

 
 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编码


编写代码如下:


private static final String APPLE = "ud83cudf4e";

//钉钉机器人消息内容
//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.jar
commons-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();
 }

方法仅供参考,项目里面有现成的http请求,可以直接用!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/135448.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号