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

slacklibbot

v0.6.1

Published

Slack Bot framework

Downloads

34

Readme

SlackLibBot

A Slack bot framework written in TypeScript

Installation

> npm install slacklibbot --save

Example

import { start, setup } from 'slacklibbot'

const bot = setup({ name: 'Bot' })

bot.register('my-command', 'Description', async (bot, msg, cfg, params) => {
  const user = bot.users.find(user => user.id === msg.user)
  await bot.postMessage({ channel: bot.channel, text: `Msg recvd: ${params.join(' ')}`, ...cfg.defaultParams })
  console.log('Message received from ', user!.name)
})

start()

Configuration API

Configuration Defaults

// This can be overridden at any time
interface DefaultConfig {
  token: 'slack-bot-token',
  name: 'SlacklibBot',
  emoji: ':robot_face:',
  channel: 'general',
  timezone: 8
}

setup

This returns an object with the getter and setter functions used to control your configuration.

// Signature
interface Configuration {
  setConfig: SetConfig // Async function to update your configuration
  getConfig: GetConfig // Sync function to retrieve your configuration
  register: RegisterFunction
}

// privateKeys are keys that cannot be retrieved using @bot get [key]
// If you have passwords or tokens in your configuration, add those configuration keys to the privateKeys array when setting up
configure<YourConfig>(config: YourConfig & Partial<DefaultConfig>, privateKeys: string[]): Promise<Configuration>

/** ./config.ts */
import { setup } from 'slacklibbot'


// E.g.
const { getConfig, setConfig, register } = setup({
  token: 'xoxb-1234-abc',
  myKey: 42,
  name: 'My Bot Name'
  // plus any overrides of the DefaultConfig
})

export { getConfig, setConfig, register }

getConfig

DefaultParams is intended as a helper objet for calling postMessage. It contains the message options for the bot's username and emoji.

// Included in this framework
// Signature
interface DefaultParams {
  defaultParams: {
    icon_emoji: string
    username: string
    as_user: boolean
  }
}
function getConfig(): YourConfig & DefaultConfig & DefaultParams

/** ./my-module.ts (Sample module in your code) */
import { getConfig } from './config'
const myConfig = config.getConfig()

setConfig

// Signature
async function setConfig(key: keyof YourConfig & DefaultConfig, value: any): Promise<YourConfig & DefaultConfig>

/** ./another-module.ts (Sample module in your code) */
import { setConfig } from './config'

async function doThing() {
  // Override a default
  await setConfig('emoji', ':thumbs_up:')

  // Set your own keys
  await setConfig('myKey', { foo: 'bar' })
}

Commands API

Commands that you register are automatically added to the help response.

To call commands, you must mention the bot with the command and your parameters. E.g. If the bot's username (configured via the Slack Bot customisation user interface) is @mybot:

> @mybot help
> @mybot set emoji :thumbs_up:
> @mybot set name Slackbot

Built-in commands

> help
> get
> set

Command Registration

RegisterCallback

type RegisterCallback = (bot: SlackClient, message: Chat.Message, config: Config, params: string[])

register

function register(command: string, description: string, callback: RegisterCallback)

// Example
import { register } from './config'

register('my-command', 'This will appear when help is caled', (bot, msg, cfg, params) => {
  bot.postMessage({
    channel: msg.channel,
    text: `Message received!`,
    ...cfg.defaultParams // This is to apply the bot's username and emoji to its message
  }, )
})

Helper functions

readMessage

This allows you to write synchronous looking code that waits for a message

import { register, readMessage } from 'slacklibbot'

export interface ReadOptions {
  // Will only "read" messages that are directly to the bot
  directOnly?: boolean

  // Timeout in seconds
  timeout: number
}


// Signature
function register(bot: SlackClient, user: Users.User, options: ReadOptions): Promise<string>

// Example
register('my-command', 'My command description', (bot, msg, cfg) => {
  const msgCfg = { channel: bot.channel, ...cfg.defaultParams }
  await bot.postMessage({ ...msgCfg, text: 'Please respond:' })

  const response = await readMessage(bot, msg.user, { timeout: 10 })
  await bot.postMessage({ ...msgCfg, text: `Thanks for your response: ${response}` })
})