I\'m trying to send a message using Slack\'s chat.postMessage API call. I have no problems encoding my test messages within HTTP GET, but I\'m trying to achieve the same res
not_authed
means No authentication token provided.
Which token are you passing along in the request? You need to pass your OAuth token, which you can get from here.
If you are using java and spring such dependency, Voila!! here you go
* Make a POST call to the chat.PostMessage.
*/
public void chatPostMessage(Message message)
{
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add("token", slackPostMessageToken); //Required
map.add("text", message.getText());//Required
map.add("channel", message.getChannelId());//Required
map.add("unfurl_links","true");
map.add("as_user","false");//Default false
map.add("icon_emoji",":chart_with_upwards_trend:");
map.add("attachments","[\n" +
" {\n" +
" \"fallback\": \"Required plain-text summary of the attachment.\",\n" +
" \"color\": \"#36a64f\",\n" +
" \"pretext\": \"Optional text that appears above the attachment block\",\n" +
" \"author_name\": \"Bobby Tables\",\n" +
" \"author_link\": \"http://flickr.com/bobby/\",\n" +
" \"author_icon\": \"http://flickr.com/icons/bobby.jpg\",\n" +
" \"title\": \"Slack API Documentation\",\n" +
" \"title_link\": \"https://api.slack.com/\",\n" +
" \"text\": \"Optional text that appears within the attachment\",\n" +
" \"fields\": [\n" +
" {\n" +
" \"title\": \"Priority\",\n" +
" \"value\": \"High\",\n" +
" \"short\": false\n" +
" }\n" +
" ],\n" +
" \"image_url\": \"http://my-website.com/path/to/image.jpg\",\n" +
" \"thumb_url\": \"http://example.com/path/to/thumb.png\",\n" +
" \"footer\": \"Datoo ©\",\n" +
" \"ts\": "+System.currentTimeMillis()+"" +
" }\n" +
" ]");
map.add("username","III");
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
try {
ResponseEntity<String> response = restTemplate.postForEntity(slackPostMessageUrl, request, String.class);
System.out.println(response);
}
catch (RestClientException e) {
logger.error("Error :-( : ", e);
}
}
This may not qualify for the complete answer, but if the purpose is to send a message attachment, you can send a urlencode
d JSON structure as the value of the attachments
parameter, like so (split into multiple lines for clarity):
https://slack.com/api/chat.postMessage?
token=YOUR-TOKE-N000&
channel=%23alerts&
text=Hi&
attachments=%5B%7B%22color%22%3A%22good%22%2C%22fallback%22%3A%22plain+text%22%2C%22text%22%3A%22colored+text%22%7D%5D
The value of attachments
is URL-encoded [{"color":"good","fallback":"plain text","text":"colored text"}]
. You should be able to use all attachment attributes described here.
Slack has been updated, this now works. Try this example:
curl -X POST -H 'Content-type: application/json' --data '{"text":"This is a line of text.\nAnd this is another one."}' https://hooks.slack.com/services/AAAAAA/BBBBBB/CCCCCC
See https://api.slack.com/incoming-webhooks for documentation.
On postman you can frame your request like this:
url(POST) : https://slack.com/api/chat.postMessage
Then under Headers
section put these two headers:
key(Content-Type
) value(application/json
)
key(Authorization
) value(YOUR-TOKEN-NAME
)
Then in Body
section in raw
form put your data:
{"channel": "CHANNEL-NAME", "data": "You better post this to channel"}