Skip to content

Instantly share code, notes, and snippets.

@nothsa
Forked from jpsilvashy/README.md
Last active March 18, 2020 20:01
Show Gist options
  • Save nothsa/e335213c91194764ba0a9e6c45fe6ac0 to your computer and use it in GitHub Desktop.
Save nothsa/e335213c91194764ba0a9e6c45fe6ac0 to your computer and use it in GitHub Desktop.
Post Google Sheets form entries to Slack

Post Google Sheets form entries to Slack

By using Google Form's script editor, you can call Slack webhooks when form submissions are made. You can use this script to do things like creating a live feedback form for taking questions from an audience or notifying your team when someone signs up for an event.

Setup

First, be sure you're collecting the email address in the form:

'img'

Create a form script

Next, open the script editor:

'img'

Paste the contents of javascript file in this gist into the script editor:

'img'

You may need to authorize access to Google Docs:

'img'

Create a Slack app

Go To https://api.slack.com/apps and Create New App. Give it a name (e.g. "FormBot"), and select the Slack Workspace to use.

Next, go to the Incoming Webhooks section, turn it on, and Add New Webhook to Workspace. Select the channel where you want the form submissions to be posted, and Allow. Copy the newly-created Webhook URL and paste it into the SLACK_WEBHOOK_POST_URL variable in the script editor.

Finally, you'll need to edit the forms "triggers" so that our onSubmit function in the script is called when a new form response is submitted:

'img'

'img'

Once you create the trigger, you're all done! Test by submitting the form, and then checking the channel for the submitted data.

// Put your Slack webhook here, make sure its connected to the correct channel
var SLACK_WEBHOOK_POST_URL = "https://hooks.slack.com/services/EXAMPLE";
function onSubmit(entry) {
// Get item submitted
var response = entry.response.getItemResponses();
// Email is always the first item
var email = entry.response.getRespondentEmail();
var name = response[0].getResponse();
var neighbourhood = response[1].getResponse();
var aidType = response[2].getResponse();
var phone = (response[3].getResponse()) ? response[3].getResponse() : '';
// Stringify payload
var payload = {
payload: '{"text": "*Name:* '+name+' | *Email:* '+email+' | *Neighbourhood:* '+neighbourhood+' | *Aid Type:* '+aidType+' | *Phone:* '+phone+'"}'
};
// Build request
var options = {
method: "post",
payload: payload
};
// Send to Slack
UrlFetchApp.fetch(SLACK_WEBHOOK_POST_URL, options);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment