Skip to content

Instantly share code, notes, and snippets.

@andrewdodd
Forked from scripting/sendToSlack.js
Last active May 16, 2016 11:07
Show Gist options
  • Save andrewdodd/5f7bb2c6a8a475778add2c6e906e4927 to your computer and use it in GitHub Desktop.
Save andrewdodd/5f7bb2c6a8a475778add2c6e906e4927 to your computer and use it in GitHub Desktop.
A tiny JavaScript app that sends a message to your default Slack channel. Can be customized with a name, icon, emoji or sent to a different channel. Runs in Node.js.
import fetch from 'isomorphic-fetch'
var urlWebHook = "https://hooks.slack.com/services/abcdef"; //the URL you get on your "incoming web hooks" page.
function sendToSlack(s, theUsername, theIconUrl, theIconEmoji, theChannel) {
var payload = {
text: s
};
if (theUsername !== undefined) {
payload.username = theUsername;
}
if (theIconUrl !== undefined) {
payload.icon_url = theIconUrl;
}
if (theIconEmoji !== undefined) {
payload.icon_emoji = theIconEmoji;
}
if (theChannel !== undefined) {
payload.channel = theChannel;
}
fetch(urlWebHook, {
method: 'post',
mode: 'no-cors',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
})
}
sendToSlack("Hello World");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment