We'd like to build a workflow that requires standing up a new channel as a part of a hubot interaction. It looks like the api call to create channels is not available to bot users. Is there a viable workaround for this with hubot?
x in one of the [ ])x in each of the [ ])hello @rayterrill, that's an excellent question.
here's how i would implement this:
Make sure you're using a Slack App (as opposed to a Custom Bot or a Hubot Configuration (service)). If you got your token by visiting https://api.slack.com/apps, creating an App, adding a Bot User to that app, and then installing that App on your Development Workspace - you are all set! If you used any other way of getting your token, you want to do what I just described (described in some helpful details here).
Visit the "OAuth & Permissions" page in your app configuration, scroll down to Scopes, and add the channels.write scope. You'll be prompted with a banner at the top of the page to reinstall your app in the development workspace, do that. then at the top of the page, copy the "OAuth Access Token" (the one that starts with xoxp).
You'll need to write some code to instantiate a new WebClient object using the token we just copied. I suggest reading the token from an environment variable, and I'll demonstrate how that can be done below:
// some file in your scripts directory
const { WebClient } = require('@slack/client');
module.exports = (robot) => {
robot.hear(/make me a channel/i, (res) => {
// just an example, put this code where ever you want to make a channel
const slack = new WebClient(process.env.HUBOT_SLACK_OAUTH_TOKEN);
slack.channels.create('some-channel-name').then(() => {
res.send('channel created!');
})
.catch((error) => {
res.send('error creating channel');
});
});
};
HUBOT_SLACK_TOKEN=xoxb-... HUBOT_SLACK_OAUTH_TOKEN=xoxp-... ./bin/hubot --adapter slack
Thanks @aoberoi. I meant to circle back - I did eventuall get this working. https://gist.github.com/rayterrill/9a79f1fee30e2a8b4fb0c059eeb0ceb3.
Hopefully the new Workspace Tokens handles some of this stuff going forward.
Also just noting for the record that it is terrible that User Groups are not usable via the API. :(
@rayterrill glad it works for you! 馃檶
Workspace Tokens would make this slightly better on the Slack side of things, but there's still some challenges in Hubot core that may interfere with this being as easy as it should be. Let's see how it shapes up!
Most helpful comment
hello @rayterrill, that's an excellent question.
here's how i would implement this:
Make sure you're using a Slack App (as opposed to a Custom Bot or a Hubot Configuration (service)). If you got your token by visiting https://api.slack.com/apps, creating an App, adding a Bot User to that app, and then installing that App on your Development Workspace - you are all set! If you used any other way of getting your token, you want to do what I just described (described in some helpful details here).
Visit the "OAuth & Permissions" page in your app configuration, scroll down to Scopes, and add the
channels.writescope. You'll be prompted with a banner at the top of the page to reinstall your app in the development workspace, do that. then at the top of the page, copy the "OAuth Access Token" (the one that starts withxoxp).You'll need to write some code to instantiate a new
WebClientobject using the token we just copied. I suggest reading the token from an environment variable, and I'll demonstrate how that can be done below: