Skip to content

Instantly share code, notes, and snippets.

@stekhn
Last active July 18, 2022 15:07
Show Gist options
  • Save stekhn/6ebbf059bbfbf6725402d9c163695b82 to your computer and use it in GitHub Desktop.
Save stekhn/6ebbf059bbfbf6725402d9c163695b82 to your computer and use it in GitHub Desktop.
Use webhooks for posting to Slack. Works great with Google Cloud Functions and Google Cloud Scheduler. The incoming webhook for your Slack team needs to be created beforehand.
const https = require('https');
exports.postToSlack = () => {
// Read more about Slack webhooks here: https://api.slack.com/messaging/webhooks
const webhookPath = '/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX'
const payload = {
'channel': '#mychannel',
'username': 'slackbot',
'icon_emoji': ':bot:',
'text': 'Bow to your robot overlord'
}
const options = {
hostname: 'hooks.slack.com',
path: webhookPath,
port: 443,
method: 'POST',
headers: {
'Accept': '*/*',
'Content-Type': 'application/json'
}
}
const req = https.request(options);
req.write(JSON.stringify(payload));
req.end();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment