Skip to content

Commit

Permalink
expose username, iconUrl, and iconEmoji
Browse files Browse the repository at this point in the history
  • Loading branch information
hipstersmoothie committed Mar 2, 2021
1 parent b5ab6f1 commit b8df59f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
56 changes: 56 additions & 0 deletions plugins/slack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,62 @@ Additional Title to add at the start of the slack message.
}
```

### username

Username to post the message as.

```json
{
"plugins": [
[
"slack",
{
"url": "https://url-to-your-slack-hook.com",
"username": "My Project"
}
]
]
}
```

### iconUrl

Image url to use as the message's avatar.

```json
{
"plugins": [
[
"slack",
{
"url": "https://url-to-your-slack-hook.com",
"iconUrl": "http://lorempixel.com/48/48"
}
]
]
}
```

> NOTE: If both `iconUrl` and `iconEmoji` are specified only `iconUrl` will be respected
### iconEmoji

Emoji code to use as the message's avatar.

```json
{
"plugins": [
[
"slack",
{
"url": "https://url-to-your-slack-hook.com",
"iconEmoji": ":chart_with_upwards_trend:"
}
]
]
}
```

### channels (App Auth Only)

Channel, private group, or IM channel to send message to.
Expand Down
20 changes: 20 additions & 0 deletions plugins/slack/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ const basePluginOptions = t.partial({
publishPreRelease: t.boolean,
/** Additional Title to add at the start of the slack message */
title: t.string,
/** Username to post the message as */
username: t.string,
/** Image url to use as the message's avatar */
iconUrl: t.string,
/** Emoji code to use as the message's avatar */
iconEmoji: t.string,
});

const appPluginOptions = t.intersection([
Expand Down Expand Up @@ -319,6 +325,18 @@ export default class SlackPlugin implements IPlugin {
messages[0].unshift(createSectionBlock(this.options.title));
}

const userPostMessageOptions: Record<string, string> = {};

if (this.options.username) {
userPostMessageOptions.username = this.options.username;
}

if (this.options.iconUrl) {
userPostMessageOptions.icon_url = this.options.iconUrl;
} else if (this.options.iconEmoji) {
userPostMessageOptions.icon_emoji = this.options.iconEmoji;
}

if ("auth" in this.options) {
const channels = this.options.channels;

Expand All @@ -331,6 +349,7 @@ export default class SlackPlugin implements IPlugin {
await fetch("https://slack.com/api/chat.postMessage", {
method: "POST",
body: JSON.stringify({
...userPostMessageOptions,
channel,
blocks: message,
link_names: true,
Expand Down Expand Up @@ -366,6 +385,7 @@ export default class SlackPlugin implements IPlugin {
await fetch(`${this.options.url}${token ? `?token=${token}` : ""}`, {
method: "POST",
body: JSON.stringify({
...userPostMessageOptions,
link_names: true,
// If not in app auth only one message is constructed
blocks: messages[0],
Expand Down

0 comments on commit b8df59f

Please sign in to comment.