java实现钉钉机器人消息推送的示例代码
作者:汤圆一号 发布时间:2023-05-18 13:53:25
标签: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<String> 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<String> mobileList) {
//消息内容
Map<String, String> contentMap = Maps.newHashMap();
contentMap.put("content", content);
//通知人
Map<String, Object> atMap = Maps.newHashMap();
//1.是否通知所有人
atMap.put("isAtAll", isAtAll);
//2.通知具体人的手机号码列表
atMap.put("atMobiles", mobileList);
Map<String, Object> 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<String> mobileList = Lists.newArrayList();
增加通知人号码列表(注:isAtAll和mobileList 不能同时生效)
//是否通知所有人
boolean isAtAll = false;
//通知具体人的手机号码列表
List<String> 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项目
添加依赖
<!--糊涂工具-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.0.12</version>
</dependency>
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请求,可以直接用!
来源:https://blog.csdn.net/weixin_43968234/article/details/88898958


猜你喜欢
- Fragment是Android honeycomb 3.0开始新增的概念,Fragment名为碎片不过却和Activity十
- 前言其实很多人都会碰到文本不对齐,文字不对齐的情况,但是只要不明显被提出,一般都会置之不理。我关注这个问题是因为有个老哥问我倒计时的时候,1
- Java作为一面向对象的语言,具备面向对象的三大特征——继承,多态,封装。继承顾名思义,继任,承接,传承的意思。面向对象的语言有一个好处,就
- 前言quarkus号称超音速亚原子JAVA为Graalvm量身定制的java堆栈,是否名副其实呢?下面就来看看真实情况如何。动手前先简单介绍
- 下面通过代码给大家分享下依赖注入框架Autofac的使用,具体如下所示: Autofac是一款IOC框架,比较于其他的IOC框架,
- 1.Comparable前言,想要排序Student.有代码:import java.util.Arrays; class Stu
- 本文实例为大家分享了Android使用Gridview单行横向滚动显示的具体代码,供大家参考,具体内容如下要想实现滚动显示,layout布局
- 首先客户端从服务器端获取json数据1、利用HttpUrlConnection/** &nbs
- 记录一下微信第三方实现登录的方法。还是比较简单。一、必要的准备工作1.首先需要注册并被审核通过的微信开放平台帐号,然后创建一个移动应用,也需
- RecyclerView 滑动时的优化处理,在滑动时停止加载图片,在滑动停止时开始加载图片,这里用了Glide.pause 和Glide.r
- 下面一段简单的代码给大家分享java 获取对象中为null的字段,具体代码如下所述:private static String[] getN
- Java for循环几种写法整理概要:J2SE 1.5提供了另一种形式的for循环。借助这种形式的for循环,可以用更简单地方式来遍历数组和
- 最近做MVC网站时刚好用到,用以提供一个完整的文件夹并压缩下载,正好做个笔记。拷贝文件夹的所有内容到另一个文件夹内:public stati
- 最近在开发即时通讯这个模块的时候使用到了自定义的相机,需求与微信一样,要求相机能长按和轻点,当时在网上找自定义相机的资源,很少,所以,我在这
- Android的一个核心特性就是一个应用程序可作为其他应用程序中的元素,可为其他应用程序提供数据。例如,如果程序需要用某些控件来加载一些图片
- Windows操作系统可以实现重命名文件操作,却不能实现批量重命名。本实例实现了批量重命名功能,可以将一个文件夹内同一类型的文件按照一定的规
- Android中SQLite 使用方法详解现在的主流移动设备像android、iPhone等都使用SQLite作为复杂数据的存储引擎,在我们
- Java基础面试题及答案集锦(基础题122道,代码题19道),具体详情如下所示:1、面向对象的特征有哪些方面1.抽象:抽象就是忽略一个主题中
- Java 调用long的最大值和最小值今天对Java八种基本数据类型进行总结,当总结到整数类型中的long时,出现了测试long最大值和最小
- 11.可以使用抽象函数重写基类中的虚函数吗? 答: 可以 需使用 new 修饰符显式声明,表示隐藏了基类中该函数的实现