先建個釘釘群,并加好機器人

此時,機器人已經添加完畢,接下來編寫我們連接配接機器人小哥的代碼
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;
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();
}
}
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}
釘釘群顯示消息:
ok,簡單的消息推送,這就完成了!
我們再來測試一下通知所有人和通知具體人
将isAtAll更改為true
//是否通知所有人
boolean isAtAll = true;
//通知具體人的手機号碼清單
List mobileList = Lists.newArrayList();
增加通知人号碼清單(注:isAtAll和mobileList 不能同時生效)
//是否通知所有人
boolean isAtAll = false;
//通知具體人的手機号碼清單
List 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編碼
編寫代碼如下:
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項目
添加依賴
cn.hutool
hutool-all
4.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請求,可以直接用!
以上就是本文的全部内容,希望對大家的學習有所幫助,也希望大家多多支援我們。
時間: 2020-01-14