I've been looking all over the internet for this, and I just haven't found an answer that works. I'm trying to make a bukkit plugin that sends data to an ingoing Slack webhook when a command is run. I've gotten to noticing the command running, but I have no idea how to send the JSON. (For those of you unfamiliar with Slack, the command inside a terminal window is curl -X POST --data-urlencode 'payload={"channel":"#slack-channel-id","username":"bot's username","text":"Self explanatory","icon_emoji":"The bot's icon"}' https://slack.com/custom/webhook/token/here
I've been looking all over and googling for a good hour trying to find a way in Java to send this. But no matter what I try it doesn't work. Any help is appreciated, thanks
Asked
Active
Viewed 6,361 times
4

Redrield
- 309
- 1
- 5
- 15
-
See http://stackoverflow.com/questions/3324717/sending-http-post-request-in-java – John Hascall Jan 09 '16 at 06:40
-
@JohnHascall I tried those, they didn't work for me – Redrield Jan 09 '16 at 06:52
1 Answers
7
//You can use the following code it works! slackWebhook is the https endpoint for the channel that you can get from custom_integration link
String payload = "payload={\"channel\": \"#channel_name\", \"text\": \"This is posted "
+ "to #ewe_gps_abacus_notif and comes from a bot named change-alert.\"}";
StringEntity entity = new StringEntity(payload,
ContentType.APPLICATION_FORM_URLENCODED);
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost request = new HttpPost(slackWebhook);
request.setEntity(entity);
HttpResponse response = null;
try {
response = httpClient.execute(request);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(response.getStatusLine().getStatusCode());

ridzi1989
- 86
- 1
- 2