java实现钉钉机器人消息推送的示例代码
先建个钉钉群,并加好机器人
此时,机器人已经添加完毕,接下来编写我们连接机器人小哥的代码
importcom.alibaba.fastjson.JSON;
importcom.google.common.collect.Lists;
importcom.google.common.collect.Maps;
importjava.util.List;
importjava.util.Map;
/**
*@authoryanghao
*@versionDingTalkTest.java,v0.12019-03-2911:36
*/
publicclassDingTalkTest{
publicstaticvoidmain(String[]args){
try{
//钉钉机器人地址(配置机器人的webhook)
StringdingUrl="https://oapi.dingtalk.com/robot/send?access_token=............";
//是否通知所有人
booleanisAtAll=false;
//通知具体人的手机号码列表
ListmobileList=Lists.newArrayList();
//钉钉机器人消息内容
Stringcontent="小哥,你好!";
//组装请求内容
StringreqStr=buildReqStr(content,isAtAll,mobileList);
//推送消息(http请求)
Stringresult=HttpUtil.postJson(dingUrl,reqStr);
System.out.println("result=="+result);
}catch(Exceptione){
e.printStackTrace();
}
}
/**
*组装请求报文
*@paramcontent
*@return
*/
privatestaticStringbuildReqStr(Stringcontent,booleanisAtAll,ListmobileList){
//消息内容
MapcontentMap=Maps.newHashMap();
contentMap.put("content",content);
//通知人
MapatMap=Maps.newHashMap();
//1.是否通知所有人
atMap.put("isAtAll",isAtAll);
//2.通知具体人的手机号码列表
atMap.put("atMobiles",mobileList);
MapreqMap=Maps.newHashMap();
reqMap.put("msgtype","text");
reqMap.put("text",contentMap);
reqMap.put("at",atMap);
returnJSON.toJSONString(reqMap);
}
}
运行结果如下:
result=={"errmsg":"ok","errcode":0}
钉钉群显示消息:
ok,简单的消息推送,这就完成了!
我们再来测试一下通知所有人和通知具体人
将isAtAll更改为true
//是否通知所有人 booleanisAtAll=true; //通知具体人的手机号码列表 ListmobileList=Lists.newArrayList();
增加通知人号码列表(注:isAtAll和mobileList不能同时生效)
//是否通知所有人 booleanisAtAll=false; //通知具体人的手机号码列表 ListmobileList=Lists.newArrayList(); mobileList.add("182********");
再来测试一下特殊符号
换行标识符
/**
*换行标识符
*/
privatestaticfinalStringNEWLINE="\n";
//钉钉机器人消息内容
//Stringcontent="小哥,你好!";
StringBuffersb=newStringBuffer();
sb.append("小哥,你好!")
.append(NEWLINE)
.append("看会书");
Stringcontent=sb.toString();
emoji图片
先获取emoji图片的unicode编码
/**
*苹果unicode编码
*/
privatestaticfinalStringAPPLE="\ud83c\udf4e";
//钉钉机器人消息内容
//Stringcontent="小哥,你好!";
StringBuffersb=newStringBuffer();
sb.append("小哥,你好!")
.append(NEWLINE)
.append("看会书")
.append(NEWLINE)
.append("吃个").append(APPLE);
Stringcontent=sb.toString();
通常在我们的项目中,作为一些告警加入,方便且实用
很有意思的钉钉机器人,很多实用技巧,可以深入去探索一波!
更新于2019-12-05
很多小伙伴留言咨询http请求,这边给大家2个http请求代码
1.maven项目
添加依赖
cn.hutool hutool-all 4.0.12
http请求代码
privatestaticfinalinttimeout=10000;
publicstaticStringpostJson(Stringurl,StringreqStr){
Stringbody=null;
try{
body=HttpRequest.post(url).body(reqStr).timeout(timeout).execute().body();
}catch(Exceptione){
e.printStackTrace();
}
returnbody;
}
2.非maven项目
添加jar包
httpclient-xxx.jar
commons-logging-xxx.jar
http请求代码
publicstaticStringpostJson(Stringurl,Stringbody){
//创建Httpclient对象
CloseableHttpClienthttpClient=createCustomClient();
CloseableHttpResponseresponse=null;
StringresultString=null;
try{
//创建HttpPost请求
HttpPosthttpPost=newHttpPost(url);
httpPost.addHeader("Content-Type","application/json");
if(body!=null){
httpPost.setEntity(newStringEntity(body,"utf-8"));
}
//执行http请求
response=httpClient.execute(httpPost);
resultString=EntityUtils.toString(response.getEntity(),"utf-8");
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
if(response!=null){
response.close();
}
}catch(Exceptione){
e.printStackTrace();
}
}
returnresultString;
}
publicstaticCloseableHttpClientcreateCustomClient(){
RequestConfigdefaultRequestConfig=RequestConfig.custom()
.setSocketTimeout(120*1000)
.setConnectTimeout(120*1000)
.setConnectionRequestTimeout(120*1000)
.setStaleConnectionCheckEnabled(true)
.build();
returnHttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
}
方法仅供参考,项目里面有现成的http请求,可以直接用!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。