我一直在网上寻找这个,我还没有找到一个有效的答案。我正在尝试创建一个bukkit插件,在运行命令时将数据发送到正在运行的Slack webhook。我已经注意到命令正在运行,但我不知道如何发送JSON。 (对于那些不熟悉Slack的人来说,终端窗口内的命令是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
我一直在寻找并用谷歌搜索一小段时间试图找到一种方法用Java来发送它。但不管我是什么尝试它不起作用。感谢任何帮助,谢谢
答案 0 :(得分:4)
//您可以使用以下代码! slackWebhook是您可以从custom_integration链接
获取的频道的https端点 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());