钉钉机器人告警小记

1、配置钉钉机器人

①安全设置常使用关键词

②使用outgoing机制,配置post地址和token,作为回调

 

 

 

 

2、使用钉钉机器人发送消息

自动化用例跑批通知、告警等

public static void sendMsg(String title, String message, String url, List<String> list) {
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        DingTalkClient client = new DefaultDingTalkClient(url);
        try {
            request.setMsgtype("text");
            OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
            text.setContent(title + message);
            request.setText(text);
            OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
//            at.setIsAtAll(true);
            at.setAtMobiles(list);
            request.setAt(at);
            client.execute(request, key, appSecret);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

 

 

 3、艾特钉钉机器人,接收消息进行处理

官方文档:https://open.dingtalk.com/document/robots/receive-message
请求地址与钉钉机器人的post地址一致
 1 @RestController
 2 @EnableAutoConfiguration
 3 @RequestMapping("/ding")
 4 @Slf4j
 5 public class DingAt {
 6     @CrossOrigin
 7     @RequestMapping(value = "/jenkins", method = RequestMethod.POST)
 8     @org.springframework.web.bind.annotation.ResponseBody
 9    public static void DingTalk(@RequestBody DingTalkChatVo dingTalkChatVo)  {
10         final String senderNick = dingTalkChatVo.getSenderNick();
11         log.info("[收到钉钉消息][{}][{}][{}]", senderNick, dingTalkChatVo.getContentMsg(), dingTalkChatVo.toJsonStr());
12                if (dingTalkChatVo.getContentMsg().equals("hi")){
13             DingdingControl.sendMsg("","你也hi","url");
14         }else if(dingTalkChatVo.getContentMsg().equals("测试")){
15             DingdingControl.sendMsg("","收到","url");
16         }
17     }

 

 






posted @ 2021-12-24 11:44  小町  阅读(308)  评论(0编辑  收藏  举报