Last active
August 15, 2018 09:26
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use:
Set
onFormSubmit
as the trigger.onFormSubmit
をトリガーとして設定する。