@atomist/slack-messages

中文文档
1.2.2发布于 2021/03/16Apache-2.0TypeScriptCommonJS零依赖近 30 天下载 3.8万

Atomist utilities for creating formatted Slack messages

原始使用文档
文档目录7 节
  1. Using
  2. Additional helper functions
  3. GitHub to Slack markdown conversion
  4. Support
  5. Development
  6. Build and test
  7. Release

@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;"
// 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>"
// @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.

版本与发布记录

@atomist/slack-messages 共发布 60 个版本,首次发布于 2017/05/09;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.2.2latest2021/03/165 年前
1.2.2-master.20210315181220branch-masternext2021/03/165 年前
1.2.2-master.202103151811592021/03/165 年前
1.2.2-atomist.npm-update-master.202103151804242021/03/165 年前
1.2.2-master.202103151803242021/03/165 年前
1.2.2-atomist.npm-update-master.202103151800492021/03/165 年前

关于 @atomist/slack-messages 的常见问题

@atomist/slack-messages 是什么?

Atomist utilities for creating formatted Slack messages

如何安装 @atomist/slack-messages?

在项目目录运行 npm install @atomist/slack-messages,或使用 pnpm add / yarn add / bun add。

@atomist/slack-messages 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@atomist/slack-messages 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@atomist/slack-messages 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=8.2.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@atomist/slack-messages 可以商用吗?

@atomist/slack-messages 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@atomist/slack-messages 的下载量有多少?

根据 npm 官方下载统计,@atomist/slack-messages 在 2026/10/07 – 2026/11/05 这 30 天内被下载 37,543 次,日均约 1,251 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@atomist/slack-messages 还在维护吗?

最新版本 1.2.2 发布于 2021/03/16(5 年前);至今共发布 60 个版本,其中过去 12 个月发布了 0 个;首次发布于 2017/05/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。