Created
July 22, 2022 16:07
-
-
Save paulcushing/b1d007e79cdb6c5c279885b55c1d1ad3 to your computer and use it in GitHub Desktop.
A Digital Ocean function that accepts a message and sends it to your Slack channel.
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 fetch = require("node-fetch"); | |
let slackToken = process.env.slack_token | |
function main(args) { | |
if (!args.name || !args.email || !args.message || !args.site) { | |
return {"body": "Error - missing arguments"} | |
} | |
let name = args.name | |
let email = args.email | |
let message = args.message | |
let site = args.site | |
const payload = { | |
channel: "contact_form", | |
icon_url: "https://www.thepaulcushing.com/content/images/size/w256h256/2019/11/android-chrome-512x512-1.png", | |
blocks: [ | |
{ | |
type: "section", | |
text: { | |
type: "mrkdwn", | |
text: `*Message From:* ${name} - ${email}`, | |
}, | |
}, | |
{ | |
type: "section", | |
text: { | |
type: "mrkdwn", | |
text: `${message}`, | |
}, | |
}, | |
{ | |
type: "section", | |
text: { | |
type: "mrkdwn", | |
text: `-- via ${site} --`, | |
}, | |
}, | |
], | |
} | |
return fetch("https://slack.com/api/chat.postMessage", { | |
method: "POST", | |
body: JSON.stringify(payload), | |
headers: { | |
"Content-Type": "application/json; charset=utf-8", | |
"Content-Length": payload.length, | |
Authorization: `Bearer ${slackToken}`, | |
Accept: "application/json", | |
}, | |
}) | |
.then((res) => { | |
if (!res.ok) { | |
console.log(`Server error ${res.status}`) | |
return {"body": "API Error"} | |
} | |
return {"body": "ok"} | |
}) | |
.catch((error) => { | |
console.log(error) | |
return {"body": "Error"} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment