此时,机器人已经添加完毕,接下来编写我们连接机器人小哥的代码
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请求,可以直接用!
相关学习推荐:java基础教程
以上就是学习java实现钉钉机器人消息推送的示例代码的详细内容。