java钉钉官方实现消息通知_学习java实现钉钉机器人消息推送的示例代码

学习java实现钉钉机器人消息推送的示例代码,在这里,小哥,插入图片,机器人,通知

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

易采站长站,站长之家为您整理了学习java实现钉钉机器人消息推送的示例代码的相关内容。

f825dd9b18c93eaa5d54453ee748146f.png

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

845bd02b108d8c72008b1156bb154d08.png

54d46fa731455b577da0b0b9975fa31d.png

c0807c031bafc214d694700f78ad95d5.png

212698de9147b3016d41c7d89dc2ab56.png

89d6ef1b443b099ede595cf34133cdb6.png

7cefbcf47c8c164c166877a96ffd3ea5.png

此时,机器人已经添加完毕,接下来编写我们连接机器人小哥的代码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}

钉钉群显示消息:

47aa6c429a410476e50756f18fbf3575.png

ok,简单的消息推送,这就完成了!

我们再来测试一下通知所有人和通知具体人

将isAtAll更改为true//是否通知所有人boolean isAtAll = true;//通知具体人的手机号码列表List mobileList = Lists.newArrayList();

aca63b894b794b061fe49095aec93179.png

增加通知人号码列表(注:isAtAll和mobileList 不能同时生效)//是否通知所有人boolean isAtAll = false;//通知具体人的手机号码列表List mobileList = Lists.newArrayList();mobileList.add("182********");

334ad80d7b4c24295560c32bc5a2ec91.png

再来测试一下特殊符号

换行标识符/** * 换行标识符 */private static final String NEWLINE = "\n";//钉钉机器人消息内容//String content = "小哥,你好!";StringBuffer sb = new StringBuffer();sb.append("小哥,你好!") .append(NEWLINE) .append("看会书");String content = sb.toString();

e13f2c98b169e13deb0f94080f39eb2c.png

emoji图片

先获取emoji图片的unicode编码

1f49d14e82c70bed18f62d1cf7c3976a.png

编写代码如下:/** * 苹果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();

d9dba36223756d512fa44a1533c58ecd.png

通常在我们的项目中,作为一些告警加入,方便且实用

很有意思的钉钉机器人,很多实用技巧,可以深入去探索一波!

更新于2019-12-05

很多小伙伴留言咨询http请求,这边给大家2个http请求代码

1. maven项目

添加依赖cn.hutoolhutool-all4.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请求,可以直接用!以上就是关于对学习java实现钉钉机器人消息推送的示例代码的详细介绍。欢迎大家对学习java实现钉钉机器人消息推送的示例代码内容提出宝贵意见

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值