Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save allilevine/6fe95d260d540a3c985ea8e637b33a80 to your computer and use it in GitHub Desktop.
Save allilevine/6fe95d260d540a3c985ea8e637b33a80 to your computer and use it in GitHub Desktop.
  1. Open the spreadsheet where your Google Form responses are collected.

  2. Go to "Tools" -> "Script Editor"

  3. Copy and paste the script below.

  4. For SlackName, fill in your Slack's address/slug (e.g. from https://your-slack-name-is-here.slack.com).

  5. 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).

  6. 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).

  7. For ccEmail, fill in your email (it will receive the sign up notifications).

  8. 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.

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