學習java實作釘釘機器人訊息推播的範例程式碼
先建造一個釘子群,並加上好機器人
##此時,機器人已經添加完畢,接下來編寫我們連接機器人小哥的程式碼
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實作釘釘機器人訊息推播的範例程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undress AI Tool
免費脫衣圖片

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

testthepdfinanotherapptoderineiftheissueiswiththefileoredge.2.enablethebuilt inpdfviewerbyTurningOff“ eflblyopenpenpenpenpenpdffilesexternally”和“ downloadpdffiles” inedgesettings.3.clearbrowsingdatainclorwearbrowsingdataincludingcookiesandcachedcachedfileresteroresoreloresorelorsolesoresolesoresolvereresoreorsolvereresoreolversorelesoresolvererverenn

Importjava.ioandjava.net.SocketforI/Oandsocketcommunication.2.CreateaSocketobjecttoconnecttotheserverusinghostnameandport.3.UsePrintWritertosenddataviaoutputstreamandBufferedReadertoreadserverresponsesfrominputstream.4.Usetry-with-resourcestoautomati

容器化Java應用:創建Dockerfile,使用基礎鏡像如eclipse-temurin:17-jre-alpine,複製JAR文件並定義啟動命令,通過dockerbuild構建鏡像並用dockerrun測試本地運行。 2.推送鏡像到容器註冊表:使用dockertag標記鏡像並推送到DockerHub等註冊表,需先登錄dockerlogin。 3.部署到Kubernetes:編寫deployment.yaml定義Deployment,設置副本數、容器鏡像和資源限制,編寫service.yaml創建

VSCode中可通過快捷鍵快速切換面板與編輯區。要跳轉至左側資源管理器面板,使用Ctrl Shift E(Windows/Linux)或Cmd Shift E(Mac);返回編輯區可用Ctrl `或Esc或Ctrl 1~9。相比鼠標操作,鍵盤快捷鍵更高效且不打斷編碼節奏。其他技巧包括:Ctrl KCtrl E聚焦搜索框,F2重命名文件,Delete刪除文件,Enter打開文件,方向鍵展開/收起文件夾。

runthewindowsupdatetrubloubleshooterviaSettings>更新&安全> is esseShootsoAtomationfixCommonissues.2.ResetWindowSupDateComponentsByStoppingRealatedServices,RenamingTheSoftWaredWaredWaredSoftwaredSistribution andCatroot2Folders,intrestrestartingthertingthertingtherserviceSteStoceTocle

JavaserializationConvertSanObject'SstateIntoAbyTeSteAmForStorageorTransermission,andDeserializationReconstructstheObjectStheObjectFromThstream.1.toenableserialization,aclassMustimustimplementTheSerializableizableface.2.UseObjectObjectObjectObjectOutputputputputputtreamToserialializeanobectizeanobectementeabectenobexpent,savin

AwhileloopinJavarepeatedlyexecutescodeaslongastheconditionistrue;2.Initializeacontrolvariablebeforetheloop;3.Definetheloopconditionusingabooleanexpression;4.Updatethecontrolvariableinsidethelooptopreventinfinitelooping;5.Useexampleslikeprintingnumber

NumPy是Python中進行科學計算的核心庫,擅長處理線性代數運算,提供高效的ndarray數組和numpy.linalg模塊中的函數。 1.使用np.linalg.solve(A,b)可求解線性方程組Ax=b,得到解向量x;2.矩陣轉置通過A.T實現;3.矩陣乘法可用np.dot(A,B)或A@B;4.矩陣逆通過np.linalg.inv(A)計算,需確保矩陣可逆;5.行列式由np.linalg.det(A)給出;6.特徵值與特徵向量通過np.linalg.eig(A)求得,特徵向量已歸一化;
