slack-router

An express.js router for Slack Apps

Usage no npm install needed!

<script type="module">
  import slackRouter from 'https://cdn.skypack.dev/slack-router';
</script>

README

Slack Router

Start building a Slack App quickly with OAuth support, url parsing, and authenticated HTTPS client to call Slack’s API. This was made to build Slack Apps on Glitch even faster, but it works anywhere.

Install

npm i slack-router

Usage

const express = require('express'),
  SlackRouter = require('slack-router'),
  app = express()

// Configure express with the Slack App settings
const slackRouter = new SlackRouter({
  scope: 'chat:write,bot',
  client_id: "XXXXXXXXXXXXXXXXXXXXX",
  client_secret: "XXXXXXXXXXXXXXXXX",
  signing_secret: "XXXXXXXXXXXXXXXX",
  verification_token: "XXXXXXXXXXXX", // optional
  redirect_uri: 'XXXXXXXXXXXXX'       // optional
  datastore: '.data/workspaces'       // optional
})

// All GET routes redirect to the “Add to Slack” OAuth flow
app.get('/', slackRouter, (req, res) => {
  
  // a writeable datastore associated to the workspace
  req.slack.data
})

// All POST routes expect Slack callback events
// and verify against the verification token
app.post('/', slackRouter, (req, res) => {

  // a writeable datastore associated to the workspace
  req.slack.data

  // the api function auto-appends the workspace’s token
  req.slack.api('users.info', { user: req.slack.user.id }).then(r => {
    r.data // the results of 'users.info' API request
  })
  
})

Features

☑ OAuth support without 3rd-party database

☑ Supports Single Channel Installations single_channel=true

☑ Verifies request signatures and/or verification tokens

☑ Support for short-lived tokens with automatic refresh

☑ Automatic retrieval of workspace's authentication info

☑ Auto-parsing of string-encoded JSON payloads

☑ Authenticated HTTP client for Slack's API

☑ Writeable datastore associated to the workspace

Configuration

The configuration options used for the constructor

  • scope - A comma-delimited scopes for Slack OAuth
  • client_id - The Slack App client id
  • client_secret - The Slack App client secret
  • signing_secret - The Slack App signing secret
  • redirect_uri - The Slack App OAuth redirect uri
  • verification_token - Slack App verification token
  • access_token - Access token to use when the App is not distributed (optional)
  • datastore - A file name to write workspace info to or pass in an object to use an alternate datastore (optional)
  • slack_root - The root domain to make Slack requests to (optional)

Slack Request

The Slack record object is appended to all requests where the router is used. It is a Object that can be written to and saved for future requests.

data

The datastore associated to the workspace

app_url

The redirect url to open the App DM in Slack

api(endpoint or uri, args)

An authenticated HTTP client that will post to Slack’s API

reply(message)

Sends a message back to the channel the event was recieved on

set(key, value)

Sets a value in the workspace datastore

get(key)

Returns a value from the workspace datastore

DataStore Interface

The default datastore is file-based and stored at .data/workspaces. If you would like to change this, there are two options:

  • Passing in a new file path to datastore will attempt to use that file path
  • Passing in a new Object to datastore will attempt to call two functions get and save

DataStore.get(team_id)

This method should return a Promise containing a single workspace info associated to a team_id

DataStore.save(team_id, {})

The method should return a Promise containing the saved workspace info associated to a team_id

DataStore.update(team_id, {})

The method should return a Promise containing the updated workspace info associated to a team_id