Hubot-slack: Some way to create channels with hubot?

Created on 21 Mar 2018  路  3Comments  路  Source: slackapi/hubot-slack

Description

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?

What type of issue is this? (place an x in one of the [ ])

  • [ ] bug
  • [ ] enhancement (feature request)
  • [x] question
  • [ ] documentation related
  • [ ] testing related
  • [ ] discussion

Requirements (place an x in each of the [ ])

  • [x] I've read and understood the Contributing guidelines and have done my best effort to follow them.
  • [x] I've read and agree to the Code of Conduct.
  • [x] I've searched for any related issues and avoided creating a duplicate issue.
question

Most helpful comment

hello @rayterrill, that's an excellent question.

here's how i would implement this:

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

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

  3. 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');
    });
  });
};
  1. Almost there! Now just make sure that when you start hubot, you put the additional environment variable on the command line:
HUBOT_SLACK_TOKEN=xoxb-... HUBOT_SLACK_OAUTH_TOKEN=xoxp-... ./bin/hubot --adapter slack

All 3 comments

hello @rayterrill, that's an excellent question.

here's how i would implement this:

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

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

  3. 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');
    });
  });
};
  1. Almost there! Now just make sure that when you start hubot, you put the additional environment variable on the command line:
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!

Was this page helpful?
0 / 5 - 0 ratings