Skip to content

Instantly share code, notes, and snippets.

@DeveloperMos
Forked from stekhn/post-to-slack.js
Created April 23, 2020 14:40
Show Gist options
  • Save DeveloperMos/db7d531cfada56935f93cce688011177 to your computer and use it in GitHub Desktop.
Save DeveloperMos/db7d531cfada56935f93cce688011177 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