1

I've been looking at this for a while now. I'm trying to post a message and some buttons to a slack channel using the url for chat.postMessage (https://slack.com/api/chat.postMessage). That is the url stored in the messagePostURL variable. My PostingConstants.relevancePost variable is holding the exact JSON that is shown on the slack buttons tutorial for interactive messages found at https://api.slack.com/docs/message-buttons and I've added my channel name and token to the JSON code. The same JSON code works when sending it to a webhook, but not sending this directly to the chat.postMessage url. Why might this not work properly?

    String urlParameters  = PostingConstants.relevancePost;
    byte[] postData       = urlParameters.getBytes( StandardCharsets.UTF_8 );
    int    postDataLength = postData.length;
    //String request        = messagePostURL;
    URL    url            = new URL( messagePostURL );
    HttpURLConnection conn= (HttpURLConnection) url.openConnection();
    conn.setDoOutput( true );
    conn.setInstanceFollowRedirects( false );
    conn.setRequestMethod( "POST" );
    conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");
    conn.setRequestProperty( "charset", "utf-8");
    conn.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
    conn.setUseCaches( false );
    try( DataOutputStream wr = new DataOutputStream( conn.getOutputStream())) {
        wr.write( postData );
    }

Printing conn.getResponseCode() gives 200 (this is good to my knowledge), and printing conn.getResponseMessage() gives OK (again should be good).

Cody Berry
  • 263
  • 3
  • 14
  • Are you supplying a token? – aglassman Mar 02 '18 at 17:34
  • Also, check the result code and message from the connection. Slack typically sends a pretty useful error message if something failed. – aglassman Mar 02 '18 at 17:36
  • Yes, I am supplying it specifically with the xoxb token for the Bot User OAuth Access Token. Concerning what they send back, I get OK and 200 which are both good as far as I remember when I was making a bot in python to post images. – Cody Berry Mar 02 '18 at 17:43
  • @aglassman, when sending as a webhook, I don't need to convert the string that I've formatted as a json obj into an actual json object. Would I need to make that conversion using this new method? – Cody Berry Mar 02 '18 at 17:44
  • No, a string is fine, but you should set Content-Type to application/json. – aglassman Mar 02 '18 at 18:04
  • How would I do that? I just found the page talking about the changes since October and I need to set Authorization also. I'm not sure where to set these things though. Is it in the JSON string or somehow else? – Cody Berry Mar 02 '18 at 18:08
  • You should be able to google most of that. As for changing content type, literally just replace "application/x-www-form-urlencoded" with "application/json". – aglassman Mar 02 '18 at 18:11
  • Those are two very different endpoints, with slightly different syntax. Just because it works with one, does not mean it will work with the other. I would suggest to focus on chat.postMessage, since its the more powerful one – Erik Kalkoken Mar 02 '18 at 18:16
  • I believe the API does also return OK 200 on error. Instead you have to check the JSON response message, which contains a property names `ok`, which will be `false` on error – Erik Kalkoken Mar 02 '18 at 18:20
  • also, please include the data you are posting – Erik Kalkoken Mar 02 '18 at 18:23

0 Answers0