Skip to content

Instantly share code, notes, and snippets.

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 hisasann/c93771ced7bfb2306d53ff4140a54f99 to your computer and use it in GitHub Desktop.
Save hisasann/c93771ced7bfb2306d53ff4140a54f99 to your computer and use it in GitHub Desktop.
GoogleFormからのSubmitをトリガーにしてSlackに通知するGoogleAppsScript
// Google Form が Submit されたときに呼ばれる
function onFormSubmit(e) {
Logger.log('フォームが送信されたぞ');
const itemResponses = e.response.getItemResponses();
itemResponses.forEach(function (itemResponse, index) {
Logger.log('質問' + index + ': ' + itemResponse.getItem().getTitle());
Logger.log('回答' + index + ': ' + itemResponse.getResponse());
});
postMessage`
<@UserId>
🍏 111: ここに回答を
🍎 222: ${こんな感じで}
🍋 333: 埋め込んでいく
`;
}
// 引数の String テンプレートをもとに Slack にメッセージを送信する
function postMessage(strings, ...values) {
const value = unionStringTemplate(strings, values);
const options = {
'method': 'post',
'headers': {'Content-type': 'application/json'},
'payload': JSON.stringify({
'channel': '#slack-notify',
'attachments': [
{
'color': '#36a64f',
'title': 'ほげふが',
'title_link': '何かしらの URL',
'text': value || `Google Form からの投稿ではありません`,
}
]
})
};
UrlFetchApp.fetch('https://hooks.slack.com/services/XXXXXXXXXXXXXXXXXXXXXXXX', options);
}
// String テンプレートを結合する
function unionStringTemplate (strings, values) {
console.log(strings);
if (!strings) {
return '';
}
let res = '';
for (let i = 0, len = strings.length; i < len; i++) {
res += strings[i];
if (i >= values.length) {
break;
}
res += values[i];
}
console.log(res);
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment