Comparing version 1.0.7 to 1.0.8
17
index.js
@@ -27,4 +27,5 @@ 'use strict'; | ||
* | ||
* @param {Function} cb Callback function for error handling | ||
* @callback {Error} Error returned in cb | ||
* @param {Function} cb cb(err, body) | ||
* @callback {Error} err An error connecting to slack | ||
* @callback {Object} body Body returned by slack on connection | ||
*/ | ||
@@ -57,2 +58,3 @@ start(cb) { | ||
self.channels = body.channels; | ||
self.self = body.self; | ||
self.ws = new WebSocket(wsUrl); | ||
@@ -62,3 +64,3 @@ | ||
debug("Socket opened", wsUrl); | ||
cb(); | ||
cb(null, body); | ||
}) | ||
@@ -89,3 +91,2 @@ self.ws.on('message', function(data, flags) { | ||
* @return {Object} message the slack message object | ||
* @return {string} channel the channel posted in | ||
* @return {Array} matches capture groups if regexp is specified | ||
@@ -114,4 +115,7 @@ */ | ||
* @param {string} channelId|channelName channel id or name you want to post in. uses default if omitted. | ||
* @param {Function} cb cb(err) | ||
* @return {Error} err an error | ||
*/ | ||
say(text, channelId) { | ||
say(text, channelId, cb) { | ||
if( !cb ) { cb = console.error; } | ||
var self = this; | ||
@@ -121,4 +125,3 @@ channelId = channelId || self.defaultChannel; | ||
if( channelId && channelId[0] == '#' ) { channelId = self.channelIdForName(channelId); } | ||
// TODO: surface this error | ||
if( !channelId ) { return console.error("Invalid channelId"); } | ||
if( !channelId ) { return cb(new Error("Invalid channelId")); } | ||
@@ -125,0 +128,0 @@ var message = { |
{ | ||
"name": "tinybot", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"description": "A tiny wrapper around the Slack RTM API that provides methods to listen for and send slack messages.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -30,44 +30,52 @@ # tinybot | ||
var Tinybot = require('tinybot'); | ||
var bot = new Tinybot('mySecretSlackToken'); | ||
```js | ||
var Tinybot = require('tinybot'); | ||
var bot = new Tinybot('mySecretSlackToken'); | ||
bot.say("Hello world"); // posts to #general by default | ||
bot.say("Hello world", "#random"); // post to a channel by name | ||
bot.say("Hello world", "C123456789"); // post to a channel by ID | ||
bot.say("Hello world"); // posts to #general by default | ||
bot.say("Hello world", "#random"); // post to a channel by name | ||
bot.say("Hello world", "C123456789"); // post to a channel by ID | ||
``` | ||
Basic listener | ||
var Tinybot = require('tinybot'); | ||
var bot = new Tinybot('mySecretSlackToken'); | ||
```js | ||
var Tinybot = require('tinybot'); | ||
var bot = new Tinybot('mySecretSlackToken'); | ||
bot.on('message', function(message) { | ||
bot.say(`Message received: ${JSON.stringify(message)}`); | ||
}) | ||
bot.on('message', function(message) { | ||
// message contains unedited message from slack websocket | ||
bot.say(`Message received: ${JSON.stringify(message)}`); | ||
}) | ||
``` | ||
Filtered listeners | ||
var Tinybot = require('tinybot'); | ||
var bot = new Tinybot('mySecretSlackToken'); | ||
```js | ||
// listen for only messages in a channel | ||
bot.hears({channel: '#general'}, function() { | ||
bot.say("I heard that!", '#general') | ||
}) | ||
var Tinybot = require('tinybot'); | ||
var bot = new Tinybot('mySecretSlackToken', '#random'); | ||
// rain on everyone's parade with regex matches | ||
bot.hears({channel: '#general', text: /I love (.*)/}, function foo(message, matches) { | ||
bot.say(`${matches[1]} sucks`); | ||
}) | ||
// listen for only messages in a channel | ||
bot.hears({channel: '#general'}, function() { | ||
bot.say("I heard that!", '#general') | ||
}) | ||
// trolls anyone who posts in #random from an iPhone with nested matchers | ||
bot.hears({channel: '#random', "file.name": /Slack for iOS/}, function fooBar(message) { | ||
bot.say("ooooh, fancy", '#random'); | ||
}) | ||
// rain on everyone's parade with regex matches | ||
bot.hears({channel: '#general', text: /I love (.*)/}, function foo(message, matches) { | ||
bot.say(`${matches[1]} sucks`); | ||
}) | ||
// snooze one meeting with hearsOnce | ||
bot.hearsOnce({channel: '#sales', text: /meeting/}, function() { | ||
bot.say("let's circle back and put a pin in this. I'm gonna take a quick 5", '#sales'); | ||
}) | ||
// trolls anyone who posts in #random from an iPhone with nested matchers | ||
bot.hears({channel: '#random', "file.name": /Slack for iOS/}, function fooBar(message) { | ||
bot.say("ooooh, fancy", '#random'); | ||
}) | ||
bot.drop(/foo.*/); // deregister functions named foo and fooBar | ||
bot.drop(/.*/); // deregister all listeners | ||
// snooze one meeting with hearsOnce | ||
bot.hearsOnce({channel: '#sales', text: /meeting/}, function() { | ||
bot.say("let's circle back and put a pin in this. I'm gonna take a quick 5", '#sales'); | ||
}) | ||
bot.drop(/foo.*/); // deregister functions named foo and fooBar | ||
bot.drop(/.*/); // deregister all listeners | ||
``` |
@@ -9,3 +9,3 @@ var expect = require('expect'); | ||
if( !slackToken ) { return console.error("Put your slack token into secrets.json in this folder") } | ||
if( !slackToken ) { return console.error("Put your slack token into secrets.json in this folder, under the key 'token'") } | ||
@@ -99,3 +99,3 @@ describe('live calls', function() { | ||
bot.hears({text: 'sick', channel: '#general'}, spy); | ||
bot.hears({text: 'sick'}, function() { | ||
bot.hears({channel: '#general'}, function() { | ||
if( ++counter == 2 ) { | ||
@@ -105,2 +105,3 @@ expect(spy.calls.length).toEqual(1); | ||
} | ||
debug(counter); | ||
}) | ||
@@ -110,3 +111,5 @@ | ||
slackTest.socket.send({ text: 'sick', channel: 'CG0'}) | ||
slackTest.socket.send({ text: 'sick', channel: 'NOPE'}) | ||
slackTest.socket.send({ text: 'sick', channel: 'NOPE'}, function(err) { | ||
expect(err).toBeTruthy(); | ||
}) | ||
}) | ||
@@ -141,2 +144,21 @@ | ||
it('matches booleans', function(cb) { | ||
bot.hears({"file": true}, function(message) { | ||
bot.say('wow, nice file.'); | ||
}) | ||
var conversation = [ | ||
{ | ||
file: { | ||
name: 'Anything' | ||
} | ||
}, | ||
{ | ||
response: 'wow, nice file.' | ||
} | ||
] | ||
slackTest.expectConversation(conversation, cb); | ||
}) | ||
describe('drop', function() { | ||
@@ -143,0 +165,0 @@ it('allows dropping by function name', function(cb) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18254
426
81