国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

如何使用java實(shí)現釘釘機器人消息推送功能

發(fā)布時(shí)間:2021-09-27 17:50 來(lái)源:億速云 閱讀:0 作者:小新 欄目: 開(kāi)發(fā)技術(shù)

小編給大家分享一下如何使用java實(shí)現釘釘機器人消息推送功能,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

先建個(gè)釘釘群,并加好機器人

此時(shí),機器人已經(jīng)添加完畢,接下來(lái)編寫(xiě)我們連接機器人小哥的代碼

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,簡(jiǎn)單的消息推送,這就完成了!

我們再來(lái)測試一下通知所有人和通知具體人

將isAtAll更改為true

//是否通知所有人boolean isAtAll = true;//通知具體人的手機號碼列表List<String> mobileList = Lists.newArrayList();

增加通知人號碼列表(注:isAtAll和mobileList 不能同時(shí)生效)

//是否通知所有人boolean isAtAll = false;//通知具體人的手機號碼列表List<String> mobileList = Lists.newArrayList();mobileList.add("182********");

再來(lái)測試一下特殊符號

換行標識符

/** * 換行標識符 */private static final String NEWLINE = "\n";//釘釘機器人消息內容//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";//釘釘機器人消息內容//String content = "小哥,你好!";StringBuffer sb = new StringBuffer();sb.append("小哥,你好!") .append(NEWLINE) .append("看會(huì )書(shū)") .append(NEWLINE) .append("吃個(gè)").append(APPLE);String content = sb.toString();

通常在我們的項目中,作為一些告警加入,方便且實(shí)用很有意思的釘釘機器人,很多實(shí)用技巧,可以深入去探索一波!

更新于2019-12-05

很多小伙伴留言咨詢(xún)http請求,這邊給大家2個(gè)http請求代碼

1. maven項目

添加依賴(lài)

<!--糊涂工具--><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.jarcommons-logging-xxx.jar

http請求代碼

public static String postJson(String url, String body) {  // 創(chuàng  )建Httpclient對象  CloseableHttpClient httpClient = createCustomClient();  CloseableHttpResponse response = null;  String resultString = null;  try {   // 創(chuàng  )建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(); }

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。

久久精品国产精品亚洲38| 国产精品成年片在线观看| JLZZJLZZ亚洲乱熟无码| 亚洲AV日韩AV成人AV| 亚洲精品无码你懂的| 无人视频免费观看免费视频|