[英]Send a message from slack user to another slack user with apache camel
[英]Java: Send POST curl command to send a message in slack
我有一个 selenium java 自动化,我需要在我的 Z93F724684FZF4B8 代码中发送以下 CURL 命令
curl -X POST -H 'Content-type: application/json' --data '{"text":"Test"}' https://hooks.slack.com/services/XXX/YYY/ZZZ
我尝试了一些在这里找到的解决方案,例如:
String command = "curl -X POST -H 'Content-type: application/json' --data '{"text":"Test"}' https://hooks.slack.com/services/XXX/YYY/ZZZ";
Runtime.getRuntime().exec(command);
还有这个:
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost("https://hooks.slack.com/services/XXX/YYY/ZZZ");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("text", "test"));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);
脚本运行,但没有任何反应(Eclipse 控制台上没有消息/警告/错误),它只是运行并成功通过,但消息未在 Slack 通道上发送。 如果我在我的 linux 终端中运行同样的 curl 命令,则消息会在 Slack 上发送。
我还在学习 Java,所以很可能我做错了什么。
我终于可以让它工作了:
String[] command = new String[] { "/bin/bash", "-c", "curl -X POST -H 'Content-type: application/json' --data '{\"text\":\"test\"}' https://hooks.slack.com/services/AAA/BBB/CCC"};
Process proc = new ProcessBuilder(command).start();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.