Last active
April 25, 2016 14:09
-
-
Save hassaku/603b28a1ad53e96900be12a745bb0aeb to your computer and use it in GitHub Desktop.
Notify on Slack when new channel is created
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Libraries | |
- SlackApp: M3W5Ut3Q39AaIwLquryEPMwV62A3znfOO | |
- Underscore: MGwgKN2Th03tJ5OdmlzB8KPxhMjh3Sh48 | |
- Moment: MHMchiX6c1bwSqGM1PZiW_PxhMjh3Sh48 | |
Usage | |
- Create sheet named "Channel List", then paste this script on script editor. | |
*/ | |
var _ = Underscore.load(); | |
var TOKEN = "YOUR-SLACK-TOKEN"; | |
var CHANNEL_NAME = "lobby"; | |
var SHEET_NAME = "Channel List"; | |
var ADDITIONAL_MESSAGE = "新しく作成されたチャンネルがあります。 " | |
function getChannelId(slackApp, name) { | |
var channel = _.findWhere(slackApp.channelsList().channels, {name: name}); | |
if (_.isEmpty(channel)) { | |
throw new Error(name + " is not found"); | |
} | |
return channel.id | |
} | |
function postMessage(slackApp, message) { | |
var channelId = getChannelId(slackApp, CHANNEL_NAME); | |
slackApp.chatPostMessage(channelId, message, { | |
username : "bot", | |
icon_emoji : ":new:" | |
}); | |
} | |
function main() { | |
var slackApp = SlackApp.create(TOKEN); | |
var current_channels = _.pluck(slackApp.channelsList().channels, 'name'); | |
var message = ''; | |
var sheet = SpreadsheetApp.getActive().getSheetByName(SHEET_NAME); | |
var rows = _.map(sheet.getDataRange().getValues(), function(elm){ return elm.toString(); }); | |
_.each(current_channels, function(channel, i) { | |
if (!_.include(rows, channel)) { | |
message += " #" + channel; | |
} | |
}); | |
//if (message) Logger.log(ADDITIONAL_MESSAGE + message) | |
if (message) postMessage(slackApp, ADDITIONAL_MESSAGE + message); | |
_.each(current_channels, function(channel, i) { | |
sheet.getRange('A' + (i+1)).setValue(channel); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment