Skip to content

Instantly share code, notes, and snippets.

@TkTech
Last active October 8, 2020 18:53
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 TkTech/3edcb2f46a93843686a062dbaf5367a6 to your computer and use it in GitHub Desktop.
Save TkTech/3edcb2f46a93843686a062dbaf5367a6 to your computer and use it in GitHub Desktop.
Post the results of a Google Form to a Slack channel webhook
var webhook = 'https://hooks.slack.com/services/***';
var header = 'A user requested an integration be reviewed.';
var whoLeader = 'Review requested by:';
function OnSubmit(e) {
var responses = e.response.getItemResponses();
UrlFetchApp.fetch(webhook, {
method: 'post',
payload: JSON.stringify({
'blocks': [{
'type': 'header',
'text': {
'type': 'plain_text',
'text': header
}
}, {
'type': 'section',
'fields': [{
'type': 'plain_text',
'text': whoLeader
}, {
'type': 'plain_text',
'text': e.response.getRespondentEmail()
}]
}].concat(makeBlocks(responses))
})
});
}
var makeBlocks = function(responses) {
var fields = [];
for (var i = 0; i < responses.length; i++) {
var response = responses[i];
var question = response.getItem().getTitle();
// For most response types, this gives you a nice simple string.
// 3 types give more complicated objects, which we do not yet handle:
// - CheckboxItem
// - GridItem
// - CheckboxGridItem
var answer = response.getResponse();
fields.push({
'type': 'section',
'fields': [{
'type': 'plain_text',
'text': question
}, {
'type': 'plain_text',
'text': answer
}]
});
}
return fields;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment