Skip to content

Instantly share code, notes, and snippets.

@rayterrill
Created March 28, 2018 17:01
Show Gist options
  • Save rayterrill/9a79f1fee30e2a8b4fb0c059eeb0ceb3 to your computer and use it in GitHub Desktop.
Save rayterrill/9a79f1fee30e2a8b4fb0c059eeb0ceb3 to your computer and use it in GitHub Desktop.
// Description:
// Allows Hubot to create channels for incidents on the fly
//
// Configuration:
// None
//
// Commands:
// hubot create incident <name> - automagically creates an incident channel + invites all users
const { WebClient } = require('@slack/client');
var dateFormat = require('dateformat');
const botID = process.env.BOT_ID;
const usergroupID = process.env.USERGROUP_ID;
const myID = process.env.MY_ID;
module.exports = function (robot) {
robot.respond(/create incident (.*)/i, function (res) {
incidentName = res.match[1];
var now = new Date();
date = dateFormat(now, "mmddyy");
// can only have channels of 21 characters. substring out the name if too long.
if (incidentName.length > 10) {
incidentName = incidentName.substring(0, 11);
}
channelName = 'ic_' + incidentName + '_' + date;
var reply = "Attempting to create incident channel " + channelName;
//uses the admin token to create a channel
const token = process.env.ADMIN_TOKEN;
const web = new WebClient(token);
web.channels.create(channelName)
.then((res) => {
console.log(res);
channel = res.channel.id;
web.channels.invite(channel, botID)
.then((res) => {
web.usergroups.users.list(usergroupID)
.then((res) => {
users = res.users;
//loop through the user group list and invite each user individually. this really sucks.
for (var i = 0, len = users.length; i < len; i++) {
if (users[i] !== myID) { //dont invite yourself
web.channels.invite(channel, users[i])
.catch((error) => {
console.log('Error inviting user. Probably self.');
});
}
}
//send a message to the room letting everyone know why it was stood up
robot.messageRoom(channelName, "An incident channel has been stood up in response to an incident. Please use this channel for all communications related to the incident. Edit this description to provide unique incident information.");
})
.catch((error) => {
console.log(error);
});
})
.catch((error) => {
console.log(error);
})
})
.catch(console.error);
res.reply(reply);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment