WechatHookCallback implements AlarmCallback { @Override public void doAlarm(List<AlarmMessage> alarmMessages) { if (this.alarmRulesWatcher.getWechatSettings() == null || this.alarmRulesWatcher.getWechatSettings().getWebhooks().isEmpty()) { return; } CloseableHttpClient httpClient = HttpClients.custom().build(); try { this.alarmRulesWatcher.getWechatSettings().getWebhooks().forEach(url -> { alarmMessages.forEach(alarmMessage -> { String requestBody = String.format( this.alarmRulesWatcher.getWechatSettings().getTextTemplate(), alarmMessage.getAlarmMessage() ); sendAlarmMessage(httpClient, url, requestBody); }); }); } finally { try { httpClient.close(); } catch (IOException e) { log.error(e.getMessage(), e); } } } WechatHookCallback(final AlarmRulesWatcher alarmRulesWatcher); @Override void doAlarm(List<AlarmMessage> alarmMessages);  }
@Test public void testWechatWebhook() { List<String> remoteEndpoints = new ArrayList<>(); remoteEndpoints.add("http: Rules rules = new Rules(); String template = "{\"msgtype\":\"text\",\"text\":{\"content\":\"Skywaling alarm: %s\"}}"; rules.setWecchats(WechatSettings.builder().webhooks(remoteEndpoints).textTemplate(template).build()); AlarmRulesWatcher alarmRulesWatcher = new AlarmRulesWatcher(rules, null); WechatHookCallback wechatHookCallback = new WechatHookCallback(alarmRulesWatcher); List<AlarmMessage> alarmMessages = new ArrayList<>(2); AlarmMessage alarmMessage = new AlarmMessage(); alarmMessage.setScopeId(DefaultScopeDefine.ALL); alarmMessage.setRuleName("service_resp_time_rule"); alarmMessage.setAlarmMessage("alarmMessage with [DefaultScopeDefine.All]"); alarmMessages.add(alarmMessage); AlarmMessage anotherAlarmMessage = new AlarmMessage(); anotherAlarmMessage.setRuleName("service_resp_time_rule_2"); anotherAlarmMessage.setScopeId(DefaultScopeDefine.ENDPOINT); anotherAlarmMessage.setAlarmMessage("anotherAlarmMessage with [DefaultScopeDefine.Endpoint]"); alarmMessages.add(anotherAlarmMessage); wechatHookCallback.doAlarm(alarmMessages); Assert.assertTrue(isSuccess); }