Skip to content

Instantly share code, notes, and snippets.

@skynyrd
Last active November 10, 2017 08:09
Show Gist options
  • Save skynyrd/5f0dc0cff2ed30aab5ea9016e19aadcc to your computer and use it in GitHub Desktop.
Save skynyrd/5f0dc0cff2ed30aab5ea9016e19aadcc to your computer and use it in GitHub Desktop.
Slack webhook in java
// Slack
private void sendSlackNotification(String id, String message) {
MediaType JSON
= MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(JSON,
String.format("{ \"text\": \"Cannot send the product with id <%s>,\n Error: %s\"}", id, message.replaceAll("\"", "'")));
Request request = new Request.Builder()
.url(Constants.slackWebHookUrl)
.post(body)
.build();
try {
Response response = httpClient.newCall(request).execute();
if(!response.isSuccessful()) {
log.error(String.format("Cannot send slack notification! Response message: %s", response.message()));
}
} catch (IOException e) {
log.error(String.format("Cannot send slack notification! Ex: %s", e.toString()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment