Last active
July 10, 2023 04:09
-
-
Save ringtail003/712e5122e5362d0dd638e2d16b6526b8 to your computer and use it in GitHub Desktop.
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
const configs = { | |
slackMessageTrigger: "slackToMail", | |
slackAcceptedMessage: "受け付けました", | |
slackIncomingWebhookUrl: "", // ★ | |
spreadSheetFileId: "", // ★ | |
spreadSheetSheetName: "シート1", // ★ | |
mailTo: "to@example.co.jp", // ★ | |
mailFrom: "from@example.co.jp", // ★ | |
mailSubjectPrefix: "【テスト】", | |
}; | |
const emailSectionMap = [ | |
["title", "タイトル"], | |
["body", "本文"], | |
["requester", "リクエスタ"], | |
]; | |
function doPost(e) { | |
const postMessage = detectPostMessageOrDebugString(e); | |
const payload = convertPostMessageToPayload(postMessage); | |
writeSpreadSheet( | |
configs.spreadSheetFileId, | |
configs.spreadSheetSheetName, | |
payload, | |
); | |
sendEmail( | |
configs.mailTo, | |
configs.mailFrom, | |
configs.mailSubjectPrefix, | |
payload | |
); | |
sendToSlackAcceptedMessage( | |
configs.slackIncomingWebhookUrl, | |
configs.slackAcceptedMessage, | |
); | |
return ContentService.createTextOutput("処理が完了しました"); | |
} | |
/* | |
@param {eventParameter} event - POSTイベントパラメータ @see https://developers.google.com/apps-script/guides/web?hl=ja#request_parameters | |
@return {string} - 文字列変換したもの、またはデバッグ実行した文字列 | |
*/ | |
function detectPostMessageOrDebugString(event) { | |
if (event?.postData) { | |
return event.postData.getDataAsString(); | |
} | |
return "token=xx" + | |
"&team_id=xx" + | |
"&team_domain=xx" + | |
"&service_id=xx" + | |
"&channel_id=xx" + | |
"&channel_name=xx" + | |
"×tamp=0.0" + | |
"&user_id=xx" + | |
"&user_name=xx" + | |
"&text=" + encodeURIComponent(`${configs.slackMessageTrigger}` + | |
",,title::TEST_TITLE" + | |
",,body::TEST_BODY" + | |
",,requester::TEST_USER") + | |
`&trigger_word=${configs.slackMessageTrigger}` | |
; | |
} | |
/* | |
@param {string} postMessage - POSTされた文字列 e.g. "token=xx&text=trigger,,title::a,,body::b" | |
@return {object} - textフィールドをオブジェクトに変換したもの e.g. { title: "a", body: "b" } | |
*/ | |
function convertPostMessageToPayload(postMessage) { | |
const textField = postMessage | |
.split("&") | |
.map(v => v.split("=")) | |
.find(v => v[0] === "text") | |
; | |
const payload = decodeURIComponent(textField[1]) | |
.split(",,") | |
.map(v => v.split("::")) | |
.reduce((acc, [key, value]) => { | |
if (value) { | |
acc[key] = value; | |
} | |
return acc; | |
}, {}) | |
; | |
return { | |
postMessage, | |
...payload, | |
} | |
} | |
/* | |
@param {string} spreadSheetFileId - 書き込みするファイルのID | |
@param {string} spreadSheetName - 書き込みするシート名 | |
@param {object} payload - POSTされた内容 | |
*/ | |
function writeSpreadSheet(spreadSheetFileId, spreadSheetName, payload) { | |
const sheet = SpreadsheetApp | |
.openById(spreadSheetFileId) | |
.getSheetByName(spreadSheetName) | |
; | |
const values = [ | |
Utilities.formatDate(new Date(), "JST", "yyyy/MM/dd HH:mm:ss"), | |
...Object.values(payload), | |
]; | |
sheet.insertRows(1, 1); | |
sheet.getRange(1, 1, 1, values.length).setValues([values]); | |
} | |
/* | |
@param {string} mailTo - 送信先アドレス e.g. "foo@bar.com" | |
@param {string} mailFrom - 送信元アドレス e.g. "foo@bar.com" | |
@param {string} mailSubjectPrefix - メールの件名 e.g. "XXXのお知らせ" | |
@param {object} payload - POSTされた内容 | |
*/ | |
function sendEmail(mailTo, mailFrom, mailSubjectPrefix, payload) { | |
const subject = `${mailSubjectPrefix}${payload.title}`; | |
const body = emailSectionMap.map(([key, label]) => { | |
return `【${label}】\r\n${payload[key]}\r\n`; | |
}).join("\r\n"); | |
const options = { | |
from: mailFrom, | |
}; | |
GmailApp.sendEmail(mailTo, subject, body, options); | |
} | |
/* | |
@param {string} message - Slackに受付完了を知らせるメッセージ | |
*/ | |
function sendToSlackAcceptedMessage(url, message) { | |
if (!url) { | |
return; | |
} | |
const payload = JSON.stringify({ | |
text: message, | |
icon_emoji: ":robot_face:" | |
}); | |
const options = { | |
method: "post", | |
contentType: "application/json", | |
payload, | |
}; | |
UrlFetchApp.fetch(url, options); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment