slack-notify
A simple, flexible, zero-dependency Node.js wrapper around the Slack webhook API. Makes it easy to send notifications to Slack from your application.
Installation
npm install slack-notify
Usage
import SlackNotify from 'slack-notify';
const MY_SLACK_WEBHOOK_URL = 'https://hooks.slack.com/services/RANDOMCHARS';
const slack = SlackNotify(MY_SLACK_WEBHOOK_URL);
slack.send('Hello!')
.then(() => {
console.log('done!');
}).catch((err) => {
console.error(err);
});
slack.send({
channel: '#myCustomChannelName',
icon_url: 'http://example.com/my-icon.png',
text: 'Here is my notification',
unfurl_links: 1,
username: 'Jimmy'
});
var statLog = slack.extend({
channel: '#statistics',
icon_emoji: ':computer:',
username: 'Statistics'
});
statLog({
text: 'Current server statistics',
fields: {
'CPU usage': '7.51%',
'Memory usage': '254mb'
}
});
slack.send('Hello!').then(() => {
console.log('Done!');
}).catch((err) => {
console.error('API error:', err);
})
slack.bug('Something broke!');
slack.success('Something happened correctly!');
slack.alert('Something important!');
slack.alert({
text: 'Current server stats',
fields: {
'CPU usage': '7.51%',
'Memory usage': '254mb'
}
});
slack.alert({
text: 'Current server stats',
attachments: [
{
fallback: 'Required Fallback String',
fields: [
{ title: 'CPU usage', value: '7.51%', short: true },
{ title: 'Memory usage', value: '254mb', short: true }
]
}
]
});
slack.alert({
text: 'Current server stats',
attachments: [
{
fallback: 'Required Fallback String',
color: '#FF0000',
fields: [
{ title: 'CPU usage', value: '90%', short: true },
{ title: 'Memory usage', value: '254mb', short: true }
]
}
]
});
Running the Test Suite
npm install
npm test
CJS / ESM support
This library supports CommonJS and ES Modules.
const SlackNotify = require('slack-notify');
import SlackNotify from 'slack-notify';
const slack = SlackNotify(MY_SLACK_WEBHOOK_URL);
License
MIT. Copyright © 2014-2023 Andrew Childs