繁体   English   中英

Java - 向 Slack Webhook 发送消息

[英]Java - Sending Message to Slack Webhook

我正在尝试使用 Slack 传入 webhook 发送消息。 我有以下代码。 它运行,但是当我检查我的松弛时,没有消息。 谁能看到我可能做错了什么。

public class SlackTest {

    static String web_hook_url = "https://hooks.slack.com/services/***********/******************";

    public static void main(String[] args) {


        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(web_hook_url);

        try {
            String json = "{\"name\": John}";
            System.out.println(json);
            StringEntity entity = new StringEntity(json);
            httpPost.setEntity(entity);
            httpPost.setHeader("Accept", "application/json");
            httpPost.setHeader("Content-type", "application/json");

            client.execute(httpPost);

            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

}

我首先使用Postman测试发送邮件。 然后使用Postman生成相应的Java代码。 然后稍微调整一下代码,即可得出以下结果。

OkHttpClient client2 = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{ \"text\" : \"more text"\" }");
            Request request2 = new Request.Builder()
            .url("https://hooks.slack.com/services/********/*********/***************")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .addHeader("Accept", "*/*")
                    .addHeader("Cache-Control", "no-cache")
                    .addHeader("Host", "hooks.slack.com")
                    .addHeader("accept-encoding", "gzip, deflate")
                    .addHeader("Connection", "keep-alive")
                    .addHeader("cache-control", "no-cache")
                    .build();
Response response2 = client2.newCall(request2).execute();

今天这对我有用

    public void sendByWebHook(String msg) throws SlackApiException {
        assert slackMessage != null : "null slackMessage";
        final String METHOD_NAME = "sendByWebHook";
        final String HOOK_URL = "https://hooks.slack.com/services/T.../B.../FooBar...";

        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(HOOK_URL);

        try {
            String json = "{ \"text\" : \"" + msg + ""\" }";
            StringEntity entity = new StringEntity(json);
            httpPost.setEntity(entity);
            httpPost.setHeader("Accept", "application/json");
            httpPost.setHeader("Content-type", "application/json");
            client.execute(httpPost);
            client.close();
        } catch (UnsupportedEncodingException e) {
            logger.error("{}->Error ", METHOD_NAME, e);
            throw new SlackApiException("Error sending slack message", e);
        } catch (ClientProtocolException e) {
            logger.error("{}->Error ", METHOD_NAME, e);
            throw new SlackApiException("Error sending slack message", e);
        } catch (IOException e) {
            logger.error("{}->Error ", METHOD_NAME, e);
            throw new SlackApiException("Error sending slack message", e);
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM