钉钉机器人怎么用java测试_钉钉机器人工具类

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

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

支持普通钉钉消息发送

支持ActionCard消息发送

4997d276e05f

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;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值