Home> Java> javaTutorial> body text

Learn Java to implement sample code for DingTalk robot message push

coldplay.xixi
Release: 2020-08-13 17:16:42
forward
3126 people have browsed it

Learn Java to implement sample code for DingTalk robot message push

First create a DingTalk group and add robots

Learn Java to implement sample code for DingTalk robot message push

Learn Java to implement sample code for DingTalk robot message push

Learn Java to implement sample code for DingTalk robot message push

Learn Java to implement sample code for DingTalk robot message push

Learn Java to implement sample code for DingTalk robot message push

Learn Java to implement sample code for DingTalk robot message push

#At this point, the robot has been added. Next, write the code for us to connect to the robot brother

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); } }
Copy after login

The running results are as follows:

result == {"errmsg":"ok","errcode":0}
Copy after login

DingTalk group display message:


Learn Java to implement sample code for DingTalk robot message push

ok, simple message push, that’s it!

Let’s test notifying everyone and notifying specific people

Change isAtAll to true

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

Learn Java to implement sample code for DingTalk robot message push

Add the notifier number list(Note: isAtAll and mobileList cannot take effect at the same time)

//是否通知所有人 boolean isAtAll = false; //通知具体人的手机号码列表 List mobileList = Lists.newArrayList(); mobileList.add("182********");
Copy after login

Learn Java to implement sample code for DingTalk robot message push

Let’s test the special Symbol

Line break identifier

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

Learn Java to implement sample code for DingTalk robot message push

##emoji picture


Get the unicode encoding of the emoji image first


Learn Java to implement sample code for DingTalk robot message push
Write the code as follows:

/** * 苹果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();
Copy after login

Learn Java to implement sample code for DingTalk robot message pushUsually in our projects, it is added as some alarms, which is convenient and practical

Very interesting DingTalk robot, with many practical skills, you can explore it in depth!


Updated on 2019-12-05

Many friends have left messages asking about http requests. Here are 2 http request codes for you

1. maven project

Add dependency

  cn.hutool hutool-all 4.0.12 
Copy after login

http request code

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; }
Copy after login

2. Non-maven project

Add jar package

httpclient-xxx.jar

commons-logging-xxx.jar

http request code

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(); }
Copy after login

The method is for reference only, there are ready-made ones in the project http request can be used directly!

Related learning recommendations:
java basic tutorial

The above is the detailed content of Learn Java to implement sample code for DingTalk robot message push. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!