diff --git a/src/index.ts b/src/index.ts index 7c4e165..f54d4f3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,7 +12,8 @@ function doPost(e: GoogleAppsScript.Events.DoPost) { const req: AlertRequest = JSON.parse(e.postData.contents) const alertMessages = req.alerts.map(alertToMessage) const text = alertMessages.join('\n\n') - sendMessage(text) + const channelID = e.parameter.channelID as string | undefined + sendMessage(text, channelID) } function getStatusEmoji(status: string) { @@ -37,24 +38,32 @@ ${link} `.trim() } -function sendMessage(message: string) { +function sendMessage(message: string, channelID: string | undefined) { + const sign = computeSignature(message) + const headers: Record = { + 'Content-Type': 'text/plain; charset=utf-8', + 'X-TRAQ-Signature': sign + } + if (channelID !== undefined) { + headers['X-TRAQ-Channel-Id'] = channelID + } + UrlFetchApp.fetch(`https://q.trap.jp/api/v3/webhooks/${WEBHOOK_ID}`, { + method: 'post', + contentType: 'text/plain; charset=utf-8', + headers: headers, + payload: message + }) +} + +function computeSignature(message: string) { const signature = Utilities.computeHmacSignature( Utilities.MacAlgorithm.HMAC_SHA_1, message, WEBHOOK_SECRET, Utilities.Charset.UTF_8 ) - const sign = signature.reduce((str, ch) => { + return signature.reduce((str, ch) => { const chr = (ch < 0 ? ch + 256 : ch).toString(16) return str + (chr.length === 1 ? '0' : '') + chr }, '') - UrlFetchApp.fetch(`https://q.trap.jp/api/v3/webhooks/${WEBHOOK_ID}`, { - method: 'post', - contentType: 'text/plain; charset=utf-8', - headers: { - 'Content-Type': 'text/plain; charset=utf-8', - 'X-TRAQ-Signature': sign - }, - payload: message - }) }