npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

slack-message-builder

v1.2.1

Published

Library for building and manipulating messages for the Slack API

Downloads

5,237

Readme

Sponsored by Beep Boop Build Status Coverage Status

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 = require('slack-message-builder')
smb().text('I am a test message http://slack.com')
  .attachment()
    .text("And here's an attachment!")
  .end()
.json()

Produces

{
    "text": "I am a test message http://slack.com",
    "attachments": [
        {
            "text": "And here's an attachment!"
        }
    ]
}

Attachments

const smb = require('slack-message-builder')
smb()
  .attachment()
    .fallback("Required plain-text summary of the attachment.")
    .color("#36a64f")
    .pretext("Optional text that appears above the attachment block")
    .authorName("Bobby Tables")
    .authorLink("http://flickr.com/bobby/")
    .authorIcon("http://flickr.com/icons/bobby.jpg")
    .title("Slack API Documentation")
    .titleLink("https://api.slack.com/")
    .text("Optional text that appears within the attachment")
    .field()
      .title("Priority")
      .value("High")
      .short(false)
    .end()
    .imageUrl("http://my-website.com/path/to/image.jpg")
    .thumbUrl("http://example.com/path/to/thumb.png")
    .footer("Slack API")
    .footerIcon("https://platform.slack-edge.com/img/default_application_icon.png")
    .ts(12345678)
  .end()
.json()

Produces

{
    "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 = require('slack-message-builder')
smb()
  .text("Would you like to play a game?")
  .attachment()
    .text("Choose a game to play")
    .fallback("You are unable to choose a game")
    .callbackId("wopr_game")
    .color("#3AA3E3")
    .button()
      .name("chess")
      .text("Chess")
      .type("button")
      .value("chess")
    .end()
    .button()
      .name("maze")
      .text("Falken's Maze")
      .type("button")
      .value("maze")
    .end()
    .button()
      .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?")
          .okText("Yes")
          .dismissText("No")
        .end()
    .end()
  .end()
.json()

Produces

{
    "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 = require('slack-message-builder')
smb()
  .text("Pick a user")
  .attachment()
    .text("")
    .fallback("Pick a user")
    .callbackId("user_callback")
    .select()
      .name("pick_user")
      .text("Users")
      .dataSource("users")
    .end()
    .select()
      .name("pick_channel")
      .text("Channels")
      .dataSource("channels")
    .end()
    .select()
      .name("pick_value")
      .text("Static")
      .option("some text", "a value")
      .option("some more text", "moar value")
      .option("an object value", { foo: 'bar' })
      .option("even more text", "even moar value", "a description", isSelected) // isSelected = true
    .end()
    .select()
      .name("pick_dynamic")
      .text("Choose something dynamic!")
      .dataSource("external")
    .end()
  .end()
.json()

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 = require('slack-message-builder')
smb()
  .text('Pick a user')
  .attachment()
    .text('Pick a user')
    .fallback('Pick a user')
    .callbackId('user_callback')
    .select()
      .name('option_group')
      .text('Static Option Groups')
      .optionGroup()
        .text('Option header')
        .option('some text', 'a value')
        .option('some more text', 'moar value')
      .end()
      .optionGroup()
        .text('Second Option header')
        .option('some text', 'a value')
        .option('some more text', 'moar value')
      .end()
    .end()
  .end()
.json()

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 = require('slapp')

  slapp.action('buttonCallbackId', 'action', (msg) => {
    msg.respond(smb(msg.body.original_message)
          .attachments.get(-1) // get the last attachment
            .buttons(null) // remove the buttons
            .text(`:white_check_mark: got it`) // add some confirmation text
          .end()
        .json())
  })

Using JSON

Mix and match JSON documents with slack-message-builder's functions

smb()
  .text("I am a test message")
  .attachments([{"text": "And Here's an attachment!", "color":"#3AA3E3"}])
  .json()
smb()
  .attachment()
  .fields([{"title": "title", "value":"value"}])
  .end()
  .json()