Skip to content

Instantly share code, notes, and snippets.

@sparkedon
Forked from jpsilvashy/README.md
Last active November 30, 2018 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sparkedon/9c0296bcaba050dbb6f1ba2f17550e08 to your computer and use it in GitHub Desktop.
Save sparkedon/9c0296bcaba050dbb6f1ba2f17550e08 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 a new app,

Next, create an "incoming webhook" for your app, and swap in a url for the SLACK_WEBHOOK_POST_URL in our script in the script editor.

Then you'll need to edit the forms "triggers" so that our onSubmit function is called when a new form response is recorded:

'img'

'img'

Once you create the trigger, you're all done!

// This is a modification of the script found on Github here: https://gist.github.com/jpsilvashy/f7e7ac5b8f34663079dff52274ab6b7b
// I took the original script and, with the help of my friend @xorl (here on Github), got it to work properly with the code below.
// The purpose of this script is to post a "question of the day" to a Slack channel from a list of Google form submissions.
// So far it works really well! Some of the goals/ideas I have are:
// * request a response on command -- like !qotd
// * upvote/downvote/flag a response for review
var SLACK_WEBHOOK_POST_URL = "https://hooks.slack.com/services/EXAMPLE";
function onSubmit(e) {
var form = FormApp.getActiveForm();
var allResponses = form.getResponses();
var latestResponse = allResponses[allResponses.length - 1];
var latestEmail = allResponses[allResponses.length - 1].getRespondentEmail();
var itemResponses = latestResponse.getItemResponses();
var randomResponse = "";
// Email is always the first item
var randomNum = Math.floor(Math.random()*allResponses.length);
var questionEmail = allResponses[randomNum].getRespondentEmail();
var data = "*Question of the day:* " + allResponses[randomNum].getItemResponses()[0].getResponse() + " [Submitted by: " + questionEmail + "]";
// Stringify payload
var payload = {
payload: '{"text": "' + data + '"}'
};
// 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