Skip to content

Instantly share code, notes, and snippets.

@JodiWarren
Forked from andychase/googleforms2slack.gs
Created October 7, 2015 09:42
Show Gist options
  • Save JodiWarren/224f1455101d2740ac64 to your computer and use it in GitHub Desktop.
Save JodiWarren/224f1455101d2740ac64 to your computer and use it in GitHub Desktop.
Google Forms Slack Notification
// Google Forms Slack Notification
// Jodi Warren <github.com/jodiwarren> based on a script by Andy Chase <github.com/andychase>
// License: CC0 1.0 Universal <creativecommons.org/publicdomain/zero/1.0>
// Install 1: This code goes in ( tools > script editor... ) of your google docs form
// Install 2: ( resources > current project triggers ) ( [onSubmit], [from Form], [On form submit] )
// Setup 1: Put your slack api url below
var POST_URL = "https://hooks.slack.com/services/1234567890"; // Change this or it won't work at all
// Customise your Slack message
var preambleText = "A new form entry has been submitted", // This is basically a title.
usernameOverride = null, // override the configured name, like "Mr. Bottington"
channelOverride = null, // set a specific channel for this to go into. Overrides the setting on Slack
iconUrl = null, // Set a fun picture as the icon
iconEmoji = null,
attachmentColour = '#195905'; // Or set an emoji as the icon, like so: ":ghost:"
// === BEWARE! ===
// === Below here lies nerd stuff ===
function onSubmit(e) {
var response = e.response.getItemResponses();
var attachmentFields = [];
// loop through each response, and add them to attachmentFields
// as a properly formatted object
for (var i = 0; i < response.length; i++) {
var thisResponse = response[i],
thisQuestion = thisResponse.getItem(),
thisAnswer = thisResponse.getResponse(),
thisField = {};
// Checkbox lists (and grid lists) return as an array, not a string
// So we need to turn it into a string. Also, if this is the case
// then it's probably going to be a long string
if ( Array.isArray(thisAnswer) ){
thisField.value = thisAnswer.join(' | ');
thisField.short = false;
} else {
// otherwise, the answer is the answer, and it'll probably be relatively short
thisField.value = thisAnswer;
thisField.short = true;
}
thisField.title = thisQuestion.getTitle();
attachmentFields.push(thisField);
}
// Setup 2:
// Modify the below to make the message you want.
// See: https://developers.google.com/apps-script/reference/forms/form-response
var payload =
{
"text": preambleText,
"username": usernameOverride,
"channel": channelOverride,
"icon_url": iconUrl,
"icon_emoji": iconEmoji,
"attachments" : [
{
"color": attachmentColour,
"fields": attachmentFields
}
]
};
var options =
{
"method" : "post",
"payload" : JSON.stringify(payload)
};
UrlFetchApp.fetch(POST_URL, options);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment