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).