1. 程式人生 > >自定義釘釘機器人報警

自定義釘釘機器人報警

為了埋點實時監控業務,簡單的實現了釘釘機器人報警,釘釘開發官方文件點選

public interface AlarmService {

    Response<Boolean> orderSuccessAlarm(CompanyAppIdEnum companyAppIdEnum, String orderNo, OrderStatusEnum orderStatusEnum);

    Response<Boolean> orderFilterAlarm(CompanyAppIdEnum companyAppIdEnum,Long userId);

}

public
class AlarmServcieImpl implements AlarmService { @Autowired RedisClient RedisClient; @Autowired ConfigUtil configUtil; private static final Log logger = LogFactory.getLog(AlarmServcieImpl.class); private static final Long expireTime = 24 * 60 * 60L; //建立機器人可獲取 private
static final String requestUrl = "https://oapi.dingtalk.com/robot/send?access_token=xxxx"; @Override public Response<Boolean> orderSuccessAlarm(CompanyAppIdEnum companyAppIdEnum, String orderNo, OrderStatusEnum orderStatusEnum) { try { if (configUtil.isServerTest()) { return
new Response<>(Boolean.FALSE); } String count = RedisClient.getString(SystemConstants.ORDER_STATUS_ALARM_ROBOT, companyAppIdEnum.getCompanyId().toString(), DateUtils.getDateStr(new Date())); if (Objects.isNull(count)) { RedisClient.setString(SystemConstants.ORDER_STATUS_ALARM_ROBOT, "0", expireTime, companyAppIdEnum.getCompanyId().toString(), DateUtils.getDateStr(new Date())); } dispatcher(companyAppIdEnum, Number.getNumber(Long.valueOf(RedisClient.getString(SystemConstants.ORDER_STATUS_ALARM_ROBOT, companyAppIdEnum.getCompanyId().toString(), DateUtils.getDateStr(new Date())))), AlarmTypeEnum.ORDER_ALARM); RedisClient.incrBy(SystemConstants.ORDER_STATUS_ALARM_ROBOT, 1L, companyAppIdEnum.getCompanyId().toString(), DateUtils.getDateStr(new Date())); } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage(), e); } return new Response<>(Boolean.TRUE); } @Override public Response<Boolean> orderFilterAlarm(CompanyAppIdEnum companyAppIdEnum,Long userId) { try { if (configUtil.isServerTest()) { return new Response<>(Boolean.FALSE); } String count = RedisClient.getString(SystemConstants.ORDER_FILTER_USER_ALARM_ROBOT, companyAppIdEnum.getCompanyId().toString(), DateUtils.getDateStr(new Date())); if (Objects.isNull(count)) { RedisClient.setString(SystemConstants.ORDER_FILTER_USER_ALARM_ROBOT, "0", expireTime, companyAppIdEnum.getCompanyId().toString(), DateUtils.getDateStr(new Date())); } dispatcher(companyAppIdEnum, Number.getNumber(Long.valueOf(RedisClient.getString(SystemConstants.ORDER_STATUS_ALARM_ROBOT, companyAppIdEnum.getCompanyId().toString(), DateUtils.getDateStr(new Date())))), AlarmTypeEnum.ORDER_USER_FILTER); RedisClient.incrBy(SystemConstants.ORDER_STATUS_ALARM_ROBOT, 1L, companyAppIdEnum.getCompanyId().toString(), DateUtils.getDateStr(new Date())); } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage(), e); } return new Response<>(Boolean.TRUE); } protected void dispatcher(CompanyAppIdEnum companyAppIdEnum, Number number, AlarmTypeEnum alarmTypeEnum) { Arrays.asList(Number.values()).forEach( p -> { if (p.equals(number) && !Number.INIT_NUMBER.equals(number)) { try { sendNotice(companyAppIdEnum, number, alarmTypeEnum); } catch (IOException e) { e.printStackTrace(); logger.error(e.getMessage(), e); } return; } } ); } public static void main(String[] args) throws IOException { sendNotice(CompanyAppIdEnum.WAN_KA, Number.PRELIMINARY_WARNING, AlarmTypeEnum.ORDER_ALARM); } private static void sendNotice(CompanyAppIdEnum companyAppIdEnum, Number number, AlarmTypeEnum alarmTypeEnum) throws IOException { HttpClient httpclient = HttpClients.createDefault(); HttpPost httppost = new HttpPost(requestUrl); httppost.addHeader("Content-Type", "application/json; charset=utf-8"); String textMsg = getNotice(number, companyAppIdEnum, alarmTypeEnum); StringEntity se = new StringEntity(textMsg, "utf-8"); httppost.setEntity(se); HttpResponse response = httpclient.execute(httppost); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { String result = EntityUtils.toString(response.getEntity(), "utf-8"); logger.info("報警已經成功傳送,機構為:{},響應訊息為:{}", companyAppIdEnum.getDesc(), result); } } private static String getNotice(Number number, CompanyAppIdEnum companyAppIdEnum, AlarmTypeEnum alarmTypeEnum) { switch (alarmTypeEnum) { case ORDER_ALARM: return "{\n" + " \"msgtype\": \"markdown\",\n" + " \"markdown\": {\"title\":\"推單失敗報警\",\n" + "\"text\":\"#### 推單失敗報警 \\n " + companyAppIdEnum.getDesc() + "推單失敗已超過" + number.getCount() + "單" + "\\n > ![screenshot](https://note.youdao.com/yws/api/personal/file/WEB77f35c865dbd286c3baed15670e8892f?method=download&shareKey=dfdd0302d114373146480ae3d4330af1)\\n > ######" + new Date() + "釋出 [警告]() \"\n" + " },\n" + " \"at\": {\n" + " \"atMobiles\": [\n" + " \"15168426462\"" + " ], \n" + " \"isAtAll\": false\n" + " }\n" + " }"; case ORDER_USER_FILTER: return "{\n" + " \"msgtype\": \"markdown\",\n" + " \"markdown\": {\"title\":\"機構過濾報警\",\n" + "\"text\":\"#### 機構過濾報警 \\n " + companyAppIdEnum.getDesc() + "過濾失敗已超過" + number.getCount() + "單" + "\\n > ![screenshot](https://note.youdao.com/yws/api/personal/file/WEB77f35c865dbd286c3baed15670e8892f?method=download&shareKey=dfdd0302d114373146480ae3d4330af1)\\n > ######" + new Date() + "釋出 [警告]() \"\n" + " },\n" + " \"at\": {\n" + " \"atMobiles\": [\n" + " \"15168426462\"" + " ], \n" + " \"isAtAll\": false\n" + " }\n" + " }"; default: break; } return ""; } public enum Number { INIT_NUMBER(0L, "初始"), PRELIMINARY_WARNING(50L, "初步警告"), PRELIMINARY_WARNING_PLUS(55L, "初步警告"), INTERMEDIATE_WARNING(100L, "中級警告"), INTERMEDIATE_WARNING_PLUS(105L, "中級警告"), INTERMEDIATE_WARNING_PLUS_PLUS(200L, "中級++警告"), INTERMEDIATE_WARNING_PLUS_PLUS_PLUS(205L, "中級++警告"), SERIOUS_WARNING(300L, "嚴重警告"), SERIOUS_WARNING_PLUS(305L, "嚴重警告"), SERIOUS_WARNING_PLUS_PLUS(350L, "嚴重警告"), SERIOUS_WARNING_PLUS_PLUS_PLUS(400L, "嚴重警告"), SERIOUS_WARNING_PLUS_PLUS_PLUS_PLUS(500L, "嚴重警告"); private Long count; private String desc; Number(Long count, String desc) { this.count = count; this.desc = desc; } public Long getCount() { return count; } public Number setCount(Long count) { this.count = count; return this; } public String getDesc() { return desc; } public Number setDesc(String desc) { this.desc = desc; return this; } public static Number getNumber(Long count) { Number[] number = {INIT_NUMBER}; Arrays.asList(Number.values()).forEach( p -> { if (p.getCount().equals(count)) { number[0] = p; return; } } ); return number[0]; } } public enum AlarmTypeEnum { ORDER_ALARM, ORDER_USER_FILTER; } }

按照釘釘的文件來開發,建立機器人後,即可獲取Webhook地址,整個過程還是很簡單的,以上只是提供了一個思路.

更多文章請關注微信公眾號:
https://note.youdao.com/yws/api/personal/file/WEB6c0f1a891fdea03ecc708833e0df9b3b?method=download&shareKey=45f2c5279d958cfcdc96b6c92be0ad96