Slack Message Builder
Slack Message Builder is a node.js module that builds JSON documents that can be used to post messages to slack's chat.postMessage API. Can be used where ever you need to generate Slack message JSON especially in Slapp.
Install
npm install --save slack-message-builder
Usage
Basic Formatting
const smb = text'I am a test message http://slack.com' text"And here's an attachment!"
"text": "I am a test message http://slack.com" "attachments": "text": "And here's an attachment!"
Attachments
const smb = color"#36a64f" title"Slack API Documentation" text"Optional text that appears within the attachment" title"Priority" value"High"
"attachments": "fallback": "Required plain-text summary of the attachment." "color": "#36a64f" "pretext": "Optional text that appears above the attachment block" "author_name": "Bobby Tables" "author_link": "http://flickr.com/bobby/" "author_icon": "http://flickr.com/icons/bobby.jpg" "title": "Slack API Documentation" "title_link": "https://api.slack.com/" "text": "Optional text that appears within the attachment" "fields": "title": "Priority" "value": "High" "short": false "image_url": "http://my-website.com/path/to/image.jpg" "thumb_url": "http://example.com/path/to/thumb.png" "footer": "Slack API" "footer_icon": "https://platform.slack-edge.com/img/default_application_icon.png" "ts": 123456789
Buttons
const smb = text"Would you like to play a game?" text"Choose a game to play" color"#3AA3E3" name"chess" text"Chess" type"button" value"chess" name"maze" text"Falken's Maze" type"button" value"maze" name"war" text"Thermonuclear War" style"danger" type"button" value"war" title"Are you sure?" text"Wouldn't you prefer a good game of chess?"
"text": "Would you like to play a game?" "attachments": "text": "Choose a game to play" "fallback": "You are unable to choose a game" "callback_id": "wopr_game" "color": "#3AA3E3" "actions": "name": "chess" "text": "Chess" "type": "button" "value": "chess" "name": "maze" "text": "Falken's Maze" "type": "button" "value": "maze" "name": "war" "text": "Thermonuclear War" "style": "danger" "type": "button" "value": "war" "confirm": "title": "Are you sure?" "text": "Wouldn't you prefer a good game of chess?" "ok_text": "Yes" "dismiss_text": "No"
Message Menus (type=select)
Message menus:
const smb = text"Pick a user" text"" name"pick_user" text"Users" name"pick_channel" text"Channels" name"pick_value" text"Static" // isSelected = true name"pick_dynamic" text"Choose something dynamic!"
Produces:
text: 'Pickauser' attachments: text: '' fallback: 'Pickauser' callback_id: 'user_callback' actions: type: 'select' name: 'pick_user' text: 'Users' data_source: 'users' type: 'select' name: 'pick_channel' text: 'Channels' data_source: 'channels' type: 'select' name: 'pick_value' text: 'Static' options: text: 'some text' value: ' avalue' text: 'some more text' value: 'moar value' text: 'an object value' value: '{"foo":"bar"}' text: 'even more text' value: 'even moar value' description: "a description" selected: true type: 'select' name: 'pick_dynamic' text: 'Choosesomethingdynamic!' data_source: 'external'
Message Menus with Option Groups (type=select)
const smb = text'Pick a user' text'Pick a user' name'option_group' text'Static Option Groups' text'Option header' text'Second Option header'
Produces:
"text": "Pick a user" "attachments": "text": "Pick a user" "fallback": "Pick a user" "callback_id": "user_callback" "actions": "type": "select" "name": "option_group" "text": "Static Option Groups" "option_groups": "text": "Option header" "options": "text": "some text" "value": "a value" "text": "some more text" "value": "moar value" "text": "Second Option header" "options": "text": "some text" "value": "a value" "text": "some more text" "value": "moar value"
Modifying Original Messages
Slack message builder can also be used to modify existing messages, such as the original_message
that comes with an interactive message action. Consider the following example that uses the Slapp framework.
const slapp = slappaction'buttonCallbackId' 'action' { msg }
Using JSON
Mix and match JSON documents with slack-message-builder's functions
text"I am a test message"