Slack Post

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 see Incoming Webhooks and Message Attachments (advanced messaging) Slack API documents.

Methods

Returns a new Slack Post message object instance.

webhookURL must be in the format expected by the Slack administration integration endpoint - method will throw an error if web hook URL invalid.

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: For simple messages (text posts) this will be the message text. For advanced messages will be used as the fall back text for scenarios where advanced rendering is unsupported.

is implemented as follows:

Example:

var slackPost = require ( ' slackpost ' ) ; var myNewPost = slackPost . post ( ' https://hooks.slack.com/services/ABCDEF012/012345ABC/fjdke456HRekdftFOGRPh21s ' , ' Hello, HAL. Do you read me, HAL? ' ) ;

Override default username for the incoming webhook.

Returns slackPost object instance.

Override default target channel for the incoming webhook with either: An alternative channel. Direct message Slack username.

Format must be one of #channel or @username , anything else will throw an error.

or , anything else will throw an error. Returns slackPost object instance.

Override default icon for incoming webhook with a defined emoji.

Provide desired emoji name without leading/trailing : characters.

characters. Returns slackPost object instance.

Example:

var slackPost = require ( ' slackpost ' ) ; var myNewPost = slackPost . post ( WEBHOOK_URL , ' Message ' ) ; myNewPost . setIconEmoji ( ' chicken ' ) ;

Override the default icon for incoming webhook with a public image URL.

Returns slackPost object instance.

slackPost.enableUnfurlLinks()

When enabled, Slack will automatically attempt to extract and display summarized details for URLs within the post content.

By default URLs referenced in posts made by an incoming webhook will not be unfurled - unless they are deemed media content links.

Returns slackPost object instance.

When disabled, Slack will avoid marking up post text with Markdown-like syntax.

Method applies only to the simple message format, which by default is automatically marked up.

Returns slackPost object instance.

Sets left hand border color for advanced message format posts.

Given color is either a HTML color code (e.g. #439fe0 ) or one of good , warning or danger .

is either a HTML color code (e.g. ) or one of , or . Color names are also defined at require('slackpost').COLOR_LIST .

. If color is undefined then GOOD will be used by default.

is then will be used by default. Returns slackPost object instance.

Example:

var slackPost = require ( ' slackpost ' ) ; var myNewPost = slackPost . post ( WEBHOOK_URL , ' Message ' ) ; myNewPost . setColor ( slackPost . COLOR_LIST [ ' GOOD ' ] ) ; myNewPost . setColor ( slackPost . COLOR_LIST [ ' WARNING ' ] ) ; myNewPost . setColor ( slackPost . COLOR_LIST [ ' DANGER ' ] ) ; myNewPost . setColor ( ' #439fe0 ' ) ;

Set the optional text that appears above the advanced message block.

If enableMarkdown is true, will action Slack Markdown-like formatting of given preText .

is true, will action Slack Markdown-like formatting of given . When called will enable the advanced message format.

Returns slackPost object instance.

Sets a small display section at the top of the message for the post author.

Optional authorURL allows setting of a URL for the author (will link both the name and iconURL within the rendered Slack post).

allows setting of a URL for the author (will link both the and within the rendered Slack post). Optional iconURL will set a small 16x16px image to the left of the author name .

will set a small 16x16px image to the left of the author . When called will enable the advanced message format.

Returns slackPost object instance.

Sets a title , in bold text near the top of the message area.

, in bold text near the top of the message area. Optional URL allows for the title to be hyperlinked.

allows for the title to be hyperlinked. When called will enable the advanced message format.

Returns slackPost object instance.

Sets the richText (main text) for an advanced message post. Content will automatically collapse if it contains 700+ characters or 5+ linebreaks, and will display a "Show more..." link to expand the content.

(main text) for an advanced message post. If enableMarkdown is true, will action Slack Markdown-like formatting of given richText .

is true, will action Slack Markdown-like formatting of given . When called will enable the advanced message format.

Returns slackPost object instance.

Adds message meta data in a tabular format at the footer of the message area. Method can be called multiple times to add multiple field items to the rendered table.

Optional 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.

boolean controls if field data is considered short enough to allow side-by-side tabular display with the following/next field item, otherwise field / will consume its own full table row. When called will enable the advanced message format.

Returns slackPost object instance.

Example:

var slackPost = require ( ' slackpost ' ) ; var myNewPost = slackPost . post ( WEBHOOK_URL , ' Message ' ) ; myNewPost . addField ( ' Name ' , ' Don Draper ' , true ) ; myNewPost . addField ( ' Company ' , ' Sterling Cooper ' , true ) ; myNewPost . addField ( ' Job title ' , ' Creative Director ' ) ;

When called, will action Slack to markup field item values added via slackPost.addField() with Markdown-like syntax.

with Markdown-like syntax. Method only applies to the advanced message format with one or more fields created.

Returns slackPost object instance.

Provides a public URL to an image that will be displayed as a thumbnail to the right of an advanced message.

Image formats of GIF, JPEG, PNG and BMP are supported.

When called will enable the advanced message format.

Returns slackPost object instance.

Provides a public URL to an image that will be displayed as an image inside the message area.

Image formats of GIF, JPEG, PNG and BMP are supported.

Large images will be resized to a maximum width of 400px or a maximum height of 500px - whilst maintaining aspect ratio.

When called will enable the advanced message format.

Returns slackPost object instance.

slackPost.setFooter(text[,timestamp][,iconURL])

Adds brief text to contextualize and identify referenced post content.

Optional timestamp provided as a Unix timestamp will display a reference date/time to the right of the footer credit.

provided as a Unix timestamp will display a reference date/time to the right of the footer credit. Optional iconURL will set a small 16x16px image to the left of the footer text .

will set a small 16x16px image to the left of the footer . When called will enable the advanced message format.

Returns slackPost object instance.

Sends a composed message, using the methods presented above to the Slack incoming webhook API endpoint.

callback is expected to be a function, receiving one parameter: Upon success parameter will be null . In case of error, parameter will be an instance of Error() .

is expected to be a function, receiving one parameter:

Example usage

Sending a simple message:

var slackPost = require ( ' slackpost ' ) ; var 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 ) { console . dir ( err ) ; return ; } } ) ;

Sending an advanced message: