npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

slack-express

v5.8.0

Published

Quickly implement Slack slash commands as Express middleware.

Downloads

6

Readme

slack-express bitHound Overalll Score bitHound Dependencies Codeship Status for smallwins/slack-express

Quickly implement a Slack slash commands as Express middleware.

hello world

Define a handler for /echo slash command:

import slack, {slash, start} from 'slack-express'

slash('/echo', (payload, message)=> {
  // payload recieved as a POST from Slack command issued
  let cmd = payload.raw.command
  // sends a response to the Slack user
  message({text:`Received command from Slack: ${cmd}`})
})

start()

Now any Slack user issuing /echo slash command will recieve "Received command from Slack: /echo" message.

generated routes

Under the hood slack-express is just an Express app that generates Slack API friendly routes for authenticating an app and recieving incoming webhooks.

  • GET / Displays a generated install page with an Add to Slack button
  • GET /auth Auth callback from the Add to Slack button
  • POST / Executes slash command middlwares

The slack object is an Express app so you can mount it on an existing app with app.use or extend with regular web routes for doing things like authenticating 3rd party services.

import express from 'express'
import slack from 'slack-express'

// register a slash command handler
slack.slash('/rad', (payload, message)=> {
  message({text:'rad indeed!'})
})

// create a fresh express app
let app = express()

// mount the slash commands on the /rad route
app.use('/rad', slack)

// add other routes and junk per norms
app.get('/', (req, res)=> res.end('index page'))

// as you do
app.listen(3777)

middleware

slack.slash accepts any number of slash command middlewares. Symmetry with Express aside, the slack-express middleware sub stack is a nice pattern for flow control, data transform pipelines and helps guide app modularization. Check it out:

import slack from 'slack-express'

function logger(payload, message, next) {
  payload.custom = 'thing'
  console.log(payload)
  next()
}

function hello(payload, message) {
  message({
    text: 'hello world'
  })
}

slack.slash('/hi', logger, hello)

slack.start()

the payload

The payload parameter in the slack.slash callback contains all the information from the slash command.

import slack from 'slack-express'

slack.slash('/console.log', (payload, message)=> {
  // payload keys
  let {ok, raw, message, account} = payload
  // old school print styles debugger aw shiii
  message({text:`${JSON.stringify(payload, null, 2)}`})
})

slack.start()

Use it to process the command and respond using Slack message formatted JSON message().

persistence

Slack slash commands are issued by real humans and you may wish to associate information with them. Databases are good for this! You can find and store data for each Slack account interacting with your app by using find and save. Currently only DynamoDB is supported but Redis will be an easy drop in.

api

slack
  slash(cmd, (payload, message)=>) ... Register a slash command handler
  start() ............................ Starts up the app
  button() ........................... Add to Slack button for your app
  find(params, (err, account)=>) ..... Find a saved Slack account
  save(params, (err, account)=>) ..... Save a Slack account

local setup

You need to create an app with Slack for a SLACK_CLIENT_ID and SLACK_CLIENT_SECRET. Then you will need to create a .env file with the following:

NODE_ENV=development
APP_NAME=dasherized-app-name-here
SLACK_CLIENT_ID=your-slack-client-id-here
SLACK_CLIENT_SECRET=your-slack-client-secret-here
SLACK_VERIFICATION_TOKEN=your-slash-command-verification-token-here
AWS_REGION=us-east-1
SECRET=your-secret-text-here

Don't forget to add .env to your .gitignore.