Node.js module for sending posts to Slack via the incoming webhooks API. Supports both simple and advanced messaging formats.
MIT License
Node.js module for sending posts to Slack via the incoming webhooks API. Supports both simple and advanced messaging formats.
An existing incoming webhook integration will be required and can be created via the Slack administration system to successfully use this module.
For further API details refer to the Incoming Webhooks and Message Attachments (advanced messaging) documents.
slackPost
message instance.webhookURL
must be in the format expected by the Slack administration integration endpoint - method will throw an error if web hook URL invalid.postText
is implemented as follows:
Example:
let slackPost = require('slackpost');
let myNewPost = slackPost.post(
'https://hooks.slack.com/services/ABCDEF012/012345ABC/fjdke456HRekdftFOGRPh21s',
'Hello, HAL. Do you read me, HAL?'
);
slackPost
instance.#channel
or @username
, anything else will throw an error.slackPost
instance.:
characters.slackPost
instance.Example:
let slackPost = require('slackpost');
let myNewPost = slackPost.post(WEBHOOK_URL,'Message');
// set the post icon to ":chicken:"
myNewPost.setIconEmoji('chicken');
slackPost
instance.slackPost
instance.slackPost
instance.color
is either a HTML color code (e.g. #439fe0
) or one of good
, warning
or danger
.require('slackpost').COLOR_LIST
.color
is undefined
then GOOD
will be used by default.slackPost
instance.Example:
let slackPost = require('slackpost');
let myNewPost = slackPost.post(WEBHOOK_URL,'Message');
// color options
myNewPost.setColor(slackPost.COLOR_LIST.GOOD);
myNewPost.setColor(slackPost.COLOR_LIST.WARNING);
myNewPost.setColor(slackPost.COLOR_LIST.DANGER);
myNewPost.setColor('#439fe0');
enableMarkdown
is true, will action Slack Markdown-like formatting of given preText
.slackPost
instance.authorURL
allows setting of a URL for the author (will link both the name
and iconURL
within the rendered Slack post).iconURL
will set a small 16x16px image to the left of the author name
.slackPost
instance.title
, in bold text near the top of the message area.URL
allows for the title to be hyperlinked.slackPost
instance.richText
(main text) for an advanced message post.
enableMarkdown
is true, will action Slack Markdown-like formatting of given richText
.slackPost
instance.isShort
boolean controls if field data is considered short enough to allow side-by-side tabular display with the following/next field item, otherwise field title
/value
will consume its own full table row.slackPost
instance.Example:
let slackPost = require('slackpost');
let myNewPost = slackPost.post(WEBHOOK_URL,'Message');
// add some fields - Name and Company will appear side-by-side
myNewPost.addField('Name','Don Draper',true);
myNewPost.addField('Company','Sterling Cooper',true);
// Job title field will appear on its own row
myNewPost.addField('Job title','Creative Director');
slackPost.addField()
with Markdown-like syntax.slackPost
instance.slackPost
instance.slackPost
instance.timestamp
provided as a Unix timestamp will display a reference date/time to the right of the footer credit.iconURL
will set a small 16x16px image to the left of the footer text
.slackPost
instance.callback
is a function, receiving exactly one argument:
null
.Error()
.Sending a simple message:
let slackPost = require('slackpost');
let simpleMsg = slackPost.post(
'https://hooks.slack.com/services/ABCDEF012/012345ABC/fjdke456HRekdftFOGRPh21s',
'Hello, HAL. Do you read me, HAL?'
);
simpleMsg
.setUsername('HAL9000')
.setChannel('#spaceship')
.enableUnfurlLinks()
.disableMarkdown();
simpleMsg.send((err) => {
if (err) {
// error sending message to Slack API
console.dir(err);
return;
}
// success!
});
Sending an advanced message:
let slackPost = require('slackpost');
let advancedMsg = slackPost.post(
'https://hooks.slack.com/services/ABCDEF012/012345ABC/fjdke456HRekdftFOGRPh21s',
'This is my fallback text for mobile notifications and IRC users/etc.'
);
advancedMsg
.setUsername('magnetik')
.setChannel('@alexandra')
.setColor(slackPost.COLOR_LIST.WARNING)
.setAuthor(
'Peter Mescalchin',
'http://magnetikonline.com'
)
.setRichText('This is some *rich-text* messaging!',true);
advancedMsg.send((err) => {
if (err) {
// error sending message to Slack API
console.dir(err);
return;
}
// success!
});