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

钉钉机器人工具类

作者: 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;
        }
    }
}

相关文章

  • 钉钉机器人工具类

    工作经常用到钉钉的机器人通知,写了几次机器人的工具类,不过忘记记录了,要重新写有点麻烦,这里把钉钉机器人的工具代码...

  • jenkins 集成钉钉机器人通知

    公司使用钉钉做为公司内部的通讯工具,所以想通过Jenkins发布完成以后通过钉钉来通知大家,研究发现钉钉提供机器人...

  • bugly异常消息推送钉钉——内网穿透技术

    bugly的异常信息要推送到钉钉,不能直接推送到钉钉机器人,因为bugly推送到json格式跟钉钉机器人能接收的格...

  • jenkins集成钉钉机器人

    jenkins邮件通知不能及时的收到,所以采用了钉钉机器人 下载钉钉客户端,创建交流群并成为群主 创建钉钉机器人 ...

  • 钉钉机器人消息Python封装(开源)

    一、钉钉自定义机器人介绍 钉钉机器人是钉钉群的一个高级扩展功能,但使用起来却非常简单,只需要注册一个钉钉账号,就可...

  • GitLab-代码审核

    设置钉钉 GitLab 机器人 打开钉钉,在需要配置 GitLab 机器人的群中,点击【群设置】→【智能群助手】→...

  • 钉钉机器人

    python钉钉机器人 #ht...

  • Jenkins(十)钉钉通知

    1、Jenkins:系统管理-插件管理-搜索钉钉插件-安装钉钉插件 2、钉钉-添加群机器人 3、钉钉-复制webh...

  • Python_钉钉机器人

    一、打开钉钉群,添加自定义机器人,记住创建机器人的webhook即可。二、发送消息到钉钉群: 发送的消息类型参见钉...

  • python钉钉机器人发消息

    用于钉钉群,定时发消息之类的 1、创建钉钉机器人 2、创建机器人 3、创建成功,拿到webhook 4、创建一个钉...

网友评论

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

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