java實(shí)現(xiàn)釘釘機(jī)器人消息推送的示例代碼
先建個(gè)釘釘群,并加好機(jī)器人






此時(shí),機(jī)器人已經(jīng)添加完畢,接下來(lái)編寫(xiě)我們連接機(jī)器人小哥的代碼
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 {
//釘釘機(jī)器人地址(配置機(jī)器人的webhook)
String dingUrl = "https://oapi.dingtalk.com/robot/send?access_token=............";
//是否通知所有人
boolean isAtAll = false;
//通知具體人的手機(jī)號(hào)碼列表
List<String> mobileList = Lists.newArrayList();
//釘釘機(jī)器人消息內(nèi)容
String content = "小哥,你好!";
//組裝請(qǐng)求內(nèi)容
String reqStr = buildReqStr(content, isAtAll, mobileList);
//推送消息(http請(qǐng)求)
String result = HttpUtil.postJson(dingUrl, reqStr);
System.out.println("result == " + result);
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 組裝請(qǐng)求報(bào)文
* @param content
* @return
*/
private static String buildReqStr(String content, boolean isAtAll, List<String> mobileList) {
//消息內(nèi)容
Map<String, String> contentMap = Maps.newHashMap();
contentMap.put("content", content);
//通知人
Map<String, Object> atMap = Maps.newHashMap();
//1.是否通知所有人
atMap.put("isAtAll", isAtAll);
//2.通知具體人的手機(jī)號(hào)碼列表
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);
}
}
運(yùn)行結(jié)果如下:
result == {"errmsg":"ok","errcode":0}
釘釘群顯示消息:

ok,簡(jiǎn)單的消息推送,這就完成了!
我們?cè)賮?lái)測(cè)試一下通知所有人和通知具體人
將isAtAll更改為true
//是否通知所有人 boolean isAtAll = true; //通知具體人的手機(jī)號(hào)碼列表 List<String> mobileList = Lists.newArrayList();

增加通知人號(hào)碼列表(注:isAtAll和mobileList 不能同時(shí)生效)
//是否通知所有人
boolean isAtAll = false;
//通知具體人的手機(jī)號(hào)碼列表
List<String> mobileList = Lists.newArrayList();
mobileList.add("182********");

再來(lái)測(cè)試一下特殊符號(hào)
換行標(biāo)識(shí)符
/**
* 換行標(biāo)識(shí)符
*/
private static final String NEWLINE = "\n";
//釘釘機(jī)器人消息內(nèi)容
//String content = "小哥,你好!";
StringBuffer sb = new StringBuffer();
sb.append("小哥,你好!")
.append(NEWLINE)
.append("看會(huì)書(shū)");
String content = sb.toString();

emoji圖片
先獲取emoji圖片的unicode編碼

編寫(xiě)代碼如下:
/**
* 蘋(píng)果unicode編碼
*/
private static final String APPLE = "\ud83c\udf4e";
//釘釘機(jī)器人消息內(nèi)容
//String content = "小哥,你好!";
StringBuffer sb = new StringBuffer();
sb.append("小哥,你好!")
.append(NEWLINE)
.append("看會(huì)書(shū)")
.append(NEWLINE)
.append("吃個(gè)").append(APPLE);
String content = sb.toString();

通常在我們的項(xiàng)目中,作為一些告警加入,方便且實(shí)用
很有意思的釘釘機(jī)器人,很多實(shí)用技巧,可以深入去探索一波!
更新于2019-12-05
很多小伙伴留言咨詢http請(qǐng)求,這邊給大家2個(gè)http請(qǐng)求代碼
1. maven項(xiàng)目
添加依賴
<!--糊涂工具--> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>4.0.12</version> </dependency>
http請(qǐng)求代碼
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項(xiàng)目
添加jar包
httpclient-xxx.jar
commons-logging-xxx.jar
http請(qǐng)求代碼
public static String postJson(String url, String body) {
// 創(chuàng)建Httpclient對(duì)象
CloseableHttpClient httpClient = createCustomClient();
CloseableHttpResponse response = null;
String resultString = null;
try {
// 創(chuàng)建Http Post請(qǐng)求
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/json");
if (body != null) {
httpPost.setEntity(new StringEntity(body, "utf-8"));
}
// 執(zhí)行http請(qǐng)求
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();
}
方法僅供參考,項(xiàng)目里面有現(xiàn)成的http請(qǐng)求,可以直接用!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)控制小數(shù)精度的方法
這篇文章主要介紹了Java實(shí)現(xiàn)控制小數(shù)精度的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Java之HashMap.values()轉(zhuǎn)List時(shí)的錯(cuò)誤和正確演示
這篇文章主要介紹了Java之HashMap.values()轉(zhuǎn)List時(shí)的錯(cuò)誤和正確演示,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
java實(shí)現(xiàn)坦克大戰(zhàn)小游戲
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)坦克大戰(zhàn)小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01
Spring?Boot項(xiàng)目獲取resources目錄下文件并返回給前端的方案
我們?cè)陧?xiàng)目中經(jīng)常碰到需要讀取固定文件的場(chǎng)景,如模板文件,一般做法是將文件放在resources目錄下,程序通過(guò)多種方式可以順利讀取文件,這篇文章主要給大家介紹了關(guān)于Spring?Boot項(xiàng)目獲取resources目錄下文件并返回給前端的相關(guān)資料,需要的朋友可以參考下2024-07-07
springboot項(xiàng)目獲取請(qǐng)求頭當(dāng)中的token的方法
本文主要介紹了springboot項(xiàng)目獲取請(qǐng)求頭當(dāng)中的token的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11
Java應(yīng)用打包后運(yùn)行需要注意編碼問(wèn)題
這篇文章主要介紹了 Java應(yīng)用打包后運(yùn)行需要注意編碼問(wèn)題的相關(guān)資料,需要的朋友可以參考下2016-12-12

