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

tinyspeck

v1.4.2

Published

A lightweight adapter for node.js to interact with Slack's Web and RTM APIs

Downloads

123

Readme

TinySpeck

A lightweight adapter for node.js to interact with Slack's Web and RTM APIs.

Usage

Install

npm i tinyspeck --save

Sending Data

The TinySpeck client is a minimal wrapper around Slack's Web API Web. The default action is sending messages.

Posting Messages

The send method defaults to calling chat.postMessage.

const slack = require('tinyspeck')

let message = {
  unfurl_links: true,
  channel: 'C1QD223DS1',
  token: 'xoxb-12345678900-ABCD1234567890',
  text: "I am a test message http://slack.com",
  attachments: [{
    text: "And here's an attachment!"
  }]
}

// send message defaults to calling chat.postMessage
slack.send(message).then(data => {
  // Success!
})

Updating Messages

If your messages includes an ts property, it will call chat.update instead.

let message = {
  ts: "123422342134.234",
  channel: 'C1QD223DS1',
  token: 'xoxb-12345678900-ABCD1234567890',
  text: "Updated Message!!"
}

instance.send(message)

Respond to WebHooks

To respond to response urls, pass the url in place of a method name.

// respond to webhooks
slack.send('https://hooks.slack.com/services/T0000/B000/XXXX', message)

Calling Other API Methods

Access any of Slack's API Methods by passing in the method name.

let message = {
  token: 'xoxb-12345678900-ABCD1234567890'
}

// pass in the method name to call
slack.send('auth.test', message).then(data => {
  // Success!
})

Creating an Instance

Use to create a new instance of TinySpeck with a custom defaults

// create an instance with defaults
let instance = slack.instance({
  unfurl_links: true,
  channel: 'C1QD223DS1',
  token: 'xoxb-12345678900-ABCD1234567890'  
})

let message = {
  text: "I am a test message http://slack.com",
  attachments: [{
    text: "And here's an attachment!"
  }]
}

// send message to any Slack endpoint
instance.send('chat.postMessage', message)

Events

Event handlers that are triggered when messages are received from Slack.

// usage
slack.on('event name', [... 'event name',] callback)

// handle the "/test" slash commands
slack.on('/test', message => { })

// handle all slash commands
slack.on('slash_command', message => { })

// handle the outgoing webhooks trigger word "googlebot"
slack.on('googlebot', message => { })

// handle multiple events
slack.on('googlebot', '/test', 'slash_commands', message => { })

// wildcard support
slack.on('*', message => { })

RTM

Creates a connection to Slack's RTM API.

// options to pass to rtm.start
slack.rtm({ options }) // returns a promise

// basic
slack.rtm({ token: 'xoxb-12345678900-ABCD1234567890' }).then(ws => {    
  // connected are the websock is returned
})

// with defaults
let instance = slack.instance({
  token: 'xoxb-12345678900-ABCD1234567890'  
})

instance.rtm()

WebServer

A simple http server to receive JSON posts from Slack's WebHooks or Events.

// usage
slack.listen(port, 'validation token (optional)')

// example
slack.listen(3000, 'gIkuvaNzQIHg97ATvDxqgjtO')

WebSocket Proxy

TinySpeck can act as a WebSocket proxy, forwarding requests from Slack's HTTP POSTS to an open WebSocket connection and back. Because this will be an open connection, it will require a querystring verification to connect to.

Using Verification Token To Authenticate

Passing in true to the third parameter of listen will enable WebSockets using the Slack's Verification Token for authentication.

Server

slack.listen(3000, 'gIkuvaNzQIHg97ATvDxqgjtO', true)

Client

const WebSocket = require('ws')
const ws = new WebSocket('ws://yourserver.com?token=qtGI5L0SXbtiQfPY53UhkSIs');

Customizing Token and Parameters

If you would like more control over the token and parameter, you can call proxy after calling listen and provide custom values.

Server

let server = slack.listen(3000, 'gIkuvaNzQIHg97ATvDxqgjtO')
let proxy = slack.proxy(server, "CUSTOM_TOKEN", "custom_param")

Client

const WebSocket = require('ws')
const ws = new WebSocket('ws://yourserver.com?custom_param=CUSTOM_TOKEN');

Sending Messages Over WebSockets

Sending messages over the websocket will call the send method and pass through your message to chat.postMessage.

Client

const WebSocket = require('ws')
const ws = new WebSocket('ws://yourserver.com?token=qtGI5L0SXbtiQfPY53UhkSIs');

let message = {
  unfurl_links: true,
  channel: 'C1QD223DS1',
  token: 'xoxb-12345678900-ABCD1234567890',
  text: "I am a test message http://slack.com",
  attachments: [{
    text: "And here's an attachment!"
  }]
}

ws.send( JSON.stringify(message) )

Calling Other Methods

If you wanted to call another Slack API method, you can include the method property to your message object and it will all that method instead.

Client

let message = {
  method: 'auth.test',
  token: 'xoxb-12345678900-ABCD1234567890'
}

ws.send( JSON.stringify(message) )