工作经常用到钉钉的机器人通知,写了几次机器人的工具类,不过忘记记录了,

要重新写有点麻烦,这里把钉钉机器人的工具代码贴出来,后续再次用的时候直接查看。经常用到的地方:应用内存在异常情况报警,应用内任务运行状况通知...

支持普通钉钉消息发送

支持ActionCard消息发送

image.png

import java.util.List;

import com.google.common.collect.Lists;

import lombok.Data;

import lombok.Getter;

import lombok.extern.slf4j.Slf4j;

import org.springframework.http.ResponseEntity;

import org.springframework.web.client.RestTemplate;

/**

* @author : aihe

* 使用场景:

* 功能描述:自定义机器人接入:https://developers.dingtalk.com/document/app/custom-robot-access/title-zob-eyu-qse

*/

@Slf4j

public class RobotUtils {

private static RestTemplate restTemplate = new RestTemplate();

public static void main(String[] args) {

sendTextMsg(SupportRobotEnum.CESHI.url

, "测试内容发送"

, Lists.newArrayList("xxx")

);

sendActionCardMsg(SupportRobotEnum.CESHI.url

, "测试内容标题",

"测试内容"

, true

, Lists.newArrayList(

//RobotBtn.buildBtn("同意去百度", "http://www.baidu.com"),

RobotBtn.buildBtn("同意去QQ", "http://www.qq.com")

)

);

}

/**

* {

* "msgtype": "text",

* "text": {

* "content": "我就是我, @150XXXXXXXX 是不一样的烟火"

* },

* "at": {

* "atMobiles": [

* "150XXXXXXXX"

* ],

* "isAtAll": false

* }

* }

*/

public static void sendTextMsg(String url, String content, List atPerson) {

RobotMsg robotMsg = new RobotMsg();

robotMsg.setMsgtype("text");

RobotAt robotAt = new RobotAt();

robotAt.setAtAll(false);

if (atPerson != null && atPerson.size() > 0) {

robotAt.setAtMobiles(atPerson);

}

robotMsg.setAt(robotAt);

RobotText robotText = new RobotText();

robotText.setContent(content);

robotMsg.setText(robotText);

ResponseEntity responseEntity = restTemplate.postForEntity(url, robotMsg, String.class);

System.out.println(responseEntity.getBody());

}

/**

* @param url 机器人地址

* @param title actionCard标题

* @param text 缩略内容

* @param vertical 按钮方向

* @param btns 按钮内容

*/

public static void sendActionCardMsg(String url, String title, String text, Boolean vertical, List btns) {

RobotMsg robotMsg = new RobotMsg();

robotMsg.setMsgtype("actionCard");

RobotAt robotAt = new RobotAt();

robotAt.setAtAll(false);

RobotActionCard robotActionCard

= new RobotActionCard();

robotActionCard.setTitle(title);

robotActionCard.setText(text);

robotActionCard.setBtnOrientation((vertical == null || vertical) ? "0" : "1");

robotActionCard.setBtns(btns);

robotMsg.setActionCard(robotActionCard);

ResponseEntity responseEntity = restTemplate.postForEntity(url, robotMsg, String.class);

System.out.println(responseEntity.getBody());

}

public enum SupportRobotEnum {

/**

* 测试机器人群

*/

CESHI("测试机器人群","");

@Getter

private String desc;

@Getter

private String url;

SupportRobotEnum(String desc, String url) {

this.desc = desc;

this.url = url;

}

}

@Data

public static class RobotAt {

public List atMobiles;

public boolean isAtAll;

}

@Data

public static class RobotMsg {

public String msgtype;

public RobotAt at;

/**

* 普通文字消息类型消息

*/

public RobotText text;

/**

* actionCard类型消息时支持

*/

public RobotActionCard actionCard;

}

@Data

public static class RobotText {

public String content;

}

@Data

public static class RobotActionCard {

public String title;

public String text;

public String hideAvatar;

public String btnOrientation;

public List btns;

}

@Data

public static class RobotBtn {

public String title;

public String actionURL;

public static RobotBtn buildBtn(String title, String actionUrl) {

RobotBtn robotBtn = new RobotBtn();

robotBtn.setActionURL(actionUrl);

robotBtn.setTitle(title);

return robotBtn;

}

}

}

钉钉机器人怎么用java测试_钉钉机器人工具类相关推荐

  1. Java学习总结:58(Collections工具类)

    Collections工具类 Java提供了一个集合的工具类--Collections,这个工具类可以实现List.Set.Map集合的操作.Collections类的常用方法如下: No. 方法 类 ...

  2. java配置文件工具类,java项目加载配置文件的工具类

    java项目加载配置文件的工具类 package com.loadproperties; import java.io.IOException; import java.io.InputStream; ...

  3. Java实现Google的S2算法工具类

    WGS84坐标系 GCJ02坐标系 BD09坐标系的各种转换 WGS84坐标系 GCJ02坐标系 BD09坐标系的各种转换 Google S2 经纬度 转 CellId 经纬度 转 cellToken ...

  4. Java 图片添加数字暗水印工具类

    Java 图片添加数字暗水印工具类. package cnki.thesis.common.utils;import org.opencv.core.*;import java.util.ArrayL ...

  5. Java时间戳与日期格式转换工具类

    Java时间戳与日期格式转换工具类 在做web开发时,有时候需要用到时间戳,在前台进行日期转换,从数据库中取出来是日期格式的,这里记录下使用的转换工具类(这些都是静态方法,通过类名.方法的形式即可调用 ...

  6. java获取客户端的IP地址工具类

    java获取客户端的IP地址工具类 import java.net.InetAddress; import java.net.UnknownHostException;import javax.ser ...

  7. Java判断不为空的工具类总结

    Java判断不为空的工具类总结 1.Java判断是否为空的工具类,可以直接使用.包含,String字符串,数组,集合等等. 1 package com.bie.util; 2 3 import jav ...

  8. (10)Java泛型-Map集合-集合框架工具类-可变参数-静态导入

    -- 部分1.5新特性Java泛型-Map集合-集合框架工具类 泛型 概述: JDK1.5版本以后出现的新特性,用于解决安全问题,是一个类型安全机制. 对于泛型可以这样理解: 没有使用泛型时,只要是对 ...

  9. 机器人测钢卷直径_如何引导机器人对钢卷定位焊接?秘密就在这里

    原标题:如何引导机器人对钢卷定位焊接?秘密就在这里 随着钢铁工业自动化的发展,我国人工成本的不断提高,焊接机器人快速寻位焊接方式必然会逐步代替传统手工焊接方式,完成冷轧厂钢卷卷芯内圈带头的焊接. 看似 ...

最新文章

  1. 看到这些代码,我自叹不如!!!
  2. 用python画图-用python进行简单的画图操作
  3. Zabbix监控Nginx性能状态
  4. mysql架构 视频_企业常见MySQL架构应用实战(高可用集群系统+调优经验)视频课程...
  5. 中外计算机百科知识,计算机百科知识.doc
  6. linspace函数matlab_从零开始的matlab学习笔记——(29)泰勒逼近函数
  7. centos7 如何安装部署k8s_如何在centos7上安装FreeIPA的客户端
  8. 高颜值免费在线SCI绘图工具支持导出PPT格式图片和图片分享功能
  9. 什么时候出python4_什么?Python4要来了?快来看看Python之父怎么说
  10. 每天固定往一个银行卡存入100元,5年之后会有多大变化?有人能坚持吗?
  11. Ubuntu 编译安装ffmpeg,mplayer,x264全教程支持VDPAU(高清硬解)
  12. 错误日志分析(dSYM)-2016
  13. 机器学习中的数学——距离定义(一):欧几里得距离(Euclidean Distance)
  14. 计算机科学导论实验报告,上海电力学院计算机导论实验报告.docx
  15. nodejs爬虫实战(一):抽屉新热榜
  16. 暴走英雄坛服务器维护到什么时候,暴走英雄坛在4月25日更新了什么 最新维护内容介绍...
  17. 拼多多 果园api分析 拼多多 anticontent 解密
  18. 【Python自然语言处理】读书笔记:第一章:语言处理与Python
  19. 数据集成平台的特点(Oracle service bus)
  20. 如何设置编辑QTableView单元格时的字体颜色

热门文章

  1. Error: spawn cmd ENOENT at Process.ChildProcess._handle.onexit
  2. 分享一个小玩意 真菌UNITE物种分类数据库
  3. 银保监会计算机类专业知识,国家公务员局2019中国银保监会国考内容:计算机类专业知识...
  4. 智能音箱音效哪个好_2018最火4款智能音箱横评:哪款性价比最高?
  5. 谈谈以前端的角度出发做好seo需要做什么?
  6. ORA-12899: value too large for column 问题解决
  7. 微信实名认证相关问题
  8. 史上最硬核全套Java视频教程(学习路线+视频+配套资料)
  9. IOS根据屏幕尺寸判断是否为iphone6plus尺寸
  10. nginx隐藏Vary头信息