DevOps 运维提醒 钉钉 机器人 群通知

应用系统在碰到运行异常等情况时,发送预警消息@群内相关人员,可以及时发出问题,观察对问题的响应情况

 

1. 创建一个群(可以是公司内部群)

2. 添加机器人

 

 

 

 

 

 

 

 

 

 

 

 

 

自定义机器人安全设置: https://open.dingtalk.com/document/robots/customize-robot-security-settings

设置说明:https://open.dingtalk.com/document/org/application-types

 

 

{
    "at": {
        "atMobiles":[
            "手机号"
        ],
        "atUserIds":[
            ""
        ],
        "isAtAll": false
    },
    "text": {
        "content":"【业务报警】接口发生异常,请及时处理,如果加了自定义关键词,内容必须包括关键词,消息才能发出"
    },
    "msgtype":"text"
}

 

 

 

https://open.dingtalk.com/document/robots/push-message

https://open.dingtalk.com/document/group/custom-robot-access

https://open.dingtalk.com/document/resourcedownload/download-server-sdk

DT研究院 https://gitee.com/dt_research_institute

 

代码实现:

package com.vipsoft.dingtalk.rpc;

import com.vipsoft.dingtalk.entity.dingtalk.request.RobotMessage; 
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 需要在启动项中添加 @EnableFeignClients(basePackages = {"com.vipsoft"})
 */
@FeignClient(name = "vipsoft-dingtalk", url = "${dingtalk.rebot-url}")
public interface IDingtalkService {


    /**
     * 发送预警信息
     * SendSmsInfo.TemplateParam = 文本
     * @return
     */
    @PostMapping(value = "/robot/send")
    String sendMessage(@RequestParam Map<String, Object> headerParam, @RequestBody RobotMessage param); 
}

RobotMessage

package com.vipsoft.dingtalk.entity.dingtalk.request;

import java.io.Serializable;

public class RobotMessage implements Serializable {
    private String msgtype;

    private RobotAtInfo at;

    private RobotContentInfo text;

    public String getMsgtype() {
        return msgtype;
    }

    public void setMsgtype(String msgtype) {
        this.msgtype = msgtype;
    }

    public RobotAtInfo getAt() {
        return at;
    }

    public void setAt(RobotAtInfo at) {
        this.at = at;
    }

    public RobotContentInfo getText() {
        return text;
    }

    public void setText(RobotContentInfo text) {
        this.text = text;
    }
}

RobotAtInfo

package com.vipsoft.dingtalk.entity.dingtalk.request;

import java.io.Serializable;
import java.util.List;

public class RobotAtInfo implements Serializable {

    private List<String> atMobiles;

    public List<String> getAtMobiles() {
        return atMobiles;
    }

    public void setAtMobiles(List<String> atMobiles) {
        this.atMobiles = atMobiles;
    }
}

RobotContentInfo

package com.vipsoft.dingtalk.entity.dingtalk.request;

import java.io.Serializable; 

public class RobotContentInfo implements Serializable {

    private String content;

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

Test

@SpringBootTest
class VipsoftDingTalkTests {
    Logger logger = LoggerFactory.getLogger(this.getClass());
 
    @Autowired
    IDingtalkService dingtalkService;

    @Test
    void sendMessage() {
        String result = "";
        Long timestamp = System.currentTimeMillis();
        List<String> phoneNoList = new ArrayList<>();
        String[] phoneNoStr = "25988888888,35988888888".split(",");
        for (String phoneNo : phoneNoStr) {
            phoneNoList.add(phoneNo);
        }
        String content = "【业务预警】 调用开放平台,推送消息接口异常";

        if (ObjectUtil.isNotEmpty(phoneNoList)) {
            Map<String, Object> map = new HashMap<>();
            map.put("access_token", "2471310a60b56329c47c0e5f202af16b89XXXXXXXXXXXXXXXXXX");
            map.put("timestamp", timestamp);
            map.put("sign", sign(timestamp));
            
            RobotAtInfo atInfo=new RobotAtInfo();
            atInfo.setAtMobiles(phoneNoList);
            RobotContentInfo contentInfo=new RobotContentInfo();
            contentInfo.setContent(content);
            RobotMessage param=new RobotMessage();
            param.setMsgtype("text");
            param.setAt(atInfo);
            param.setText(contentInfo);
            result = dingtalkService.sendMessage(map, param);
        }
        logger.info(result);

    }

    public String sign(Long timestamp) {
        try {
            String appSecret = "SECfd5138XXXXXXXXXXXXXXXXXXXXXXXX";
            String stringToSign = timestamp + "\n" + appSecret;
            Mac mac = Mac.getInstance("HmacSHA256");
            mac.init(new SecretKeySpec(appSecret.getBytes("UTF-8"), "HmacSHA256"));
            byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
            return new String(Base64.encodeBase64(signData));
        } catch (Exception ex) {
            return "";
        }
    }
}

 

posted @ 2022-03-24 10:07  VipSoft  阅读(248)  评论(0编辑  收藏  举报