Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions .github/workflows/ci-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
node-version: [18.x, 20.x, 22.x]
node-version:
- 20.x
- 22.x
- 24.x
package:
- cli-hooks
- cli-test
Expand Down Expand Up @@ -87,20 +90,20 @@ jobs:
id: check_coverage
uses: andstor/file-existence-action@076e0072799f4942c8bc574a82233e1e4d13e9d6 # v3.0.0
with:
files: packages/${{ matrix.package }}/coverage/lcov.info
files: packages/${{ matrix.package }}/**/lcov.info
- name: Upload code coverage
if: matrix.node-version == '22.x' && matrix.os == 'ubuntu-latest' && steps.check_coverage.outputs.files_exists == 'true'
if: matrix.node-version == '24.x' && matrix.os == 'ubuntu-latest' && steps.check_coverage.outputs.files_exists == 'true'
uses: codecov/codecov-action@5a1091511ad55cbe89839c7260b706298ca349f7 # v5.5.1
with:
token: ${{ secrets.CODECOV_TOKEN }}
directory: packages/${{ matrix.package }}/coverage
files: packages/${{ matrix.package }}/coverage/test-results.xml,packages/${{ matrix.package}}/lcov.info # TODO: use "lcov.info" as "file"
flags: ${{ matrix.package }}
verbose: true
- name: Upload test results to Codecov
if: ${{ !cancelled() }}
uses: codecov/test-results-action@47f89e9acb64b76debcd5ea40642d25a4adced9f # v1.1.1
with:
file: packages/${{ matrix.package }}/coverage/test-results.xml
files: packages/${{ matrix.package }}/coverage/test-results.xml,packages/${{ matrix.package}}/lcov.info # TODO: use "lcov.info" as "file"
flags: ${{ matrix.node-version }},${{ matrix.os }},${{ matrix.package }}
token: ${{ secrets.CODECOV_TOKEN }}
verbose: true
2 changes: 1 addition & 1 deletion packages/webhook/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
/dist

# coverage
/coverage
/lcov.info
87 changes: 50 additions & 37 deletions packages/webhook/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

[![codecov](https://codecov.io/gh/slackapi/node-slack-sdk/graph/badge.svg?token=OcQREPvC7r&flag=webhook)](https://codecov.io/gh/slackapi/node-slack-sdk)

The `@slack/webhook` package contains a helper for making requests to Slack's [Incoming
Webhooks](https://docs.slack.dev/messaging/sending-messages-using-incoming-webhooks). Use it in your app to send a notification to a channel.
The `@slack/webhook` package contains a helper for sending message to Slack using [incoming webhooks](https://docs.slack.dev/messaging/sending-messages-using-incoming-webhooks). Use it in your app to send a notification to a channel.

## Requirements
This package supports Node v18 and higher. It's highly recommended to use [the latest LTS version of
node](https://github.com/nodejs/Release#release-schedule), and the documentation is written using syntax and features
from that version.

This package supports Node v20 and higher. It's highly recommended to use [the latest LTS version of node](https://github.com/nodejs/Release#release-schedule), and the documentation is written using syntax and features from that version.

## Installation

Expand All @@ -24,12 +22,10 @@ $ npm install @slack/webhook

### Initialize the webhook

The package exports an `IncomingWebhook` class. You'll need to initialize it with the URL you received from Slack.
To create a webhook URL, follow the instructions in the [Getting started with Incoming Webhooks](https://docs.slack.dev/messaging/sending-messages-using-incoming-webhooks)
guide.
The package exports an `IncomingWebhook` class. You'll need to initialize it with the URL you received from Slack. To create a webhook URL, follow the instructions in the [Getting started with incoming webhooks](https://docs.slack.dev/messaging/sending-messages-using-incoming-webhooks) guide.

```javascript
const { IncomingWebhook } = require('@slack/webhook');
import { IncomingWebhook } from "@slack/webhook";

// Read a url from the environment variables
const url = process.env.SLACK_WEBHOOK_URL;
Expand All @@ -43,16 +39,16 @@ const webhook = new IncomingWebhook(url);
<strong><i>Setting default arguments</i></strong>
</summary>

The webhook can be initialized with default arguments that are reused each time a notification is sent. Use the second
parameter to the constructor to set the default arguments.
The webhook can be initialized with default arguments that are reused each time a notification is sent. Use the second parameter to the constructor to set the default arguments.

```javascript
const { IncomingWebhook } = require('@slack/webhook');
import { IncomingWebhook } from "@slack/webhook";

const url = process.env.SLACK_WEBHOOK_URL;

// Initialize with defaults
const webhook = new IncomingWebhook(url, {
icon_emoji: ':bowtie:',
unfurl_media: false,
});
```

Expand All @@ -62,60 +58,77 @@ const webhook = new IncomingWebhook(url, {

### Send a notification

Something interesting just happened in your app, so it's time to send the notification! Just call the
`.send(options)` method on the webhook. The `options` parameter is an object that should describe the contents of
the message. The method returns a `Promise` that resolves once the notification is sent.
Something interesting just happened in your app, so it's time to send the notification! Just call the `.send(options)` method on the webhook. The `options` parameter is an object that should describe the contents of the message. The method returns a `Promise` that resolves once the notification is sent.

```javascript
const { IncomingWebhook } = require('@slack/webhook');
import { IncomingWebhook } from "@slack/webhook";

const url = process.env.SLACK_WEBHOOK_URL;

const webhook = new IncomingWebhook(url);

// Send the notification
(async () => {
await webhook.send({
text: 'I\'ve got news for you...',
text: "I've got news for you...",
});
})();
```

---

### Proxy requests with a custom agent
### Send requests with a custom fetch adapter

The webhook allows you to customize the HTTP
[`Agent`](https://nodejs.org/docs/latest/api/http.html#http_class_http_agent) used to create the connection to Slack.
Using this option is the best way to make all requests from your app go through a proxy, which is a common requirement in
many corporate settings.
The `@slack/webhook` package sends requests using [`globalThis.fetch`](https://nodejs.org/api/globals.html#fetch) by default, but you can customize that for various purposes such as for custom handling of retries or proxying requests, both of which are common requirements in corporate settings.

In order to create an `Agent` from some proxy information (such as a host, port, username, and password), you can use
one of many npm packages. We recommend [`https-proxy-agent`](https://www.npmjs.com/package/https-proxy-agent). Start
by installing this package and saving it to your `package.json`.
In order to use a custom fetch adapter, provide a function that's compatible with the `fetch` interface.

The following example uses the [`undici`](https://www.npmjs.com/package/undici) package to create a dispatcher for proxying requests with a limited timeout. Start by installing this package:

```shell
$ npm install https-proxy-agent
$ npm install undici
```

Import the `HttpsProxyAgent` class, and create an instance that can be used as the `agent` option of the
`IncomingWebhook`.
Then import the `ProxyAgent` and `fetch` class from the `undici` package to create a custom `fetch` implementation. This is passed to the `IncomingWebhook` constructor and used in requests:

```javascript
const { IncomingWebhook } = require('@slack/webhook');
const { HttpsProxyAgent } = require('https-proxy-agent');
import { IncomingWebhook } from "@slack/webhook";
import { ProxyAgent, fetch as undiciFetch } from "undici";

const url = process.env.SLACK_WEBHOOK_URL;

// One of the ways you can configure HttpsProxyAgent is using a simple string.
// See: https://github.com/TooTallNate/node-https-proxy-agent for more options
const proxy = new HttpsProxyAgent(process.env.http_proxy || 'http://168.63.76.32:3128');
/**
* Configure your proxy agent here
* @see {@link https://undici.nodejs.org/#/docs/api/ProxyAgent.md}
*/
const proxyAgent = new ProxyAgent({
uri: new URL("http://localhost:8888"),
proxyTls: {
signal: AbortSignal.timeout(400),
},
});

/**
* Implement a custom fetch adapter
* @type {typeof globalThis.fetch}
* @see {@link https://undici.nodejs.org/#/docs/api/Fetch.md}
*/
const myFetch = async (url, opts) => {
return await undiciFetch(url, {
...opts,
dispatcher: proxyAgent,
});
};

// Initialize with the proxy agent option
const webhook = new IncomingWebhook(token, { agent: proxy });
// Initialize with the custom fetch adapater and proxy
const webhook = new IncomingWebhook(url, {
fetch: myFetch,
});

// Sending this webhook will now go through the proxy
(async () => {
await webhook.send({
text: 'I\'ve got news for you...',
text: "I've got news for you...",
});
})();
```
42 changes: 18 additions & 24 deletions packages/webhook/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{
"$schema": "https://www.schemastore.org/package.json",
"name": "@slack/webhook",
"version": "7.0.6",
"description": "Official library for using the Slack Platform's Incoming Webhooks",
"author": "Slack Technologies, LLC",
"license": "MIT",
"type": "module",
"keywords": [
"slack",
"request",
Expand All @@ -12,14 +14,14 @@
"api",
"proxy"
],
"main": "dist/index.js",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist/**/*"
],
"engines": {
"node": ">= 18",
"npm": ">= 8.6.0"
"node": ">= 20",
"npm": ">= 9.6.4"
},
"repository": "slackapi/node-slack-sdk",
"homepage": "https://docs.slack.dev/tools/node-slack-sdk/webhook/",
Expand All @@ -30,36 +32,28 @@
"url": "https://github.com/slackapi/node-slack-sdk/issues"
},
"scripts": {
"prepare": "npm run build",
"build": "npm run build:clean && tsc",
"build:clean": "shx rm -rf ./dist ./coverage",
"prebuild": "shx rm -rf ./dist ./lcov.info",
"build": "tsc",
"docs": "npx typedoc --plugin typedoc-plugin-markdown",
"lint": "npx @biomejs/biome check .",
"lint:fix": "npx @biomejs/biome check --write .",
"mocha": "mocha --config ./test/.mocharc.json src/*.spec.ts",
"test": "npm run lint && npm run test:unit",
"test:unit": "npm run build && c8 --config ./test/.c8rc.json npm run mocha"
},
"dependencies": {
"@slack/types": "^2.9.0",
"@types/node": ">=18.0.0",
"axios": "^1.11.0"
"prepack": "npm run build",
"pretest": "npm run lint && npm run build",
"test": "node --import tsx --test --experimental-test-coverage",
"posttest": "node --import tsx --test --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=./lcov.info"
},
"devDependencies": {
"@biomejs/biome": "^2.0.5",
"@types/chai": "^4.3.5",
"@types/mocha": "^10.0.1",
"c8": "^10.1.3",
"chai": "^4.3.8",
"mocha": "^11.7.1",
"mocha-junit-reporter": "^2.2.1",
"mocha-multi-reporters": "^1.5.1",
"nock": "^14.0.6",
"@types/node": ">=20.0.0",
"shx": "^0.4.0",
"source-map-support": "^0.5.21",
"ts-node": "^10.9.2",
"tsx": "^4.20.6",
"typedoc": "^0.28.7",
"typedoc-plugin-markdown": "^4.7.1",
"typescript": "^5.8.3"
"typescript": "^5.8.3",
"undici": "^7.16.0"
},
"peerDependencies": {
"@slack/types": "^2.17.0"
}
}
115 changes: 0 additions & 115 deletions packages/webhook/src/IncomingWebhook.spec.ts

This file was deleted.

Loading