java推送到 钉钉用户_javaweb利用钉钉机器人向钉钉群推送消息(解决中文乱码)...
可以参考 官方文档:https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.karFPe&treeId=257&articleId=105735&docType=1
1.新建一个群。添加自定义机器人。获取自定义机器人webhook。
2.封装消息实体类。
packagecom.thinkgem.wlw.modules.midea.dingTalkTest;importcom.alibaba.fastjson.JSON;importcom.thinkgem.wlw.common.utils.StringUtils;importjava.util.HashMap;importjava.util.List;importjava.util.Map;/*** @Author: zhouhe
* @Date: 2019/6/20 17:25*/
public classTextMessage {privateString text;private ListatMobiles;private booleanisAtAll;publicTextMessage(String text) {this.text =text;
}publicString getText() {returntext;
}public voidsetText(String text) {this.text =text;
}public ListgetAtMobiles() {returnatMobiles;
}public void setAtMobiles(ListatMobiles) {this.atMobiles =atMobiles;
}public booleanisAtAll() {returnisAtAll;
}public void setIsAtAll(booleanisAtAll) {this.isAtAll =isAtAll;
}publicString toJsonString() {
Map items = newHashMap();
items.put("msgtype", "text");
Map textContent = newHashMap();if(StringUtils.isBlank(text)) {throw new IllegalArgumentException("text should not be blank");
}
textContent.put("content", text);
items.put("text", textContent);
Map atItems = newHashMap();if (atMobiles != null && !atMobiles.isEmpty()) {
atItems.put("atMobiles", atMobiles);
}if(isAtAll) {
atItems.put("isAtAll", isAtAll);
}
items.put("at", atItems);returnJSON.toJSONString(items);
}
}
3.封装发送消息结果实体类
packagecom.thinkgem.wlw.modules.midea.dingTalkTest;importcom.alibaba.fastjson.JSON;importjava.util.HashMap;importjava.util.Map;/*** @Author: zhouhe
* @Date: 2019/6/20 17:26*/
public classSendResult {private booleanisSuccess;privateInteger errorCode;privateString errorMsg;public booleanisSuccess() {returnisSuccess;
}public void setIsSuccess(booleanisSuccess) {this.isSuccess =isSuccess;
}publicInteger getErrorCode() {returnerrorCode;
}public voidsetErrorCode(Integer errorCode) {this.errorCode =errorCode;
}publicString getErrorMsg() {returnerrorMsg;
}public voidsetErrorMsg(String errorMsg) {this.errorMsg =errorMsg;
}
@OverridepublicString toString() {
Map items = new HashMap();
items.put("errorCode", errorCode);
items.put("errorMsg", errorMsg);
items.put("isSuccess", isSuccess);returnJSON.toJSONString(items);
}
}
4.发送消息通用类
packagecom.thinkgem.wlw.modules.midea.dingTalkTest;importcom.alibaba.fastjson.JSONObject;importorg.apache.http.HttpResponse;importorg.apache.http.HttpStatus;importorg.apache.http.client.HttpClient;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.entity.StringEntity;importorg.apache.http.impl.client.HttpClients;importorg.apache.http.util.EntityUtils;importjava.io.IOException;/*** @Author: zhouhe
* @Date: 2019/6/20 17:26*/
public classRobotClient {public static SendResult send(String webhook, TextMessage message) throwsIOException {
HttpClient httpclient=HttpClients.createDefault();
HttpPost httppost= newHttpPost(webhook);
httppost.addHeader("Content-Type", "application/json; charset=utf-8");
StringEntity se= new StringEntity(message.toJsonString(), "utf-8");
httppost.setEntity(se);
SendResult sendResult= newSendResult();
HttpResponse response=httpclient.execute(httppost);if (response.getStatusLine().getStatusCode() ==HttpStatus.SC_OK) {
String result=EntityUtils.toString(response.getEntity());
JSONObject obj=JSONObject.parseObject(result);
Integer errcode= obj.getInteger("errcode");
sendResult.setErrorCode(errcode);
sendResult.setErrorMsg(obj.getString("errmsg"));
sendResult.setIsSuccess(errcode.equals(0));
}returnsendResult;
}
}
5.发送文本消息类
packagecom.thinkgem.wlw.modules.midea.dingTalkTest;importgroovy.util.logging.Slf4j;importjava.util.ArrayList;/*** @Author: zhouhe
* @Date: 2019/6/20 17:32*/@Slf4jpublic classSendTextMessage {//机器人消息token
public static String WEBHOOK_TOKEN = "https://oapi.dingtalk.com/robot/send?access_token=xxx";private static RobotClient robot = newRobotClient();/*** 发送普通文本消息
*
*@parammessage
*@return
*/
public staticSendResult send(String message) {
TextMessage textMessage= newTextMessage(message);
SendResult sendResult= null;try{
sendResult=robot.send(WEBHOOK_TOKEN, textMessage);
}catch(Exception e) {//log.error("===> send robot message error:", sendResult);
}returnsendResult;
}/*** 发送文本消息 可以@部分人
*
*@parammessage
*@paramatMobiles 要@人的电话号码 ArrayList
*@return
*/
public static SendResult sendWithAt(String message, ArrayListatMobiles) {
TextMessage textMessage= newTextMessage(message);
SendResult sendResult= null;
textMessage.setAtMobiles(atMobiles);try{
sendResult=robot.send(WEBHOOK_TOKEN, textMessage);
}catch(Exception e) {//log.error("===> send robot message atPeople error:", sendResult);
}returnsendResult;
}/*** 发送文本消息 并@所有人
*
*@parammessage
*@return
*/
public staticSendResult sendWithAtAll(String message) {
TextMessage textMessage= newTextMessage(message);
SendResult sendResult= null;
textMessage.setIsAtAll(false);try{
sendResult=robot.send(WEBHOOK_TOKEN, textMessage);
}catch(Exception e) {//log.error("===> send robot message atAll error:", sendResult);
}returnsendResult;
}
}
6.调用
packagecom.thinkgem.wlw.modules.midea.dingTalk;importcom.thinkgem.wlw.modules.midea.dingTalkTest.SendTextMessage;importjava.util.HashMap;importjava.util.Map;/*** @Author: zhouhe
* @Date: 2019/6/20 14:52*/
public classSendMessage {public static voidmain(String[] args){
SendTextMessage.sendWithAtAll("我是测试的!!!");
}
}
效果(钉钉消息):
@Slf4j maven依赖:
org.codehaus.groovy
groovy-all
2.4.5
注:如果钉钉机器人中设置了关键字,那么发送的消息内容必须包含关键字才行,否则发送不出去。详见钉钉开发文档
java推送到 钉钉用户_javaweb利用钉钉机器人向钉钉群推送消息(解决中文乱码)...相关推荐
- java txt中文乱码,JAVA读取TXT文件 可解决中文乱码问题
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.I ...
- java web 中有效解决中文乱码问题-pageEncoding与charset区别, response和request的setCharacterEncoding 区别
java web 中有效解决中文乱码问题-pageEncoding与charset区别, response和request的setCharacterEncoding 区别 参考文章: (1)java ...
- java解压多目录Zip文件(解决中文乱码问题)--转载
原文地址:http://zhangyongbo.iteye.com/blog/1749439 import java.io.BufferedOutputStream; import java.io.F ...
- Java 按行读写文件(解决中文乱码)
import java.io.*;public class EmployeeTest{EmployeeTest(){}// 按行读取文件public void readFile01(){try {Fi ...
- JAVA之NIO按行读写大文件,完美解决中文乱码问题
JAVA之NIO按行读写大文件,完美解决中文乱码问题 参考文章: (1)JAVA之NIO按行读写大文件,完美解决中文乱码问题 (2)https://www.cnblogs.com/jpfss/p/89 ...
- java读取.properties文件及解决中文乱码问题
java读取.properties文件及解决中文乱码问题 参考文章: (1)java读取.properties文件及解决中文乱码问题 (2)https://www.cnblogs.com/helloq ...
- Java HttpURLConnection模拟请求Rest接口解决中文乱码问题
Java HttpURLConnection模拟请求Rest接口解决中文乱码问题 参考文章: (1)Java HttpURLConnection模拟请求Rest接口解决中文乱码问题 (2)https: ...
- java 中文乱码过滤器_JAVA中文乱码过滤器(用java过滤器解决中文乱码)V0422 整理版...
JAVA中文乱码过滤器(用java过滤器解决中文乱码)V0422 整理版 不用再担心java中文乱码问题,一步到位,一次性解决.更方便更快捷. web-xml配置 EncodingFilter com ...
- java zip解压 中文_java解压ZIP 解决中文乱码 (GBK和UTF-8)
java解压ZIP 解决中文乱码 (GBK和UTF-8) 工具使用 : zip4j GitHub : zip4j 版本 : 2.2.8 Maven : net.lingala.zip4j zip4j ...
- java 过滤器 中文_java中Filter过滤器解决中文乱码办法
java中Filter过滤器解决中文乱码办法 发布时间:2020-04-07 10:19:09 来源:亿速云 阅读:16 作者:小新 这篇文章主要为大家详细介绍了java中Filter过滤器解决中文乱 ...
最新文章
- 如何理解卷积神经网络中的1*1卷积
- 理解一下jQuery.extend()和jQuery.fn.extend()方法
- hdu2438 三分
- 安装 PHP Memcache 扩展
- mysql-bin.index找不到_MySQL不能启动 mysql-bin.index' not found (Errcode: 13)
- Spring集成Quartz定时器
- 无偏估计的数学证明和分析
- 服务器运维 考什么证书,腾讯云服务器运维高级工程师认证(TCP)证书有效期、考试内容、费用...
- Using RCU‘s CPU Stall Detector(待更新)
- HBase学习(四) HBase API操作
- 一般的度量空间上开集的构造
- SketchUp: Modeling Exteriors from Photos SketchUp:从照片建模外部 Lynda课程中文字幕
- must be of typexxx解决方法
- 英伟达显卡与CUDA
- 计算机科学数学基础及教材推荐(转)
- 什么是示波器?示波器的使用方法图解
- QSS 控件背景渐变色
- 国际认可度最高的验厂:BSCI
- power bi导入文件夹_从Power BI Desktop中的PDF文件导入数据
- 标准体制中的 GB8567——88