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 = 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.postMessageslack
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
Respond to WebHooks
To respond to response urls, pass the url in place of a method name.
// respond to webhooksslack
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 callslack
Creating an Instance
Use to create a new instance of TinySpeck with a custom defaults
// create an instance with defaultslet instance = slack let message = text: "I am a test message http://slack.com" attachments: text: "And here's an attachment!" // send message to any Slack endpointinstance
Events
Event handlers that are triggered when messages are received from Slack.
// usageslack // handle the "/test" slash commandsslack // handle all slash commandsslack // handle the outgoing webhooks trigger word "googlebot"slack // handle multiple eventsslack // wildcard supportslack
RTM
Creates a connection to Slack's RTM API.
// options to pass to rtm.startslack // returns a promise // basicslack // with defaultslet instance = slack instance
WebServer
A simple http server to receive JSON posts from Slack's WebHooks or Events.
// usageslack // exampleslack
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
Client
const WebSocket = const ws = '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 = slacklet proxy = slack
Client
const WebSocket = const ws = '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 = const ws = '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
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