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-slash

v1.1.0

Published

Framework for handling slash commands in Slack

Downloads

1

Readme

#slack-slash

Slack-Slash is a simple framework for handling slash commands in Slack. Configure your commands and handlers in one JSON file and the framework does the rest.

What's a Slash Command?

/:command [text]

Slash commands listen for custom triggers in chat messages across all Slack channels.

For example, typing /weather 94070 could send a message to an external URL that would look up the current weather forecast for San Francisco and post it back to the user in Slack.

For more information check out the Slack API Documentation.

Adding Handlers

Adding handlers is a simple as requiring a module. In fact, it's even easier.

  1. npm install --save [handler-package-name]
  2. Add a config object for your handler inside handlers.json
  3. Set your slack token environment variables

Configuring Handlers

Handlers are defined as objects inside handlers.json.

Handler Object Properties

command:  @{String} slash command typed into slack
pkg:      @{String} module name to require
tokenVar: @{String} environment variable where slack token for slash command is stored
options:  @(Object) any options to pass to the handler [optional]

Example

// handlers.json
[
  {
    "command": "jira",
    "pkg": "slack-slash-jira",
    "tokenVar": "slack_slash_jira_token",
    "options": {...}
  }
]

Writing Handlers

slack-slash handlers are small modules meant to take the body text from a Slack Slash Command and return a message to the Slack user.

Getting Started

slack-slash handlers export a function. That function takes one or two arguments and must contain a handle method on its prototype that returns the Slack message.

Sample Handler

// myCustomHandler.js

module.exports = slashHandler;

var slashHandler = function (token, options) {
  this.token = token;
  this.options = options;
}

slashHandler.prototype.handle = function (req, handleCb) {
  var bodyText = req.body.text;
  handleCb(null, 'Received commmand with text: ' + bodyText);
};

// or if you want to respond with a Slack attachment
slashHandler.prototype.handle = function (req, handleCb) {
  var bodyText = req.body.text;
  handleCb(null, {
    text: 'Received commmand with text: ' + bodyText,
    attachments: [
      {
        'text': 'Handling slash command'
      }
    ]
  });

// or if your response is delayed and should post to response_url instead
slashHandler.prototype.handle = function (req, handleCb) {
  var bodyText = req.body.text;
  handleCb(null, {
    isDelayedResponse: true,
    text: 'Received commmand with text: ' + bodyText,
    attachments: [
      {
        'text': 'Handling slash command'
      }
    ]
  });
};

Handler Arguments

These arguments are defined in handlers.json and get passed into your handler.

  • token - Token string from configured Slack integration. The token is used to validate requests came from Slack.

  • options - Optional object with any properties you need to pass to your handler. As of v1.1.0 this object will always contain these two properties in addition to any custom ones that you set:

    • options.ssFilePath - The full path where any public files for your handler can be saved ../public/handlerCommand
    • options.ssPublicPath - The relative path which is just the name of your command, handlerCommand

handle(req, callback)

The handle method is the entry point to your handler. It will be called by slack-slash with two arguments, the request and a callback function.

Arguments

  • req - This is the request object that contains the Slack post body inside req.body.
  • callback(error, message) - The callback you must call when you are finished handling the request with an error (which can be null) and your formatted message.

Callback Message

The callback message can be either a string, which can include formatting supported by Slack or a Slack attachment.

Delayed Responses and Multiple Responses

Slack expects a response from slash commands within 3000 milliseconds. In cases where your handler cannot respond in that timeframe or if you wish to send multiple responses, Slack provides a response_url in the request object. In order to let slack-slash know your message should be treated as a delayed response, set the property isDelayedResponse: true in the object passed to the callback function. You may send up to 5 delayed responses within 30 minutes of the user's invocation.

Slack Post Body

This is the data available in the request from Slack.

{
  token: 'xxx',
  team_id: 'xxx',
  team_domain: 'xxx',
  channel_id: 'xxx',
  channel_name: 'xxx',
  user_id: 'xxx',
  user_name: 'xxx',
  command: 'xxx',
  text: 'xxx',
  response_url: 'xxx'
}

Example Handlers

License

MIT