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

slackbot-dispatch

v1.1.0

Published

Slackbots add-on to help with adding functionality to a slackbot

Downloads

15

Readme

Slack Bot Dispatch

Helping organize code for slackbots to quickly add functionality

The idea with this codebase is to provide a small framework for adding "strategies" to a slackbot. Think of a strategy like a question, answer pair.

Hopefully, with this framework, you will only have to code new strategies instead of worrying about running a slackbot.

Installing and Running

Literally, install the npm package and run npm start. An example strategy will be installed as well.

Note: there are 2 environment variables that need to be set

  • BOT_NAME
  • BOT_API_KEY
 $ export BOT_API_KEY=xyz
 $ export BOT_NAME=leeeroyjenkins
 $ npm install slackbot-dispatch
 $ npm start

Note: log4js is used to log certain events, set LOG_LEVEL environment variable to change output


Usages

Once the slackbot is running, you can ask questions.

Show Usages

text

Valid Strategy

text

Invalid Strategy

text


Strategies explained

The idea is that there is a directory of strategies that are the interface to your slackbot's functionality. Each strategy has a way of determining if it can handle the current messaging coming in, and returning the output for that message or request from a user. The strategy manager loops through the directory of strategies, defaulted to lib/strategies, and uses those throughout the life of the slackbot.

Every message coming in is examined to determine if it meets the following criteria. If so, it is sent to the dispatcher to determine if any of the strategies can handle the current message.

  • Does the message mention the slackbot?
  • Is the user not the slackbot?
  • Is the message in a Channel or Group?
  • Is the message an actual "chat" message (slack sends lots of messages like files being uploaded, users joining a channel, etc)

Implementing a new strategy

The idea is that one creates a class that implements the BotStrategyBase class. By putting the strategy into the lib/strategies folder, the strategy should be included on startup. Everything else should just work. There is an example in the lib/strategies directory.

There is an example copied into the target folder on install.

Example strategy

Methods

getKeys

Returns the messages that a single strategy will respond to. The key is how a user tells the slackbot which question in which to give an answer

| Method Name | Params | Result | Description | | --- | --- | --- | --- | | getKeys | none | string[] | Returns the messages that a single strategy will respond to. The key is how a user tells the slackbot which question in which to give an answer | | getUsage | none | string[] | The usage method helps to tell the users of the slackbot how to format their questions.| | canHandle | message | boolean | Has a default implementation to try to match part of the incoming message to the current keys. To change the behavior, implement in the strategy to override the base functionality. | | handle | message | string | Returns the message that the slackbot will respond with. Should be implemented to return a Promise that resolves a string. |

Message example

{
  type: 'message',
  channel: 'ABCDEFG',
  user: 'U0D98DJS',
  text: '<@U04L00> example-key whats up',
  ts: '1475840569.000306',
  team: 'T0293DLD8',
  isChannel: false,
  isGroup: true,
  changedText: 'example-key whats up'
}

Note: changedText is text with the userId replaced from the beginning

Customization

Strategy Directory

By default, the bot will look for strategies in the lib/strategies dir. To change this, set the SLACK_STRATEGY_DIR environment variable to a filepath.

Logging

Log4js is used for logging. Set the LOG_LEVEL to the desired level for output.