关于钉钉机器人异常消息推送流程备忘录

开发的速卖通对接奇门自定义接口,数据安全出塔项目,因为项目需要部署到聚石塔中,但异常信息查看比较麻烦,不利于排查错误信息,所以考虑使用钉钉的机器人进行异常消息推送。

首先,你需要一个钉钉账号,其次你需要拉一个群

比如我选择创建一个项目群

创建完后有一条消息推送,这个是钉钉的机器人,点击它的头像,选择更多机器人

 

我选择的是自定义

 

 选择ip地址段,这里的设置相当于ip白名单,只有地址段内的ip才被允许推送消息

完成配置后会给一个链接地址,请复制此地址进行存储,这个就是你进行消息推送的地址

 

下面的链接是钉钉开放平台sdk的下载地址,使用sdk进行开发,比较快捷方便,文档上有开发案例

https://developers.dingtalk.com/document/resourcedownload/download-server-sdk?pnamespace=app

下面是自定义机器人接入的开发文档,我是在此基础上进行开发的,支持text,link,markdown格式的消息推送

 https://developers.dingtalk.com/document/app/custom-robot-access

接下来介绍项目中的使用,我创建的项目是springboot项目,思路是,建立一个全局的异常捕获机制,获取到异常信息,进行钉钉机器人异常消息推送

package com.text..control;

import com.text.utils.DingTalkUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;

/**
 * @description: 全局异常处理
 * @author: 
 * @date: 2021-07-15 8:48
 * @version: 1.0
 */
@ControllerAdvice
public class ExceptionHandler {


    @org.springframework.web.bind.annotation.ExceptionHandler(value=Exception.class)
    public void exceptionHandler(Exception e){
        //全局捕获异常,并进行钉钉异常消息推送
        DingTalkUtils.dingTalkSendException(e);
    }
}

请记得更换 DingTalkClient client = new DefaultDingTalkClient()中的链接地址,这个是上面复制的机器人推送地址

    public static void dingTalkSendException(Exception e){
        //得到异常棧的首个元素
        StackTraceElement  stackTrace = e.getStackTrace()[0];
        StringBuilder sb=new StringBuilder();
        sb.append("报错信息:"+e.getMessage()+"\n")
          .append("报错文件:"+stackTrace.getFileName()+"\n")
          .append("报错行号:"+stackTrace.getLineNumber()+"\n")
          .append("报错方法:"+stackTrace.getMethodName()+"\n")
          .append("报错Class:"+stackTrace.getClass()+"\n")
          .append("报错ClassName:"+stackTrace.getClassName());
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send");
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        request.setMsgtype("text");
        OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
        text.setContent(sb.toString());
        request.setText(text);
        OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();

        at.setIsAtAll(true);
        request.setAt(at);


        OapiRobotSendResponse response=null;
        try {
            response= client.execute(request);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        System.out.println("钉钉机器人异常通知返回信息:"+response.getBody());
    }

下面是速卖通自定义api接口中关于异常信息的处理

比如以下代码,我调取速卖通的某个接口,其接口api上有标明返回异常信息的结构体

        AliexpressCategoryRedefiningGetchildattributesresultbypostcateidandpathResponse rsp = client.execute(req, session);
        JSONObject bodyJson = JSONObject.parseObject(rsp.getBody());
        //速卖通返回的结构体需要进行重新整合,result外又包了一层需要去除
        JSONObject aliexpressResponse = bodyJson.getJSONObject("aliexpress_category_redefining_getchildattributesresultbypostcateidandpath_response");
        System.out.println(bodyJson);
        if(bodyJson.containsKey("error_response")){
            throw new RuntimeException(bodyJson.toString());
        }
        return  aliexpressResponse;
{
	"error_response":{
		"msg":"Remote service error",
		"code":50,
		"sub_msg":"非法参数",
		"sub_code":"isv.invalid-parameter"
	}
}

所以判断返回的结构体中是否包含error_response这个key,如果有抛一个异常

这个是效果图,这样的话不需要取查看奇门的日志才能定位问题了,其他异常信息正常抛就可以了 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值