Last active
November 10, 2017 08:09
-
-
Save skynyrd/5f0dc0cff2ed30aab5ea9016e19aadcc to your computer and use it in GitHub Desktop.
Slack webhook in java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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