Skip to content

Instantly share code, notes, and snippets.

@arayaryoma
Last active August 15, 2018 09:26
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 arayaryoma/cdf517bf787823a92bae1bbc52eeede4 to your computer and use it in GitHub Desktop.
Save arayaryoma/cdf517bf787823a92bae1bbc52eeede4 to your computer and use it in GitHub Desktop.
EN: Google App Script to send answers of google forms to Slack (Incoming Webhook) / JA: Google Formsの回答をSlackのIncoming Webhookを使ってSlackに通知するGAS
function onFormSubmit(event) {
try {
var itemResponses = event.response.getItemResponses();
var fields = [];
for(var i = 0; i < itemResponses.length; i++) {
var itemResponse = itemResponses[i];
var title = itemResponse.getItem().getTitle();
var res = itemResponse.getResponse();
fields.push(generateAttachmentField(title, res));
}
var data = {
icon_emoji: ':clap:',
text: 'A new answer was submitted',
attachments: [{
color: '#FFFFFF',
fields: fields
}]
};
sendMessageToSlack(data);
} catch(e) {
handleError(e);
}
}
function generateAttachmentField(title, value) {
if(value.length < 1) {
value = 'No Answer'
}
return {
title: title,
value: value,
short: false
}
}
function handleError(e) {
var data = {
text: 'Error:' + JSON.stringify(e)
}
sendMessageToSlack(data);
}
function sendMessageToSlack(data) {
var webhookUrl = ''; // Replace with your webhook url
var payload = JSON.stringify(data);
var options = {
method: 'POST',
contentType: 'application/json',
payload: payload
};
UrlFetchApp.fetch(webhookUrl, options);
}
@arayaryoma
Copy link
Author

How to use:
Set onFormSubmit as the trigger.
onFormSubmit をトリガーとして設定する。

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