Comparing version 0.4.1 to 0.4.2
@@ -9,3 +9,3 @@ module.exports = function(ferd) { | ||
ferd.listen(/yo/i, function(response) { | ||
var a = ferd.listen(/yo/i, function(response) { | ||
var sender = response.getMessageSender(); | ||
@@ -15,12 +15,8 @@ response.send(getRandomYo() + sender.name); | ||
}); | ||
// ferd.ignore(a); | ||
ferd.listen(/how many yo?/i, function(response) { | ||
response.send("You've said yo " + yoCount + " times"); | ||
ferd.session(/hello/, /goodbye/, function(response) { | ||
response.send("I hear you... " + response.getMessageSender().name); | ||
}); | ||
ferd.listen(/ferd poo/i, function(response) { | ||
var sender = response.getMessageSender(); | ||
response.sendDirectMessage('No, ' + sender.name + '. Poo be upon you!'); | ||
}); | ||
}; | ||
@@ -27,0 +23,0 @@ |
{ | ||
"name": "ferd", | ||
"version": "0.4.1", | ||
"version": "0.4.2", | ||
"description": "A modular Slackbot", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# A modular slackbot | ||
A modular Slacbot. More info coming soon. | ||
Ferd is a modular Slack Bot. It abstracts away the complexity of the Slack Real-Time Messaging API. | ||
# To Run | ||
Uses ES6. Runs using node --harmony | ||
``` | ||
var ferdModule = require('ferdModule'); | ||
var Ferd = require('ferd'); | ||
var ferd = new Ferd(); //create new ferd instance | ||
ferd.addModule(ferdModule); //inject module into ferd | ||
ferd.login(); //bot starts listening | ||
ferd.logout(); //bot stops listening | ||
``` | ||
# Slack Modules | ||
``` | ||
/* All variables in this closure are accessible by all bots using this module */ | ||
var randomYo = ["whats up? ", "hey ", "yo "]; | ||
var getRandomYo = function() { | ||
return randomYo[Math.floor(Math.random() * randomYo.length)]; | ||
}; | ||
module.exports = function(ferd) { | ||
/* All variables in this closure are accessible to all listeners in this module */ | ||
var yoCount = 0; | ||
/* Listens to all messages */ | ||
ferd.listen(/(.*) is (.*)/, function(response) { | ||
var sender = response.getMessageSender(); | ||
response.send("No, " + sender.name + ", you " + "aren't " + response.match[2]); | ||
}); | ||
/* Listens to messages tagged with bot's name or mentions bot */ | ||
var listener = ferd.respond(/yo/i, function(response) { | ||
var sender = response.getMessageSender(); | ||
response.send(getRandomYo() + sender.name); | ||
yoCount++; | ||
}); | ||
/* Stops listening on specific listener */ | ||
ferd.ignore(listener); | ||
/* Say 'hello' to join session and 'goodbye' to leave session. | ||
* Use to create poker and trivia games. | ||
*/ | ||
ferd.session(/hello/, /goodbye/, function(response) { | ||
response.send("I hear you... " + response.getMessageSender().name); | ||
}); | ||
/* Get all mentions of `yo` since bot's initialization */ | ||
ferd.listen(/how many yo?/ function(response) { | ||
response.send(yoCount); | ||
}); | ||
}; | ||
``` | ||
## Slack types | ||
https://api.slack.com/types | ||
https://api.slack.com/types |
@@ -5,8 +5,5 @@ var Slack = require('slack-client'); | ||
// TODO: FIGURE OUT HOW TO DESTROY OBSERVABLES | ||
// BUG: OBSERVABLES MUST BE EXPLICITLY DESTROYED. MEMORY LEAK! | ||
/** | ||
* Ferd | ||
* | ||
* | ||
* @constructor | ||
@@ -21,2 +18,4 @@ * @description A modular Slack Bot. Returns nothing. | ||
this.id = null; | ||
this.disposablesCounter = 0; | ||
this.disposables = {}; | ||
}; | ||
@@ -41,8 +40,14 @@ | ||
* | ||
* @description Closes WebSocket with Slack's Real-time Messaging API. Returns | ||
* nothing. | ||
* @description Closes WebSocket with Slack's Real-time Messaging API. Disposese all the disposables created | ||
* from listeners. Returns nothing. | ||
*/ | ||
Ferd.prototype.logout = function() { | ||
// destroy all observables created from this.messages | ||
for(var key in this.disposables) { | ||
this.disposables[key].dispose(); | ||
} | ||
this.slack.disconnect(); | ||
// destroy all observables created from this.messages | ||
}; | ||
@@ -52,3 +57,3 @@ | ||
* Ferd.prototype.addModule | ||
* | ||
* | ||
* @description Injects a module into Ferd. Returns nothing. | ||
@@ -64,3 +69,3 @@ * @param {module} ferdModule A module to inject into Ferd. Example: | ||
* Ferd.prototype.addModules | ||
* | ||
* | ||
* @description Injects modules into Ferd. Returns nothing. | ||
@@ -110,2 +115,43 @@ * @param {module[]} ferdModules An array of modules to inject into Ferd. | ||
/** | ||
* Allows multi-user sessions | ||
* @param {regex} hello regex triggered to register user in session | ||
* @param {regex} goodbye regex triggered to kick user out of session | ||
* @param {Function} callback callback for all user messages in session | ||
* @param {object} options | ||
* @return {disposable} garbage can | ||
*/ | ||
Ferd.prototype.session = function(hello, goodbye, callback, options) { | ||
var self = this; | ||
var users = {}; | ||
var helloCb = (options && options.hello ) || ((res) => {res.send("hello " + res.getMessageSender().name) }); | ||
var goodbyeCb = (options && options.goodbye) || ((res) => {res.send("goodbye " + res.getMessageSender().name)}); | ||
var goodbyeListener = this.listen(goodbye, (res) => { | ||
var userId = res.incomingMessage.user; | ||
users[userId] = null; | ||
goodbyeCb(res); | ||
}); | ||
var callbackListener = this.hear(m => { | ||
return !!users[m.user]; | ||
}, /.*/, callback); | ||
var helloListener = this.listen(hello, (res) => { | ||
var userId = res.incomingMessage.user; | ||
var username = res.getMessageSender().name; | ||
users[userId] = username; | ||
helloCb(res); | ||
}); | ||
var disposable = rx.Disposable.create(() => { | ||
callbackListener.dispose(); | ||
helloListener.dispose(); | ||
goodbyeListener.dispose(); | ||
}); | ||
return disposable; | ||
}; | ||
/** | ||
* Ferd.prototype.hear | ||
@@ -118,3 +164,3 @@ * | ||
* @param {Function} callback A callback with a response object passed in | ||
* @return {disposable} More reactive shenanigans. | ||
* @return {disposable} trashcan. | ||
*/ | ||
@@ -125,3 +171,3 @@ Ferd.prototype.hear = function(filter, capture, callback) { | ||
var messages = this.messages | ||
.filter(m => filter(m) && m.text.match(capture)); | ||
.filter(m => filter(m) && m.text && m.text.match(capture)); | ||
@@ -132,10 +178,25 @@ var disposable = messages | ||
callback(response); | ||
}, function(err) { | ||
console.error(err); | ||
}, function() { | ||
console.log('Completed!'); | ||
}); | ||
self.disposables[self.disposablesCounter++] = disposable; | ||
return disposable; | ||
}; | ||
}; | ||
/** | ||
* Ferd.prototype.ignore | ||
* | ||
* @description Stops listening on the stream, or more abstractly, disposes the disposable | ||
* @param {disposable} disposable The disposable to dispose | ||
*/ | ||
Ferd.prototype.ignore = function(disposable) { | ||
return disposable.dispose(); | ||
} | ||
/** | ||
* Ferd.prototype._createMessageStream | ||
* | ||
* | ||
* @private | ||
@@ -153,3 +214,3 @@ * @description Sets up a message stream. | ||
* Ferd.prototype._setUp | ||
* | ||
* | ||
* @private | ||
@@ -156,0 +217,0 @@ * @description Bootstraps Ferd identity. Returns nothing. |
@@ -40,4 +40,6 @@ /** | ||
* | ||
* @description Sends a message to same channel. Returns nothing. | ||
* @param {Object} outgoingMessage The message to send back. | ||
* @description Sends a message to same channel in which the incoming message | ||
* was captured. | ||
* @param {Object} outgoingMessage The message to send back | ||
* @return {Object} The message object | ||
*/ | ||
@@ -47,9 +49,30 @@ Response.prototype.send = function(outgoingMessage) { | ||
var channel = this.getChannelGroupOrDMByID(channelId); | ||
channel.send(outgoingMessage); | ||
var m = channel.send(outgoingMessage); | ||
return m; | ||
}; | ||
/** | ||
* Posts message to same channel | ||
* @param {obj} outgoingMessage A message object | ||
* Response.prototype.updateMessage | ||
* | ||
* @description Updates a message with a new string. Returns nothing. | ||
* @param {Object} message The message object | ||
* @param {String} string The new message to replace the old | ||
*/ | ||
Response.prototype.updateMessage = function(message, string) { | ||
if (message.ts) { | ||
message.updateMessage(string); | ||
} else { | ||
Object.observe(message, function(changes) { | ||
message.updateMessage(string); | ||
Object.unobserve(message); | ||
}, ['add']); | ||
} | ||
}; | ||
/** | ||
* Response.prototype.postMessage | ||
* | ||
* @description Posts message to same channel. Returns nothing. | ||
* @param {Object} outgoingMessage A message object | ||
*/ | ||
Response.prototype.postMessage = function(outgoingMessage) { | ||
@@ -56,0 +79,0 @@ var channelId = this.incomingMessage.channel; |
17148
10
454
62