Skip to content

Commit

Permalink
v2.2.1 - Added additional override functionality that I mistakenly th…
Browse files Browse the repository at this point in the history
…ought was in the previous pull request.
  • Loading branch information
TheAppleFreak committed Oct 6, 2022
1 parent da10d4e commit ea5d4c0
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 2.2.1 (2022/10/6)

* Realized that I hadn't quite proofread the code in 2.2.0 as closely as I should have, and that it worked in a different way than I expected. Updated the code and documentation to reflect that you can override the properties in both the constructor as well as in the formatter to achieve the same result.

## 2.2.0 (2022/10/6)

* Readded the `channel`, `username`, `iconEmoji`, and `iconUrl` properties to the constructor and typings. It appears that [Slack seems to have reversed course from how things worked previously, allowing the use of these properties once again](https://api.slack.com/legacy/custom-integrations/messaging/webhooks#legacy-customizations). This change is courtesy of [Pull #19](https://github.com/TheAppleFreak/winston-slack-webhook-transport/pull/19). Thanks @janpieterz!
Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,20 @@ logger.add(new SlackHook({ webhookUrl: "https://hooks.slack.com/services/xxx/xxx
* `unfurlMedia` - Enables or disables [media unfurling.](https://api.slack.com/reference/messaging/link-unfurling#no_unfurling_please) (Default: `false`)
* `mrkdwn` - Enables or disables [`mrkdwn` formatting](https://api.slack.com/reference/surfaces/formatting#basics) within attachments or layout blocks (Default: `false`)
* `proxy` - Allows specifying a proxy server that [gets passed directly down to Axios](https://github.com/axios/axios#request-config) (Default: `undefined`)
* `channel` - Overrides the webhook's default channel. This should be a channel ID.
* `username` - Overrides the webhook's default username.
* `iconEmoji` - An [emoji code string](https://www.webpagefx.com/tools/emoji-cheat-sheet/) to use in place of the default icon. (Interchangeable with `iconUrl`)
* `iconUrl` - An icon image URL string to use in place of the default icon. Interchangeable with `iconEmoji`.
* `channel` - Overrides the webhook's default channel. This should be a channel ID. (Default: `undefined`)
* `username` - Overrides the webhook's default username. (Default: `undefined`)
* `iconEmoji` - An [emoji code string](https://www.webpagefx.com/tools/emoji-cheat-sheet/) to use in place of the default icon. (Interchangeable with `iconUrl`) (Default: `undefined`)
* `iconUrl` - An icon image URL string to use in place of the default icon. Interchangeable with `iconEmoji`. (Default: `undefined`)

### Message formatting

`winston-slack-webhook-transport` supports the ability to format messages using Slack's message layout features. To do this, supply a custom formatter function that returns the [requisite object structure](https://api.slack.com/messaging/composing/layouts) to create the desired layout. You can use the [Slack Block Kit Builder](https://app.slack.com/block-kit-builder/) to quickly and easily prototype advanced layouts using Block Kit.

If for some reason you don't want to send a message to Slack, you can also return `false` to prevent the log message from being sent.

Note that if you're using Block Kit using either the `attachments` or `blocks` keys, the `text` parameter will function as a fallback for surfaces that do not support Block Kit, such as IRC clients or push notifications. It is recommended to include `text` when possible in these cases.
Formatters can also override the channel the message is posted to, username, and icon by defining the properties `channel`, `username`, `iconEmoji`, or `iconUrl` in the same object structure. These will override any options set in the transport constructor.

Note that if you're using Block Kit using either the `attachments` or `blocks` keys, the `text` parameter will function as a fallback for surfaces that do not support Block Kit, such as push notifications. It is recommended to include `text` when possible in these cases.

```javascript
const winston = require("winston");
Expand Down
40 changes: 40 additions & 0 deletions __tests__/slackHook.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ describe ("Standard options", () => {
name: 'totally-fake-slackhook',
formatter: 'totally-fake-formatter',
webhookUrl: 'https://totally.fake.url',
channel: '#totally-fake-channel',
username: 'totally-fake-username',
iconEmoji: ':totally-fake-emoji:',
iconUrl: 'https://totally.fake.icon.url',
unfurlLinks: true,
unfurlMedia: true,
mrkdwn: true
Expand All @@ -30,6 +34,10 @@ describe ("Standard options", () => {
expect(fakeSlackHook.unfurlLinks).toEqual(fakeOpts.unfurlLinks);
expect(fakeSlackHook.unfurlMedia).toEqual(fakeOpts.unfurlMedia);
expect(fakeSlackHook.mrkdwn).toEqual(fakeOpts.mrkdwn);
expect(fakeSlackHook.channel).toEqual(fakeOpts.channel);
expect(fakeSlackHook.username).toEqual(fakeOpts.username);
expect(fakeSlackHook.iconEmoji).toEqual(fakeOpts.iconEmoji);
expect(fakeSlackHook.iconUrl).toEqual(fakeOpts.iconUrl);
expect(mockAxios.create).toHaveBeenCalledTimes(1);
});

Expand All @@ -38,6 +46,10 @@ describe ("Standard options", () => {
const fakePayload = {
unfurl_links: fakeOpts.unfurlLinks,
unfurl_media: fakeOpts.unfurlMedia,
channel: fakeOpts.channel,
username: fakeOpts.username,
icon_emoji: fakeOpts.iconEmoji,
icon_url: fakeOpts.iconUrl,
mrkdwn: fakeOpts.mrkdwn,
text: 'undefined: undefined'
};
Expand All @@ -58,6 +70,10 @@ describe ("Standard options with custom formatter", () => {
name: 'totally-fake-slackhook',
formatter: fakeFormatter,
webhookUrl: 'https://totally.fake.url',
channel: '#totally-fake-channel',
username: 'totally-fake-username',
iconEmoji: ':totally-fake-emoji:',
iconUrl: 'https://totally.fake.icon.url',
unfurlLinks: true,
unfurlMedia: true,
mrkdwn: true
Expand All @@ -82,6 +98,10 @@ describe ("Standard options with custom formatter", () => {
expect(fakeSlackHook.unfurlLinks).toEqual(fakeOpts.unfurlLinks);
expect(fakeSlackHook.unfurlMedia).toEqual(fakeOpts.unfurlMedia);
expect(fakeSlackHook.mrkdwn).toEqual(fakeOpts.mrkdwn);
expect(fakeSlackHook.channel).toEqual(fakeOpts.channel);
expect(fakeSlackHook.username).toEqual(fakeOpts.username);
expect(fakeSlackHook.iconEmoji).toEqual(fakeOpts.iconEmoji);
expect(fakeSlackHook.iconUrl).toEqual(fakeOpts.iconUrl);
expect(mockAxios.create).toHaveBeenCalledTimes(1);
});

Expand All @@ -92,6 +112,10 @@ describe ("Standard options with custom formatter", () => {
unfurl_links: fakeOpts.unfurlLinks,
unfurl_media: fakeOpts.unfurlMedia,
mrkdwn: fakeOpts.mrkdwn,
channel: fakeOpts.channel,
username: fakeOpts.username,
icon_emoji: fakeOpts.iconEmoji,
icon_url: fakeOpts.iconUrl,
text: 'Custom message: undefined'
};

Expand All @@ -113,6 +137,10 @@ describe ("Custom options with custom formatter", () => {
name: 'totally-fake-slackhook',
formatter: fakeFormatter,
webhookUrl: 'https://totally.fake.url',
channel: '#totally-fake-channel',
username: 'totally-fake-username',
iconEmoji: ':totally-fake-emoji:',
iconUrl: 'https://totally.fake.icon.url',
unfurlLinks: true,
unfurlMedia: true,
mrkdwn: true
Expand Down Expand Up @@ -161,6 +189,10 @@ describe ("Standard options with formatter that filters out all messages", () =>
name: 'totally-fake-slackhook',
formatter: fakeFormatter,
webhookUrl: 'https://totally.fake.url',
channel: '#totally-fake-channel',
username: 'totally-fake-username',
iconEmoji: ':totally-fake-emoji:',
iconUrl: 'https://totally.fake.icon.url',
unfurlLinks: true,
unfurlMedia: true,
mrkdwn: true
Expand All @@ -185,6 +217,10 @@ describe ("Standard options with formatter that filters out all messages", () =>
expect(fakeSlackHook.unfurlLinks).toEqual(fakeOpts.unfurlLinks);
expect(fakeSlackHook.unfurlMedia).toEqual(fakeOpts.unfurlMedia);
expect(fakeSlackHook.mrkdwn).toEqual(fakeOpts.mrkdwn);
expect(fakeSlackHook.channel).toEqual(fakeOpts.channel);
expect(fakeSlackHook.username).toEqual(fakeOpts.username);
expect(fakeSlackHook.iconEmoji).toEqual(fakeOpts.iconEmoji);
expect(fakeSlackHook.iconUrl).toEqual(fakeOpts.iconUrl);
expect(mockAxios.create).toHaveBeenCalledTimes(1);
});

Expand All @@ -193,6 +229,10 @@ describe ("Standard options with formatter that filters out all messages", () =>
const fakePayload = {
unfurl_links: fakeOpts.unfurlLinks,
unfurl_media: fakeOpts.unfurlMedia,
channel: fakeOpts.channel,
username: fakeOpts.username,
icon_emoji: fakeOpts.iconEmoji,
icon_url: fakeOpts.iconUrl,
mrkdwn: fakeOpts.mrkdwn,
text: 'undefined: undefined'
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "winston-slack-webhook-transport",
"version": "2.2.0",
"version": "2.2.1",
"description": "A Slack transport for Winston 3 that logs to a channel via webhooks.",
"main": "slackHook.js",
"typings": "./slackHook.d.ts",
Expand Down
10 changes: 9 additions & 1 deletion slackHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ module.exports = class SlackHook extends Transport {
this.unfurlLinks = opts.unfurlLinks || false;
this.unfurlMedia = opts.unfurlMedia || false;
this.mrkdwn = opts.mrkdwn || false;
this.channel = opts.channel || '';
this.username = opts.username || '';
this.iconEmoji = opts.iconEmoji || '';
this.iconUrl = opts.iconUrl || '';

this.axiosInstance = axios.create({
proxy: opts.proxy || undefined
Expand All @@ -26,7 +30,11 @@ module.exports = class SlackHook extends Transport {
let payload = {
unfurl_links: this.unfurlLinks,
unfurl_media: this.unfurlMedia,
mrkdwn: this.mrkdwn
mrkdwn: this.mrkdwn,
channel: this.channel,
username: this.username,
icon_emoji: this.iconEmoji,
icon_url: this.iconUrl
}

if (this.formatter && typeof this.formatter === 'function') {
Expand Down

0 comments on commit ea5d4c0

Please sign in to comment.