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

@atomist/slack-messages

v1.2.2

Published

Atomist utilities for creating formatted Slack messages

Downloads

105,208

Readme

@atomist/slack-messages

atomist sdm goals npm version

Node.js module that helps to create well-formatted Slack messages sent through the @atomist bot from your software delivery machine (SDM). This module also facilitates adding actions to your Slack messages that execute SDM commands.

Support for sending Slack Block messages is also supported.

See the Atomist documentation for more information on what SDMs are and what they can do for you using the Atomist API for software.

Using

Construct a message as a plain map following the Slack message formatting API.

import { SlackMessage } from "@atomist/slack-messages";
// A very simple message
const msg: SlackMessage = { text: "Simple message" };
import {
    escape,
    SlackMessage,
    url,
} from "@atomist/slack-messages";
// Here is an example of a message with a Slack action (button).
const msg: SlackMessage = {
    text: `${url(user.url, "@" + user.name)} opened issue: ${url(issue.url, issue.title)}`,
    attachments: [
        {
            text: escape(issue.body),
            fallback: escape(issue.title),
            mrkdwn_in: ["text"],
            actions: [
                {
                    text: "Close issue",
                    type: "button",
                    name: "closeissue",
                    value: "somebuttonid",
                },
            ],
            callback_id: "cllbck1",
        },
    ],
};

And then render the message with render(msg). This will construct a JSON string representation of the message:

import { render } from "@atomist/slack-messages"

const renderedMsg = render(msg);

or to produce a pretty JSON string:

const renderedMsg = render(msg, true);

This will produce the following JSON string (pretty version):

{
  "text": "<https://github.com/anna|@anna> opened issue: <https://github.com/someorg/somerepo/issues/484|This issue title contains &lt;unsafe&gt; characters and &amp;>",
  "attachments": [
    {
      "text": "This is a very important issue with body containing &lt;unsafe&gt; characters and even &amp;",
      "fallback": "This issue title contains &lt;unsafe&gt; characters and &amp;",
      "mrkdwn_in": [
        "text"
      ],
      "callback_id": "cllbck1",
      "actions": [
        {
          "text": "Close issue",
          "type": "button",
          "name": "rug",
          "value": "somebuttonid"
        }
      ]
    }
  ]
}

Note that the render function will automatically assign a unique callback_id to each attachments that has actions. But, if you provide your custom callback_id it will be preserved as is.

Additional helper functions

Special characters

escape("Slack requires you to escape <, > and &");
// => "Slack requires you to escape &lt;, &gt; and &amp;"

Links

// Simple link
url("https://www.atomist.com");
// => "<https://www.atomist.com>"

// Link with label
url("https://www.atomist.com", "atomist");
// => "<https://www.atomist.com|atomist>"

User & channel links

// @some-user (Slack will display user name for provided user ID)
user("U123");
// => "<@U123>"

// #some-channel (Slack will display channel name for provided channel ID)
channel("C123");
// => "<#C123>"

Special variables

// @channel
atChannel();
// => "<!channel>"

// @here
atHere();
// => "<!here>"

// @everyone
atEveryone();
// => "<!everyone>"

Emoji

emoji("smile");
// => ":smile:";

Markdown

Slack will render markdown if field where markdown is present is included in mrkdwn_in array.

bold("This text will appear bold");
// => "*This text will appear bold*"

italic("This text will appear italic");
// => "_This text will appear italic_"

strikethrough("This text will appear strike-through");
// => "~This text will appear strike-through~"

// Single line code block
codeLine("var a = new A();");
// =>  "`var a = new A();`"

// Multi line code block
codeBlock("var a = new A();\nvar b = new B();");
// => "```var a = new A();\nvar b = new B();```"

// List
listItem("Item 1");
// => "• Item 1"

GitHub to Slack markdown conversion

GitHub and Slack markdown are different enough to make your GitHub issues or GitHub PRs look quite bad in Slack by default. You can use the githubToSlack function from Markdown to convert text that uses GitHub markdown to text that will look good in Slack:

import { githubToSlack } from "@atomist/slack-messages"

githubToSlack("* list item 1\n* list item 2\n\**some bold text** and *some italic text* with a link [click here](http://someplace.com)");
// => "• list item 1\n• list item 2\n*some bold text* and _some italic text_ with a link <http://someplace.com|click here>"

Support

General support questions should be discussed in the #help channel in the Atomist community Slack workspace.

If you find a problem, please create an issue.

Development

You will need to install Node.js to build and test this project.

Build and test

Install dependencies.

$ npm install

Use the build package script to compile, test, lint, and build the documentation.

$ npm run build

Release

Releases are handled via the Atomist SDM. Just press the 'Approve' button in the Atomist dashboard or Slack.


Created by Atomist. Need Help? Join our Slack workspace.