Skip to content

Commit

Permalink
Merge pull request #1862 from intuit/slack-message-space
Browse files Browse the repository at this point in the history
Slack message spacing + notification text
  • Loading branch information
hipstersmoothie committed Mar 9, 2021
2 parents 49fccd6 + f494c7a commit 3b56060
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 2 deletions.
2 changes: 1 addition & 1 deletion plugins/slack/README.md
Expand Up @@ -218,7 +218,7 @@ There are a lot of steps to creating a Slack app and installing it.
Let's go over what you'll need to do to get set up with app auth.

1. [Create the app](https://api.slack.com/apps)
2. From your app's `Basic Information` page go to `Permissions => Bot Token Scopes` and add `chat:write` and `file:write`
2. From your app's `Basic Information` page go to `Permissions => Bot Token Scopes` and add `chat:write` and `file:write` (Optionally add `chat:write.customize` to use the `username` and `icon` options)
3. Copy the `Bot User OAuth Access Token` into your `.env` file and store it as `SLACK_TOKEN`
4. Install the app in the channels you want it to post to via the Slack UI

Expand Down
122 changes: 122 additions & 0 deletions plugins/slack/__tests__/__snapshots__/slack.test.ts.snap
Expand Up @@ -31,6 +31,13 @@ Array [
},
"type": "section",
},
Object {
"text": Object {
"text": " ",
"type": "mrkdwn",
},
"type": "section",
},
Object {
"type": "divider",
},
Expand All @@ -49,6 +56,13 @@ Array [
},
"type": "section",
},
Object {
"text": Object {
"text": " ",
"type": "mrkdwn",
},
"type": "section",
},
Object {
"text": Object {
"text": "*📝 Documentation*",
Expand All @@ -63,6 +77,13 @@ Array [
},
"type": "section",
},
Object {
"text": Object {
"text": " ",
"type": "mrkdwn",
},
"type": "section",
},
Object {
"type": "divider",
},
Expand Down Expand Up @@ -123,6 +144,13 @@ Array [
},
"type": "section",
},
Object {
"text": Object {
"text": " ",
"type": "mrkdwn",
},
"type": "section",
},
Object {
"type": "divider",
},
Expand All @@ -141,6 +169,13 @@ Array [
},
"type": "section",
},
Object {
"text": Object {
"text": " ",
"type": "mrkdwn",
},
"type": "section",
},
Object {
"text": Object {
"text": "*📝 Documentation*",
Expand All @@ -155,6 +190,13 @@ Array [
},
"type": "section",
},
Object {
"text": Object {
"text": " ",
"type": "mrkdwn",
},
"type": "section",
},
Object {
"type": "divider",
},
Expand Down Expand Up @@ -198,6 +240,13 @@ Array [
},
"type": "section",
},
Object {
"text": Object {
"text": " ",
"type": "mrkdwn",
},
"type": "section",
},
Object {
"type": "divider",
},
Expand Down Expand Up @@ -258,8 +307,16 @@ Object {
},
"type": "section",
},
Object {
"text": Object {
"text": " ",
"type": "mrkdwn",
},
"type": "section",
},
],
"link_names": true,
"text": "New Releases: 1.0.0 :tada:",
}
`;
Expand Down Expand Up @@ -298,8 +355,16 @@ Object {
},
"type": "section",
},
Object {
"text": Object {
"text": " ",
"type": "mrkdwn",
},
"type": "section",
},
],
"link_names": true,
"text": "New Releases: 1.0.0 :tada:",
}
`;
Expand Down Expand Up @@ -344,8 +409,16 @@ Object {
},
"type": "section",
},
Object {
"text": Object {
"text": " ",
"type": "mrkdwn",
},
"type": "section",
},
],
"link_names": true,
"text": "New Release: :tada:",
}
`;
Expand Down Expand Up @@ -383,8 +456,16 @@ Object {
},
"type": "section",
},
Object {
"text": Object {
"text": " ",
"type": "mrkdwn",
},
"type": "section",
},
],
"link_names": true,
"text": "New Release: :tada:",
}
`;
Expand Down Expand Up @@ -422,8 +503,16 @@ Object {
},
"type": "section",
},
Object {
"text": Object {
"text": " ",
"type": "mrkdwn",
},
"type": "section",
},
],
"link_names": true,
"text": "New Release: :tada:",
}
`;
Expand Down Expand Up @@ -461,8 +550,16 @@ Object {
},
"type": "section",
},
Object {
"text": Object {
"text": " ",
"type": "mrkdwn",
},
"type": "section",
},
],
"link_names": true,
"text": "New Releases: 1.0.0 :tada:",
}
`;
Expand Down Expand Up @@ -500,8 +597,16 @@ Object {
},
"type": "section",
},
Object {
"text": Object {
"text": " ",
"type": "mrkdwn",
},
"type": "section",
},
],
"link_names": true,
"text": "New Releases: 1.0.0 :tada:",
}
`;
Expand Down Expand Up @@ -539,8 +644,16 @@ Object {
},
"type": "section",
},
Object {
"text": Object {
"text": " ",
"type": "mrkdwn",
},
"type": "section",
},
],
"link_names": true,
"text": "New Release: :tada:",
}
`;
Expand Down Expand Up @@ -578,8 +691,16 @@ Object {
},
"type": "section",
},
Object {
"text": Object {
"text": " ",
"type": "mrkdwn",
},
"type": "section",
},
],
"link_names": true,
"text": "New Releases: 1.0.0 :tada:",
}
`;
Expand Down Expand Up @@ -631,5 +752,6 @@ Object {
},
],
"link_names": true,
"text": "New Releases: 1.0.0 :tada:",
}
`;
15 changes: 14 additions & 1 deletion plugins/slack/src/index.ts
Expand Up @@ -48,6 +48,15 @@ const createSectionBlock = (text: string) => ({
},
});

/** Create some space in the message */
const createSpacerBlock = () => ({
type: "section" as const,
text: {
type: "mrkdwn",
text: " ",
},
});

/** Create slack header block */
const createHeaderBlock = (text: string) => ({
type: "header" as const,
Expand Down Expand Up @@ -96,6 +105,7 @@ export function convertToBlocks(
if (line.startsWith("#")) {
currentMessage.push(createSectionBlock(`*${line.replace(/^[#]+/, "")}*`));
} else if (line === "---") {
currentMessage.push(createSpacerBlock());
currentMessage.push(createDividerBlock());
} else if (line.startsWith("```")) {
const [, language] = line.match(/```(\S+)/) || ["", "detect"];
Expand Down Expand Up @@ -147,6 +157,7 @@ export function convertToBlocks(
}

currentMessage.push(createSectionBlock(lines.join("\n")));
currentMessage.push(createSpacerBlock());
} else if (line) {
currentMessage.push(createSectionBlock(line));
}
Expand Down Expand Up @@ -360,13 +371,14 @@ export default class SlackPlugin implements IPlugin {
await last;

if (Array.isArray(message)) {
await channels.reduce(async (lastMessage, channel) => {
await channels.reduce(async (lastMessage, channel, index) => {
await lastMessage;
await fetch("https://slack.com/api/chat.postMessage", {
method: "POST",
body: JSON.stringify({
...userPostMessageOptions,
channel,
text: index === 0 ? `${header} :tada:` : undefined,
blocks: message,
link_names: true,
}),
Expand Down Expand Up @@ -403,6 +415,7 @@ export default class SlackPlugin implements IPlugin {
body: JSON.stringify({
...userPostMessageOptions,
link_names: true,
text: `${header} :tada:`,
// If not in app auth only one message is constructed
blocks: messages[0],
}),
Expand Down

0 comments on commit 3b56060

Please sign in to comment.