美文网首页
钉钉机器人工具类

钉钉机器人工具类

作者: Real_man | 来源:发表于2021-02-22 09:58 被阅读0次

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

    • 支持普通钉钉消息发送
    • 支持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<String> 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<String> 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<RobotBtn> 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<String> 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<String> 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<RobotBtn> 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;
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:钉钉机器人工具类

          本文链接:https://www.haomeiwen.com/subject/xljhfltx.html