Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added simple Redis storage #38

Closed
wants to merge 35 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
f035d72
Fix param for bot.say() example code in readme.md
tonythomson Dec 17, 2015
3130e9f
adding some test infra but need some help
brianleroux Dec 17, 2015
b96bedc
adding tests
brianleroux Dec 17, 2015
da3af81
added clearinterval so Botkit cleanly exits on closeRTM
brianleroux Dec 18, 2015
6b8369e
added option to pass redirect_uri
lambtron Dec 20, 2015
a11c4fb
added mongo storage client
lambtron Dec 20, 2015
b4c794b
Adds file comment and example usage to simple_storage.js
RafaelCosman Dec 20, 2015
97cdcfc
Update readme.md
RafaelCosman Dec 20, 2015
ae395b5
Update readme.md
RafaelCosman Dec 20, 2015
443acf7
Update readme.md
RafaelCosman Dec 20, 2015
f5bff6d
inject RegExp match into message
guillaumepotier Dec 21, 2015
0fbc47a
updated README
guillaumepotier Dec 21, 2015
c0d20ab
fixes slash_command and outgoing webhook functions
Dec 22, 2015
4bb4407
reference channel variable when checking id
willrax Dec 24, 2015
dbe20be
swapped config for path
lambtron Dec 24, 2015
a5046a8
Package dependencies updated.
okvic77 Dec 27, 2015
0542bb2
changed to mongo_uri
lambtron Dec 27, 2015
4d73235
typo: cli command had incorrect filename
CyrusRoshan Dec 27, 2015
6bffb94
feature: made botkit.log optional
CyrusRoshan Dec 27, 2015
bc700eb
info: informed about optional logging, removed trailing whitespace
CyrusRoshan Dec 27, 2015
de93331
Adds comments to mongo_storage.js
RafaelCosman Dec 27, 2015
bbe6c99
Makes interface to simple_storage and mongo_storage consistent
RafaelCosman Dec 28, 2015
c52f38d
Comments storage_test.js
RafaelCosman Dec 28, 2015
150fade
Renames check->testStorageMethod in storage_test.js
RafaelCosman Dec 28, 2015
575a719
Updates storage documentation in README.md
RafaelCosman Dec 28, 2015
01a9d3e
Improves storage_test.js
RafaelCosman Dec 28, 2015
a7a78d8
Changes default behavior to log=true
RafaelCosman Dec 29, 2015
41d1167
Explains why match[1] is the first group
RafaelCosman Dec 29, 2015
65fb5ca
Improves regex example and adds S.O.2001 joke
RafaelCosman Dec 29, 2015
449a640
adjust method for shutting down bot process to make it friendly with …
Dec 29, 2015
b7dcf76
Update syntax for specifying a redirect URI to oauth
Dec 29, 2015
94f6ba6
add a negative test case for rtm connects
Dec 29, 2015
b008d52
update npm version
Dec 29, 2015
46b2cc1
added simple Redis storage. Moved storage classes to storages/ subdir
guillaumepotier Dec 21, 2015
7cb30b9
Redis storage | make tests pass + added allById method
guillaumepotier Dec 29, 2015
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ examples/db_slackbutton_slashcommand/
examples/db_team_bot/
.DS_Store
*/.DS_Store
.env
3 changes: 2 additions & 1 deletion examples/demo_bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ This bot demonstrates many of the core features of Botkit:

Run your bot from the command line:

token=<MY TOKEN> node team_bot.js
token=<MY TOKEN> node demo_bot.js

# USE THE BOT:

Expand Down Expand Up @@ -63,6 +63,7 @@ if (!process.env.token) {

var controller = Botkit.slackbot({
debug: false,
log: false
});

controller.spawn({
Expand Down
16 changes: 13 additions & 3 deletions lib/CoreBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* It expects to receive messages via the botkit.receiveMessage function */
/* These messages are expected to match Slack's message format. */
var mustache = require('mustache');
var simple_storage = require(__dirname+'/simple_storage.js');
var simple_storage = require(__dirname+'/storage/simple_storage.js');

function Botkit(configuration) {

Expand Down Expand Up @@ -601,7 +601,7 @@ function Botkit(configuration) {
},
save: function(channel,cb) {
botkit.log('Warning: using temporary storage. Data will be lost when process restarts.')
if (user.id) {
if (channel.id) {
botkit.memory_store['channels'][channel.id] = channel;
cb(null,channel.id);
} else {
Expand All @@ -625,11 +625,13 @@ function Botkit(configuration) {
}

botkit.log = function() {
if (configuration.log || configuration.log === undefined) { //default to true
var args=[];
for (var k = 0; k < arguments.length; k++) {
args.push(arguments[k]);
}
console.log.apply(null,args);
}
}

botkit.hears = function(keywords,events,cb) {
Expand All @@ -640,14 +642,16 @@ function Botkit(configuration) {
events = events.split(/\,/g);
}

var match;
for (var k = 0; k < keywords.length; k++) {
var keyword = keywords[k];
for (var e = 0; e < events.length; e++) {
(function(keyword) {
botkit.on(events[e],function(bot,message) {
if (message.text) {
if (message.text.match(new RegExp(keyword,'i'))) {
if (match = message.text.match(new RegExp(keyword,'i'))) {
botkit.debug("I HEARD ",keyword);
message.match = match;
cb.apply(this,[bot,message]);
return false;
}
Expand Down Expand Up @@ -715,6 +719,12 @@ function Botkit(configuration) {
}
}

botkit.shutdown = function() {
if (botkit.tickInterval) {
clearInterval(botkit.tickInterval);
}
}

botkit.startTask = function(bot,message,cb) {


Expand Down
17 changes: 11 additions & 6 deletions lib/SlackBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function Slackbot(configuration) {
} else {
slack_botkit.config.clientId = slack_app_config.clientId;
slack_botkit.config.clientSecret = slack_app_config.clientSecret;
if (slack_app_config.redirectUri) slack_botkit.config.redirectUri = slack_app_config.redirectUri;;
if (typeof(slack_app_config.scopes)=='string') {
slack_botkit.config.scopes = slack_app_config.scopes.split(/\,/);
} else {
Expand Down Expand Up @@ -89,7 +90,7 @@ function Slackbot(configuration) {

res.status(200);

var bot = botkit.spawn(team);
var bot = slack_botkit.spawn(team);

bot.team_info = team;
bot.res = res;
Expand Down Expand Up @@ -124,7 +125,7 @@ function Slackbot(configuration) {

res.status(200);

var bot = botkit.spawn(team);
var bot = slack_botkit.spawn(team);
bot.res = res;
bot.team_info = team;

Expand Down Expand Up @@ -194,8 +195,8 @@ function Slackbot(configuration) {
if (team_id) {
url = url + "&team=" + team_id;
}
if (slack_botkit.config.redirect_uri) {
url = url + "&redirect_uri="+redirect_uri;
if (slack_botkit.config.redirectUri) {
url = url + "&redirect_uri="+slack_botkit.config.redirectUri;
}

return url;
Expand Down Expand Up @@ -260,11 +261,15 @@ function Slackbot(configuration) {
var code = req.query.code;
var state = req.query.state;

oauth_access({
var opts = {
client_id: slack_botkit.config.clientId,
client_secret: slack_botkit.config.clientSecret,
code: code
},function(err,auth) {
};

if (slack_botkit.config.redirectUri) opts.redirect_uri = slack_botkit.config.redirectUri;

oauth_access(opts,function(err,auth) {

if (err) {
if (callback) {
Expand Down
1 change: 0 additions & 1 deletion lib/Slackbot_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ module.exports = function(botkit,config) {
}

bot.closeRTM = function() {

if (bot.rtm) {
bot.rtm.close();
}
Expand Down
54 changes: 0 additions & 54 deletions lib/simple_storage.js

This file was deleted.

61 changes: 61 additions & 0 deletions lib/storage/mongo_storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
var db = require('monk'); //https://www.npmjs.com/package/monk

module.exports = function(config) {

if (!config && !config.mongo_uri) throw new Error('Need to provide mongo address.');
/* Your mongo_uri will look something like
'mongodb://test:test@ds037145.mongolab.com:37145/slack-bot-test'
or 'localhost/mydb,192.168.1.1' */

var teams_db = db(config.mongo_uri).get('teams');
var users_db = db(config.mongo_uri).get('users');
var channels_db = db(config.mongo_uri).get('channels');

var unwrapFromList = function(cb) {
return function(err, data) {
if (err) {
cb(err, data);
} else {
cb(err, data[0]);
}
};
};

var storage = {
teams: {
get: function(team_id, cb) {
teams_db.find({id: team_id}, unwrapFromList(cb));
},
save: function(team_data, cb) {
teams_db.findAndModify({id: team_data.id}, team_data, {upsert: true, new: true}, cb);
},
all: function(cb) {
teams_db.find({}, cb);
}
},
users: {
get: function(user_id, cb) {
users_db.find({id: user_id}, unwrapFromList(cb));
},
save: function(user_data, cb) {
users_db.findAndModify({id: user_data.id}, user_data, {upsert: true, new: true}, cb);
},
all: function(cb) {
users_db.find({}, cb);
}
},
channels: {
get: function(channel_id, cb) {
channels_db.find({id: channel_id}, unwrapFromList(cb));
},
save: function(channel_data, cb) {
channels_db.findAndModify({id: channel_data.id}, channel_data, {upsert: true, new: true}, cb);
},
all: function(cb) {
channels_db.find({}, cb);
}
}
};

return storage;
};
63 changes: 63 additions & 0 deletions lib/storage/redis_storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
var redis = require('redis');

/*
* All optionnals
*
* config = {
* namespace: namespace,
* host: host,
* port: port
* }
*/
module.exports = function(config) {
config = config || {};
config.namespace = config.namespace || 'botkit:store';

var storage = {},
client = redis.createClient(config), // could pass specific redis config here
methods = config.methods || ['teams', 'users', 'channels'];

// Implements required API methods
for (var i = 0; i < methods.length; i++) {
storage[methods[i]] = function(hash) {
return {
get: function(id,cb) {
client.hget(config.namespace + ':' + hash, id, function (err, res) {
cb(err, JSON.parse(res));
});
},
save: function(object,cb) {
if (!object.id) // Silently catch this error?
return cb(new Error('The given object must have an id property'), {});

client.hset(config.namespace + ':' + hash, object.id, JSON.stringify(object), cb);
},
all: function(cb, options) {
client.hgetall(config.namespace + ':' + hash, function (err, res) {
if (err)
return cb(err, {});

if (null === res)
return cb(err, res);

var parsed;
var array = [];

for (var i in res) {
parsed = JSON.parse(res[i]);
res[i] = parsed;
array.push(parsed);
}

cb(err, options && options.type === 'object' ? res : array);
});
},
allById: function(cb) {
this.all(cb, {type: 'object'});
}
}
}(methods[i]);
}

return storage;
};