SlackNotificationService implements NotificationService { @Override public void sendNotification(Check check, Subscription subscription, List<Alert> alerts) throws NotificationFailedException { String token = seyrenConfig.getSlackToken(); String channel = subscription.getTarget(); String username = seyrenConfig.getSlackUsername(); String iconUrl = seyrenConfig.getSlackIconUrl(); List<String> emojis = Lists.newArrayList( Splitter.on(',').omitEmptyStrings().trimResults().split(seyrenConfig.getSlackEmojis()) ); String url = String.format("%s/api/chat.postMessage", baseUrl); HttpClient client = HttpClientBuilder.create().useSystemProperties().build(); HttpPost post = new HttpPost(url); post.addHeader("accept", "application/json"); List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>(); parameters.add(new BasicNameValuePair("token", token)); parameters.add(new BasicNameValuePair("channel", StringUtils.removeEnd(channel, "!"))); parameters.add(new BasicNameValuePair("text", formatContent(emojis, check, subscription, alerts))); parameters.add(new BasicNameValuePair("username", username)); parameters.add(new BasicNameValuePair("icon_url", iconUrl)); try { post.setEntity(new UrlEncodedFormEntity(parameters)); if (LOGGER.isDebugEnabled()) { LOGGER.info("> parameters: {}", parameters); } HttpResponse response = client.execute(post); if (LOGGER.isDebugEnabled()) { LOGGER.info("> parameters: {}", parameters); LOGGER.debug("Status: {}, Body: {}", response.getStatusLine(), new BasicResponseHandler().handleResponse(response)); } } catch (Exception e) { LOGGER.warn("Error posting to Slack", e); } finally { post.releaseConnection(); HttpClientUtils.closeQuietly(client); } } @Inject  SlackNotificationService(SeyrenConfig seyrenConfig); protected  SlackNotificationService(SeyrenConfig seyrenConfig, String baseUrl); @Override void sendNotification(Check check, Subscription subscription, List<Alert> alerts); @Override boolean canHandle(SubscriptionType subscriptionType);  }
@Test public void basicSlackTest() { BigDecimal value = new BigDecimal("1.0"); Check check = new Check() .withId("123") .withEnabled(true) .withName("test-check") .withState(AlertType.ERROR); Subscription subscription = new Subscription() .withEnabled(true) .withType(SubscriptionType.SLACK) .withTarget("target"); Alert alert = new Alert() .withValue(value) .withTimestamp(new DateTime()) .withFromType(AlertType.OK) .withToType(AlertType.ERROR); List<Alert> alerts = Arrays.asList(alert); StringBodyCapture bodyCapture = new StringBodyCapture(); clientDriver.addExpectation( onRequestTo("/api/chat.postMessage") .withMethod(ClientDriverRequest.Method.POST) .capturingBodyIn(bodyCapture) .withHeader("accept", "application/json"), giveEmptyResponse()); notificationService.sendNotification(check, subscription, alerts); String content = bodyCapture.getContent(); System.out.println(decode(content)); assertThat(content, Matchers.containsString("token=")); assertThat(content, Matchers.containsString("&channel=target")); assertThat(content, not(Matchers.containsString(encode("<!channel>")))); assertThat(content, Matchers.containsString(encode("*ERROR* test-check"))); assertThat(content, Matchers.containsString(encode("/#/checks/123"))); assertThat(content, Matchers.containsString("&username=Seyren")); assertThat(content, Matchers.containsString("&icon_url=")); verify(mockSeyrenConfig).getSlackEmojis(); verify(mockSeyrenConfig).getSlackIconUrl(); verify(mockSeyrenConfig).getSlackToken(); verify(mockSeyrenConfig).getSlackUsername(); verify(mockSeyrenConfig).getBaseUrl(); }
@Test public void mentionChannelWhenTargetContainsExclamationTest() { BigDecimal value = new BigDecimal("1.0"); Check check = new Check() .withId("123") .withEnabled(true) .withName("test-check") .withState(AlertType.ERROR); Subscription subscription = new Subscription() .withEnabled(true) .withType(SubscriptionType.SLACK) .withTarget("target!"); Alert alert = new Alert() .withValue(value) .withTimestamp(new DateTime()) .withFromType(AlertType.OK) .withToType(AlertType.ERROR); List<Alert> alerts = Arrays.asList(alert); StringBodyCapture bodyCapture = new StringBodyCapture(); clientDriver.addExpectation( onRequestTo("/api/chat.postMessage") .withMethod(ClientDriverRequest.Method.POST) .capturingBodyIn(bodyCapture) .withHeader("accept", "application/json"), giveEmptyResponse()); notificationService.sendNotification(check, subscription, alerts); String content = bodyCapture.getContent(); System.out.println(decode(content)); assertThat(content, Matchers.containsString("token=")); assertThat(content, Matchers.containsString("&channel=target")); assertThat(content, Matchers.containsString(encode("<!channel>"))); assertThat(content, Matchers.containsString(encode("*ERROR* test-check"))); assertThat(content, Matchers.containsString(encode("/#/checks/123"))); assertThat(content, Matchers.containsString("&username=Seyren")); assertThat(content, Matchers.containsString("&icon_url=")); verify(mockSeyrenConfig).getSlackEmojis(); verify(mockSeyrenConfig).getSlackIconUrl(); verify(mockSeyrenConfig).getSlackToken(); verify(mockSeyrenConfig).getSlackUsername(); verify(mockSeyrenConfig).getBaseUrl(); }