Skip to content

Instantly share code, notes, and snippets.

@jenil
Last active April 23, 2021 09:17
Show Gist options
  • Save jenil/6f8663aa44209c9f4dd9dd8fffc149b3 to your computer and use it in GitHub Desktop.
Save jenil/6f8663aa44209c9f4dd9dd8fffc149b3 to your computer and use it in GitHub Desktop.
Figma webhook to Slack notifications
const https = require("https");
exports.handler = (req, ctx, callback) => {
const event = JSON.parse(req.body);
console.log("Event:", event);
let response = {
statusCode: 200,
body: "ok"
};
if (event && event.passcode == "asd" && !event.ping) {
let data = JSON.stringify({
"blocks": [{
"type": "section",
"text": {
"type": "mrkdwn",
"text": `Update to *${event.file_name}* was made - _${event.label}_`
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": event.description
},
"accessory": {
"type": "button",
"text": {
"type": "plain_text",
"text": "View",
"emoji": true
},
"url": "https://www.figma.com/file/" + event.file_key
}
}
]
}).replace(/[\u007f-\uffff]/g, function(c) { // handle emoji
return "\\u" + ("0000" + c.charCodeAt(0).toString(16)).slice(-4);
});
const options = {
hostname: "hooks.slack.com",
path: "/services/asdasdasdad",
port: "443",
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": data.length
}
};
const req = https.request(options, res => {
res.setEncoding("utf8");
res.on("data", chunk => {
console.log("Successfully posted", chunk);
callback(null, {
statusCode: 200,
data: chunk
});
});
res.on("end", () => {
callback(null, {
statusCode: 200,
data: "end"
});
});
});
req.on("error", e => {
console.error(e);
callback(null, {
statusCode: 503,
data: null,
error: e
});
});
req.write(data);
req.end();
}
else {
callback(null, response);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment