https://github.com/sagi/workers-slack
Slack API for Cloudflare Workers
https://github.com/sagi/workers-slack
Last synced: 12 months ago
JSON representation
Slack API for Cloudflare Workers
- Host: GitHub
- URL: https://github.com/sagi/workers-slack
- Owner: sagi
- License: mit
- Created: 2019-06-28T13:19:36.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2024-07-05T05:51:11.000Z (about 2 years ago)
- Last Synced: 2025-08-11T03:56:08.395Z (12 months ago)
- Language: JavaScript
- Homepage: https://sagi.io
- Size: 1.19 MB
- Stars: 28
- Watchers: 1
- Forks: 2
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# workers-slack
[`@sagi.io/workers-slack`](https://www.npmjs.com/package/@sagi.io/workers-slack) allows
you to use Slack's Web API within Cloudflare Workers.
⭐ We use it at **[OpenSay](https://opensay.co/?s=workers-slack)** to access Slack's REST API through Cloudflare Workers.
Here is a [blog post](https://sagi.io/slack-api-for-cloudflare-workers/) that explains why I built it.
[](https://circleci.com/gh/sagi/workers-slack)
[](http://opensource.org/licenses/MIT)
[](http://npm.im/@sagi.io/workers-slack)
## Installation
~~~
$ npm i @sagi.io/workers-slack
~~~
## Cloudflare Workers Usage
If you use Cloudflare's Wrangler you must set the [`node_compat`](https://developers.cloudflare.com/workers/wrangler/configuration/#inheritable-keys) flag in your `wrangler.toml` - the reason for this is that this library is built both for Cloudflare Workers and Node.js (for which we need `globals`).
Initialize `SlackREST`:
~~~js
// Without token:
const SlackREST = require('@sagi.io/workers-slack')
const SlackAPI = new SlackREST()
// With token:
const botAccessToken = process.env.SLACK_BOT_ACCESS_TOKEN;
const SlackREST = require('@sagi.io/workers-slack')
const SlackAPI = new SlackREST({ botAccessToken })
~~~
You can then use supported [Slack methods](https://api.slack.com/methods).
For instance, here's how to use the [`chat.postMessage`](https://api.slack.com/methods/chat.postMessage) method:
~~~js
// SlackREST was initialized with a token
const formData = { channel: 'general', text: 'hello world'}
// SlackREST wasn't initialized with a token
const botAccessToken = process.env.SLACK_BOT_ACCESS_TOKEN;
const formData = { token: botAcccessToken, channel: 'general', text: 'hello world' }
const result = await SlackREST.chat.postMessage(formData)
~~~
### Verifying requests from Slack
More information [here](https://api.slack.com/authentication/verifying-requests-from-slack).
The `SlackREST.helpers.verifyRequestSignature` method returns `true` when a signature from Slack is verified. Otherwise it throws an error.
~~~js
const SlackREST = require('@sagi.io/workers-slack')
const SlackAPI = new SlackREST()
const signingSecret = process.env.SLACK_SIGNING_SECRET
const isVerifiedRequest = await SlackAPI.helpers.verifyRequestSignature(request, signingSecret)
~~~