Skip to content

Instantly share code, notes, and snippets.

@andychase
Last active February 15, 2024 07:53
Show Gist options
  • Star 37 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • Save andychase/e0b1eaa1b9ecc00ef2d2 to your computer and use it in GitHub Desktop.
Save andychase/e0b1eaa1b9ecc00ef2d2 to your computer and use it in GitHub Desktop.
Google Forms Slack Notification
// Google Forms Slack Notification
// 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/";
function onSubmit(e) {
var response = e.response.getItemResponses();
// Setup 2:
// Modify the below to make the message you want.
// See: https://developers.google.com/apps-script/reference/forms/form-response
var d = e.response.getRespondentEmail() + " | " + response[0].getResponse();
var payload =
{ "payload": '{"text": "' + d + '"}' }
var options =
{
"method" : "post",
"payload" : payload
};
UrlFetchApp.fetch(POST_URL, options);
};
@llauren
Copy link

llauren commented Sep 16, 2015

Thanks! Just what i needed!

@andruwa13
Copy link

TypeError: Can not read property "response" object undefined.

@sahilk
Copy link

sahilk commented Oct 23, 2015

Thanks a ton, Andy! Works flawlessly.

@nielsvanspauwen
Copy link

Works great, thanks for sharing!

@ilandbt
Copy link

ilandbt commented Apr 9, 2016

TypeError: Can not read property "response" object undefined.
can anyone help me with this?

Copy link

ghost commented Apr 24, 2016

This is fantastic! I was able to configure and use the code you provided andychase, many thanks!

One question, how do I configure the output of the form to grab all responses and the question names? Ideally I would like the message in slack to be something like:

"Question 1" | "Answer 1"
"Question 2" | "Answer 2"
... for all questions in form

I think I need a for loop setup to scroll through and grab all the answers and another for every question. I cannot figure out how to do this. Can you provide any solutions for directions that I may be able to expand my search?

@ldanielswakman
Copy link

I've been getting an error for this snippet, which used to run smoothly without problems for the past year. It says Authorization is required to perform that action., which I found here. I re-authorised the script, which prompted the 'Review authorisations' as mentioned, but it didn't solve the issue. Any ideas?

image

@rahim-figs
Copy link

How do I get the API link to a private slack channel?

@rpsene
Copy link

rpsene commented May 9, 2017

if (typeof e === "undefined") {
e = {namedValues: {"blah": ["blah"]};
messagePretext = "blah blah blah";
}

@jolanbailey
Copy link

Any solutions for the following?

TypeError: Cannot read property "response" from undefined. (line 11, file "Code")

@jenkliu
Copy link

jenkliu commented Oct 3, 2018

Looks like Google changed their menu organization, so line 5 should be:

// Install 2: ( Edit > current project triggers ) ( [onSubmit], [from Form], [On form submit] )

("Current project triggers" is under "Edit", not "Resources".) After that it worked like a charm. Thanks for sharing!

@nicoarapaul
Copy link

I try the above script but give 400 response the Post call, and then I update this:
var payload =
{
"text": d
}
var options =
{
'method' : "POST",
'contentType': 'application/json',
'payload' : JSON.stringify(payload)
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment