Skip to content

Instantly share code, notes, and snippets.

@HarunMbaabu
Last active July 15, 2021 21:11
Show Gist options
  • Save HarunMbaabu/9fab89f3186133e621cb06ad55a68e0e to your computer and use it in GitHub Desktop.
Save HarunMbaabu/9fab89f3186133e621cb06ad55a68e0e to your computer and use it in GitHub Desktop.

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:

  • "Timestamp"
  • "Your email"
  • "Who invite you"

3. From the resultant google sheet, go to "Tools" -> "Script Editor"

// "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 the specific slack 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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment