2

I'm trying to send a message using Slack incoming webhooks. I have the following code. It runs, but when I check my slack, there is no message. Can anyone see what I may have done wrong.

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();
        }


    }

}
Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114
tpetes
  • 31
  • 1
  • 5
  • I think you error handling is not sufficient to see API errors. `IOException` will only be raised if there is a low level error (e.g. no connection to server). see here https://stackoverflow.com/questions/32434947/will-apache-httpclient-execute-throw-an-ioexception-on-all-http-5xx-errors – Erik Kalkoken Jul 04 '19 at 10:47
  • 1
    You want to get the response of the API and check it for errors, e.g. `HttpResponse response = client.execute(httpPost);` – Erik Kalkoken Jul 04 '19 at 10:51
  • Check this answer on how to read the response: https://stackoverflow.com/questions/14024625/how-to-get-httpclient-returning-status-code-and-response-body – Erik Kalkoken Jul 04 '19 at 10:54
  • I actually figured it out. Thank you though! – tpetes Jul 04 '19 at 12:11
  • Awesome. Care to post the solution as answer? – Erik Kalkoken Jul 04 '19 at 12:18

2 Answers2

1

I first used Postman to test sending the message. Then used Postman to generate the corresponding Java code. Then adjusted the code a bit to arrive at the following . . .

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();
tpetes
  • 31
  • 1
  • 5
0

This has been working for me today

    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);
        }
    }
Pipo
  • 4,653
  • 38
  • 47