-
Open the spreadsheet where your Google Form responses are collected.
-
Go to "Tools" -> "Script Editor"
-
Copy and paste the script below.
-
For SlackName, fill in your Slack's address/slug (e.g. from https://your-slack-name-is-here.slack.com).
-
For SlackToken, create a new app at https://api.slack.com/ and give it 'Admin' Permissions Scope. Click "Install" to add the app to your Slack. Copy the app token that shows up (should include xox and a dash).
-
We'll also need 'client' permissions for the app. Get your CLIENT_ID from the "Basic Information" tab on your app (on api.slack.com). Get your TEAM_ID by going here and clicking "Test Method" (you may need to generate a legacy token first). Then add both to this URL where indicated and visit it: https://slack.com/oauth/authorize?&client_id=CLIENT_ID&team=TEAM_ID&install_redirect=install-on-team&scope=admin+client (from https://github.com/outsideris/slack-invite-automation#issue-token).
-
For ccEmail, fill in your email (it will receive the sign up notifications).
-
Replace the variables and e.values[x] with the data you want to collect and the column they're located in (e.g. e.values[0] is the first column). Be sure to change the variable names everywhere.
-
Save and Run the Initialize script. You may need to give the app permission to access your Google account and run again.
// secrets
var SlackName = 'n***'; // name of your slack
var SlackToken = 'xoxs-359****'; // Slack auth token
var ccEmail = 'x****'; // your email, if you want to get email notification (otherwise comment out SendEmail call)
// end secrets
function Initialize() {
var triggers = ScriptApp.getProjectTriggers();
for (var i in triggers) {
ScriptApp.deleteTrigger(triggers[i]);
}
ScriptApp.newTrigger("onFormSubmitEx")
.forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
.onFormSubmit()
.create();
}
function post(url, payload) {
var options =
{
"method" : "POST",
"payload" : payload,
"followRedirects" : true,
"muteHttpExceptions": true
};
var result = UrlFetchApp.fetch(url, options);
return result.getContentText();
}
// send email to your ccEmail with sign-up info
function SendMail(newEmail, whoInvite, curlStatus) {
try {
// This will show up as the sender's name
sendername = "Slack auto-invite";
// Optional but change the following variable
// to have a custom subject for Google Docs emails
subject = "New user signed up";
// This is the body of the auto-reply
message = "New user <b>" + newEmail + "</b> signed up<br><br>";
message += 'Invited by :: ' + whoInvite + "<br>";
message += 'Curl status :: ' + curlStatus + "<br>";
textbody = message.replace("<br>", "\n");
GmailApp.sendEmail(ccEmail, subject, textbody, {
cc: ccEmail,
name: sendername,
htmlBody: message
});
} catch (e) {
Logger.log(e.toString());
}
}
function onFormSubmitEx(e) {
var timestamp = e.values[0];
var toAddress = e.values[1];
var whoInvite = e.values[2];
var slackInviteUrl = 'https://' + SlackName + '.slack.com/api/users.admin.invite';
var curlStatus = post(slackInviteUrl, {
token: SlackToken,
email: toAddress
});
SendMail(toAddress, whoInvite, curlStatus);
}