Skip to content

Instantly share code, notes, and snippets.

@c9s
Last active August 29, 2015 14:20
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save c9s/48cc0c1076f79b741f74 to your computer and use it in GitHub Desktop.
Save c9s/48cc0c1076f79b741f74 to your computer and use it in GitHub Desktop.
InviteToSlack from Google SpreadSheet
/*
The original script: https://github.com/dherbst/slack-invite-script/blob/master/code.js
*/
/*
Get channels from: curl 'https://golang.slack.com/api/channels.list?token={token}' | json_pp
*/
function getInviteChannels() {
return ['C123123123'];
}
function getNotificationChannels() {
return ['#general'];
}
function getSlackHost() {
return 'https://xxxxx.slack.com';
}
function getSlackAPIToken() {
return '........';
}
/**
* Retrieves all the rows in the active spreadsheet that contain data
* When going through the rows if there no value in 'invited by' then
* sends the email field to the slack invite service.
* For more information on using the Spreadsheet API, see
* https://developers.google.com/apps-script/service_spreadsheet
*/
function readRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
// Sheet is a forms response sheet with 3 columns
// 'Timestamp' 'What is your email address?' 'Invited by'
Logger.log('Looking for rows with email but no invited by...');
for (var i = 0; i <= numRows - 1; i++) {
var row = values[i];
var firstName = row[1];
var lastName = row[2];
var email = row[3];
var invited = row[4];
Logger.log(email + ":" + invited);
if (!invited && email) {
Logger.log('Inviting email = ' + email);
var result = invite(email);
Logger.log(result);
sheet.getRange(i + 1, 6).setValue(result || 'scriptbot');
SpreadsheetApp.flush();
}
}
return;
}
/**
* obscure the full email
*/
function hideEmail(email) {
return email.replace(/(.+)@.+$/, '$1@redacted');
}
/**
* Tell the signupform channel you invited someone. This is a provides a backup
* if signup via API dies.
*/
function sayInvited(email, inviteResponse) {
var message;
var options;
var payload = getPayload();
var result;
var time = Math.ceil(new Date().getTime() / 1000);
var url = getSlackHost() + '/api/chat.postMessage?t=' + time;
if (!payload) {
return;
}
if (inviteResponse.ok === true) {
message = hideEmail(email) + ', Invited Successfully';
} else {
message = 'Error Inviting: ' +
hideEmail(email) + ' Error:' + inviteResponse.error;
}
payload.channel = getNotificationChannels().join(','); // when posting message, we don't need channel IDs
payload.text = message;
payload.username = 'dherbstscriptbot';
options = {
'method' : 'POST',
'payload' : payload,
'followRedirects' : true,
'muteHttpExceptions': true
};
result = UrlFetchApp.fetch(url, options);
if (result.getResponseCode() == 200) {
Logger.log(result);
} else {
Logger.log('exception');
Logger.log(result);
}
return;
}
/**
* Return a payload object with the basic required information.
*/
function getPayload() {
var payload;
var token = getSlackAPIToken();
if (token == 'fill_in_your_api_token') {
Logger.log('You have to fill in your api token');
return;
}
payload = {
'token' : token,
'type' : 'post'
};
return payload;
}
/**
* Sends the email to the slack invite endpoint. You need to fill in your api token
* and the channels you want the user to be added to.
*
*/
function invite(email) {
var options;
var payload = getPayload();
var result;
var time = Math.ceil(new Date().getTime() / 1000);
var url = getSlackHost() + '/api/users.admin.invite?t=' + time;
if (payload === undefined || payload === null) {
return;
}
payload.email = email;
payload.channels = getInviteChannels().join('/');
payload.set_active = 'true';
payload._attempts = '1';
options = {
'method' : 'POST',
'payload' : payload,
'followRedirects' : true,
'muteHttpExceptions': true
};
result = UrlFetchApp.fetch(url, options);
if (result.getResponseCode() == 200) {
sayInvited(email, JSON.parse(result));
}
return result;
}
/**
* Adds a custom menu to the active spreadsheet, containing a single menu item
* for invoking the readRows() function specified above.
* The onOpen() function, when defined, is automatically invoked whenever the
* spreadsheet is opened.
* For more information on using the Spreadsheet API, see
* https://developers.google.com/apps-script/service_spreadsheet
*/
function onOpen() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : 'Invite to Slack',
functionName : 'readRows'
}];
spreadsheet.addMenu('Script Center Menu', entries);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment