Skip to content

Instantly share code, notes, and snippets.

@Siva7891
Forked from akash1810/slack-hook.gs
Created April 24, 2016 04:15
Show Gist options
  • Select an option

  • Save Siva7891/19d8fcf87fa3905d664ec3b65b628cb8 to your computer and use it in GitHub Desktop.

Select an option

Save Siva7891/19d8fcf87fa3905d664ec3b65b628cb8 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. Refer: https://akash1810.github.io/2015/08/02/getting-google-forms-to-talk-to-slack.html
/**
* 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);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment