Skip to content

Instantly share code, notes, and snippets.

@akash1810
Last active October 13, 2021 13:42
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save akash1810/640a45d5997de1d95f4a to your computer and use it in GitHub Desktop.
Save akash1810/640a45d5997de1d95f4a to your computer and use it in GitHub Desktop.
Google Apps Script to post a message to Slack when someone responds to a Google Form.
/**
* ABOUT
* Google Apps Script to post a message to Slack when someone responds to a Google Form.
*
* Uses Slack incoming webhooks - https://api.slack.com/incoming-webhooks
* and FormsResponse - https://developers.google.com/apps-script/reference/forms/form-response
*
*
* AUTHOR
* Akash A <github.com/akash1810>
*
*
* USAGE
* Free to use.
*/
// Create an incoming webhook here - https://api.slack.com/incoming-webhooks
var POST_URL = "https://hooks.slack.com/services/XXXX/XXXX/XXXX";
function onSubmit(e) {
var response = e.response.getItemResponses();
var fields = [
{"title": "From", "value": e.response.getRespondentEmail()},
{"title": "When", "value": e.response.getTimestamp()}
];
for (var i = 0; i < response.length; i++) {
var question = response[i].getItem().getTitle();
var answer = response[i].getResponse();
fields.push({"title": question, "value": answer});
}
var summaryAttachment = {
"fallback": FormApp.getActiveForm().getTitle(),
"pretext": "<!channel> New response submitted to: " + FormApp.getActiveForm().getTitle(),
"title": FormApp.getActiveForm().getTitle() + " (responses)",
"title_link": "https://docs.google.com/spreadsheets/d/" + FormApp.getActiveForm().getDestinationId(),
"fields": fields,
"color": "#393939"
};
var responseAttachment = {
"fallback": FormApp.getActiveForm().getTitle(),
"title": "Respond via email? (mailto link)",
"title_link": "mailto:" + e.response.getRespondentEmail() + "?Subject=" + encodeURI(FormApp.getActiveForm().getTitle())
};
var options = {
"method" : "post",
"payload": JSON.stringify({
"username": "Feedback",
"icon_emoji": ":speech_balloon:",
"attachments": [summaryAttachment, responseAttachment]
})
};
UrlFetchApp.fetch(POST_URL, options);
};
@akash1810
Copy link
Author

For response attachment to open in gmail:

var responseAttachment = {
    "fallback": FormApp.getActiveForm().getTitle(),
    "title": "Respond via email?",
    "title_link": "https://mail.google.com/mail/u/0/?view=cm&fs=1&tf=1&source=mailto&to=" + e.response.getRespondentEmail()
  };

@gchiacchio
Copy link

Not working anymore?

@akash1810
Copy link
Author

@gchiacchio sorry, I missed your message.

I've just setup a new Google form and got it working. It didn't work right away though - I used View -> Execution transcript to debug which told me there was no Google Sheet attached to the form for responses, which is needed.

What errors are you seeing?

@laurentades
Copy link

Hi.
Got this error for transcript...

[17-02-13 13:32:56:561 GST] Starting execution
[17-02-13 13:32:56:570 GST] Execution failed: TypeError: Cannot read property "response" from undefined. (line 21, file "Code") [0.001 seconds total runtime]

Pretty new to google scripts... so that might be really trivial.... :-S

Thanks

@laurentades
Copy link

My bad... works perfectly: had not done the project trigger bit...

Great code. Simple and to the point

@akash1810
Copy link
Author

@laurentades no worries. Glad you find it useful. 😄

@scottyab
Copy link

Hey @laurentades I also have the "TypeError: Cannot read property "response" from undefined. (line 21, file "Code") " - how did you fix? project trigger? (I've accepted the authorisations). also Thx @akash1810 for the script.

@sethburtonhall
Copy link

sethburtonhall commented May 30, 2017

Same error for me as well.
TypeError: Cannot read property "response" from undefined. (line 21, file "Code")

I have set up the trigger, fyi.

@kylegibson
Copy link

I was having this same issue. I solved it by removing the trigger, and then creating it again.

@shawnweber
Copy link

This script is very elegant and works beautifully. However, Google Forms just released a feature where you can set a questions as 'file upload' and accept a file. How can I use this script to handle the file as a 'value' to pass to Slack? I've used the Logger to debug and I can see that the 'answer' variable can hold the file submitted through Google Forms, and pushes it to the 'fields' variable. However, it doesn't get passed through to Slack. Any suggestions to handle the new file upload question?

@tommycopeland
Copy link

Nice script. How would you modify it to only apply to certain scripts? I'm not sure where to install the getId() method, whether as a variable or somehow nested in the getItemResponses method?

@Soegianto
Copy link

Soegianto commented Sep 1, 2020

This is my experience as a beginner :

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

Solution : Ensure Google Sheet attached to the form for responses, in the Form Response you can click spreadsheet logo to create spreadsheet

Screen Shot 2020-09-01 at 19 08 25

Also :

  • Ensure that you create the script inside the form,

Screen Shot 2020-09-01 at 19 13 47

  • Ensure to create a trigger (from the script menu > Current Trigger > add Trigger

image

@Soegianto
Copy link

I have questions, my forms is using checkbox, it seems the answer is not posted on slack, can you help me? @akash1810 ?

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