I have created a Slack Bot in a Test workspace to play with. All I am trying to do is run a GET API to collect the content of a channel, but I am tripping up at the first hurdle...
The API = GET https://slack.com/api/conversations.history
Variables:
But my own HTML code fails.. returning with invalid_auth... So I guess it must be an issue with my HTML... But the thing is, the exact same code works with another slack bot I set up previously!
The basic HTML:
<html>
<head>
</head>
<body>
<button id = "SendButton" onclick="GetChannelFromSlack()">GET</button>
<p id="GET API Output">Test</p>
</body>
<script>
function GetChannelFromSlack() {
var slackBotToken = "xoxb-xxxxAwQ";
var slackChannelID = "xxxxH9P";
var getSlackData = new XMLHttpRequest();
var url = new URL('https://slack.com/api/conversations.history?token='+slackBotToken+'&channel='+slackChannelID);
getSlackData.open( "GET", url, true );
//getSlackData.setRequestHeader = ('Authorization', 'Bearer xoxb-xxxxAwQ');
getSlackData.responseType = 'json';
getSlackData.send();
getSlackData.onload = function() {
var jsonResponse = getSlackData.response;
document.getElementById("GET API Output").innerHTML = JSON.stringify(jsonResponse);
}
}
</script>
</html>
(Code snippet won't actually work as I have not entered in my actual bot token / channel id)
Edit Additionally, to isolate the issue - here is a curl command which works for one Bot/Channel, but not the other:
Working: curl -X GET 'https://slack.com/api/conversations.history?token=xoxb-xxx-VKa&channel=CxxxxTX'
Not Working: curl -X GET 'https://slack.com/api/conversations.history?token=xoxb-xxx-AwQ&channel=Cxxxx9P'
I do not see any difference in the setup of the two bots...
If anyone can identify why this is failing I would be really appreciate it!
Regards, Dann
Investigating further with just curl, I was able to GET output with either bot/channel using the following format:
curl -H "Authorization: Bearer xoxb-xxxxxAwQ" -X GET 'https://slack.com/api/conversations.history?channel=CxxxxTX'
Still unclear to me why the other API call format works for my original bot, but I'm happy to accept and use this format, and to adjust the HTML code to accordingly.
Thanks! Dann
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With