Created
January 11, 2021 16:59
-
-
Save basiclines/0293ba1bb053e35c42661ed2e1b4bca5 to your computer and use it in GitHub Desktop.
AWS lambda for sending Slack messages from Figma Hooks API
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
const https = require('https'); | |
const querystring = require('querystring'); | |
/** | |
* Pass the data to send as `event.data`, and the request options as | |
* `event.options`. For more information see the HTTPS module documentation | |
* at https://nodejs.org/api/https.html. | |
* | |
* Will succeed with the response body. | |
*/ | |
exports.handler = (event, context, callback) => { | |
// passcode is configured in the Figma Hooks API | |
const passcode = 'your-passcode'; | |
// slack channel webhook url | |
const slackHookHostname = 'hooks.slack.com'; | |
const slackHookLocation = '/your-url-location/'; | |
function messageComment(hookPayload) { | |
let comment = []; | |
let color = '' | |
hookPayload.comment.map(item => { | |
if (!!item.mention) { | |
comment.push(`@${hookPayload.mentions.filter(mention => (mention.id == item.mention))[0].handle}`); | |
} else | |
if (!!item.text) { | |
comment.push(item.text); | |
} | |
}) | |
if (hookPayload.file_name.toLowerCase().search('production') != -1) { | |
color = '#52B287' | |
} else if (hookPayload.file_name.toLowerCase().search('proposals') != -1) { | |
color = '#FCC565' | |
} else if (hookPayload.file_name.toLowerCase().search('explorations') != -1) { | |
color = '#E86253' | |
} else { | |
color = '#979FBD' | |
} | |
return { | |
icon_emoji: ':figma:', | |
username: 'Figma', | |
attachments:[{ | |
fallback: `New comment in <https://www.figma.com/file/${hookPayload.file_key}/|${hookPayload.file_name}>`, | |
pretext: `New comment in <https://www.figma.com/file/${hookPayload.file_key}/|${hookPayload.file_name}>`, | |
color: color, | |
fields:[{ | |
title: hookPayload.triggered_by.handle, | |
value: comment.join(''), | |
short: false | |
}] | |
}] | |
} | |
} | |
function testMessage() { | |
return { | |
icon_emoji: ':figma:', | |
username: `Test`, | |
text: `test message` | |
} | |
} | |
console.log('invokation', event) | |
if (!!event.body) { | |
let invokationBody = JSON.parse(event.body) | |
if (invokationBody.passcode == passcode) { | |
const payload = querystring.stringify({ 'payload': JSON.stringify(messageComment(invokationBody)) }) | |
const options = { | |
hostname: slackHookHostname, | |
path: slackHookLocation, | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'Content-Length': Buffer.byteLength(payload) | |
} | |
} | |
const req = https.request(options, res => { | |
console.log(`STATUS: ${res.statusCode}`); | |
let body = '' | |
res.on('data', d => body += d) | |
res.on('end', () => { | |
console.log('sent to slack', event) | |
callback(null, event) | |
}) | |
}) | |
req.on('error', error => { | |
console.error(error) | |
}) | |
req.write(payload) | |
req.end() | |
} else { | |
callback(null, 'passcode not valid or not present') | |
} | |
} else { | |
callback(null, 'no body field present') | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment