A simple Proof of Concept (POC) for sending notifications to Slack using the slack-notify library in a Node.js environment.
- Send simple text messages to Slack.
- Support for custom channels, icons, and usernames.
- Pre-configured methods for common notification types:
bug,success, andalert. - Advanced formatting using Slack attachments and fields.
- Promise-based API.
- Node.js (v14 or higher recommended)
- A Slack Incoming Webhook URL. You can create one by setting up an "Incoming Webhooks" integration in your Slack workspace.
- Clone the repository or download the source code.
- Install the dependencies:
npm installOpen slack-notify.js and replace the placeholder webhook URL with your actual Slack webhook URL:
const MY_SLACK_WEBHOOK_URL = 'https://hooks.slack.com/services/YOUR/WEBHOOK/URL';Run the script using Node.js:
node slack-notify.jsThe slack-notify.js file includes several examples:
slack.send('Hello!')
.then(() => console.log('Done!'))
.catch(err => console.error(err));slack.send({
channel: '#custom-channel',
icon_url: 'http://example.com/icon.png',
text: 'Custom notification message',
username: 'CustomBot'
});slack.bug('Something broke!'); // Posts to #bugs by default
slack.success('Operation successful!'); // Posts to #alerts by default
slack.alert('Important notice!'); // Posts to #alerts by defaultslack.alert({
text: 'Server Stats',
fields: {
'CPU usage': '7.51%',
'Memory usage': '254mb'
}
});