Created
May 25, 2020 06:20
-
-
Save hisasann/c93771ced7bfb2306d53ff4140a54f99 to your computer and use it in GitHub Desktop.
GoogleFormからのSubmitをトリガーにしてSlackに通知するGoogleAppsScript
This file contains 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
// 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