我们在工作中钉钉办公需要一些提醒或者通知,需要用到钉钉机器人来代替我们完成一些重复性的工作,看到钉钉有个机器人的自定义文档。
https://developers.dingtalk.com/document/robots/custom-robot-access?spm=ding_open_doc.document.0.0.6284657351Hh8K#topic-2026027
如何实现自定义机器人?
1.首先在钉钉群中添加机器人获取webhook地址
注意这里 :
如果选择自定义关键字 在内容的开头一定要和自定义关键字一样
如:关键字设置为:通知
那么内容的开头一定是:通知XXXXX
2.代码实现
1.搭建springboot项目
使用RestTemplate发送HTTP请求
packagecom.bky.dingding; importorg.apache.http.client.HttpClient; importorg.apache.http.client.config.RequestConfig; importorg.apache.http.config.Registry; importorg.apache.http.config.RegistryBuilder; importorg.apache.http.conn.socket.ConnectionSocketFactory; importorg.apache.http.conn.socket.PlainConnectionSocketFactory; importorg.apache.http.conn.ssl.SSLConnectionSocketFactory; importorg.apache.http.impl.client.HttpClientBuilder; importorg.apache.http.impl.conn.PoolingHttpClientConnectionManager; importorg.apache.http.ssl.SSLContextBuilder; importorg.springframework.context.annotation.Bean; importorg.springframework.context.annotation.Configuration; importorg.springframework.http.client.ClientHttpRequestFactory; importorg.springframework.http.client.HttpComponentsClientHttpRequestFactory; importorg.springframework.web.client.RestTemplate; importjavax.net.ssl.SSLContext; publicclassRestTemplateConfig { /*** 服务器返回数据(response)的时间*/privatestaticfinalIntegerREAD_TIME_OUT=6000; /*** 连接上服务器(握手成功)的时间*/privatestaticfinalIntegerCONNECT_TIME_OUT=6000; publicRestTemplaterestTemplate(){ ClientHttpRequestFactoryrequestFactory=newHttpComponentsClientHttpRequestFactory(httpClient()); returnnewRestTemplate(requestFactory); } publicHttpClienthttpClient(){ //默认证书有效SSLConnectionSocketFactorysslConnectionSocketFactory=SSLConnectionSocketFactory.getSocketFactory(); SSLContextsslContext=null; try { //信任所有的SSL证书sslContext=SSLContextBuilder.create().setProtocol(SSLConnectionSocketFactory.SSL) .loadTrustMaterial((x, y) ->true).build(); } catch (Exceptione) { e.printStackTrace(); } if (sslContext!=null) { sslConnectionSocketFactory=newSSLConnectionSocketFactory(sslContext); } // 支持HTTP、HTTPSRegistry<ConnectionSocketFactory>registry=RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.getSocketFactory()) .register("https", sslConnectionSocketFactory) .build(); PoolingHttpClientConnectionManagerconnectionManager=newPoolingHttpClientConnectionManager(registry); connectionManager.setMaxTotal(200); connectionManager.setDefaultMaxPerRoute(100); connectionManager.setValidateAfterInactivity(2000); RequestConfigrequestConfig=RequestConfig.custom() // 服务器返回数据(response)的时间,超时抛出read timeout .setSocketTimeout(READ_TIME_OUT) // 连接上服务器(握手成功)的时间,超时抛出connect timeout .setConnectTimeout(CONNECT_TIME_OUT) // 从连接池中获取连接的超时时间,超时抛出ConnectionPoolTimeoutException .setConnectionRequestTimeout(1000) .build(); returnHttpClientBuilder.create().setDefaultRequestConfig(requestConfig).setConnectionManager(connectionManager).build(); } } ```javapublicclassDingRobart { RestTemplaterestTemplate; "${dingding.robart.webhook}") (privateStringURL; privateReqBodyreqBody=newReqBody(newAt(null, true), newText("新的一天,大家发一下日报,元气满满"), "text"); //private ReqBody reqBody1 = new ReqBody(new At(null, true), new Text("工作一周辛苦了,大家发一下周报"), "text");//private ReqBody reqBody2 = new ReqBody(new At(null, true), new Text("大家好!"), "text");//private ReqBody reqBody3 = new ReqBody(new At(null, true), new Text("大家早上好,开始努力搬砖吧!"), "text");//cron="0 00 9 ? * MON-FRI") (publicJSONObjectribaoEveryDay() { HttpHeadersheaders=newHttpHeaders(); headers.add("Content-Type", "application/json"); System.out.println("开始日报"); StringrespBody=restTemplate.postForObject(URL, reqBody, String.class); returnJSONObject.parseObject(respBody); }
写完之后我们启动服务或者打成jar包就好了
3.效果