Skip to content

Instantly share code, notes, and snippets.

@vors
Last active May 9, 2020 17:51
  • Star 17 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save vors/bd585fc6e3d027804f80 to your computer and use it in GitHub Desktop.
Automation for sending Invites on Slack. Based on Google Forms and Google script. For more context read https://levels.io/slack-typeform-auto-invite-sign-ups/
/*
To automate your slack instance invites,
1. Create a google form with two text fields:
"Your email"
"Who invite you"
2. You will get a google table with responses and 3 fields:
1) "Timestamp"
2) "Your email"
3) "Who invite you"
3. From the resultant google sheet, go to "Tools" -> "Script Editor"
// kudos to @sugavaneshb for the note:
// "Tools -> Script Editor" option has to be chosen from the resultant google sheet and not from form. Else, there will be no 'active spreadsheet' when trying to run/initialize from form.
4. Copy-paste this script.
5. Populate secrets strings (see below) by your very own values.
6. Click "Run" -> "Initialize"
Congratulations, you are successfully rool-out invite automation!
*/
// 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);
}
@sugavaneshb
Copy link

Thanks for this. Worked just fine! Just a note: Please mention in the documentation that the "Tools -> Script Editor" option has to be chosen from the resultant google sheet and not from form. Else, there will be no 'active spreadsheet' when trying to run/initialize from form.

@vors
Copy link
Author

vors commented Feb 12, 2016

Thank you, updated the instruction.

@susanrose1
Copy link

I did all the steps but it doesn't seem to be doing anything. No error messages, it just doesn't end up with a slack invite. Help?

@vors
Copy link
Author

vors commented May 19, 2016

@susanrose1 there are 3 steps:

  • form should capture answer
  • script should kicks in and notify slack and you via email
  • slack should send invite

You can trouble-shot these 3 steps.
I.e.: form contains the answer?
Did you get an email?

@sarahleejane
Copy link

Hi! How do I get a Slack auth token please?

@hhkaos
Copy link

hhkaos commented Oct 15, 2016

@vaibhav-kaushal
Copy link

Worked for me. Thanks :)

@jayneil
Copy link

jayneil commented Jan 18, 2017

Hi Vors,

I ran your code but nothing is happening. I followed all the steps and made sure that I am running this script in the google form. The script is running successfully without any errors and the google form is even recording email responses but there is no email sent out for the slack invite. I event set up a project trigger 'OnFormSubmitEx' with the events 'From form' and 'On form submit'. Removing the trigger doesn't help either.

Below is the link to my code:

https://gist.github.com/jayneil/1e9b0ac446937a25076b9815ba52e31e

Is there anything I am doing wrong? I would really appreciate it if you could point me in the right direction. Thanks.

@jayneil
Copy link

jayneil commented Jan 27, 2017

There is a slackbot that automatically does this without the need for code. You can also set up a landing page to collect names, emails for free. I have had success using it. More details at Stacktodo Bot.

@jacobwallenberg
Copy link

@jayneil thanks for the suggestion, although it looks like they're no longer accepting new teams ☹️ if anyone has any other suggestions they'd be very appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment