Skip to content

Commit

Permalink
v1.3.6 - Add documentation for functions. Fix issue #42. JSLint/JSHin…
Browse files Browse the repository at this point in the history
…t happy tweaks.
  • Loading branch information
xBytez committed Oct 22, 2015
1 parent 691ea9a commit 28a12e9
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 31 deletions.
2 changes: 1 addition & 1 deletion examples/example_bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var slackAPI = require('slackbotapi');
var slack = new slackAPI({
'token': 'TOKENHERE',
'logging': true,
'autoReconnect': false
'autoReconnect': true
});

// Slack on EVENT message, send data.
Expand Down
116 changes: 88 additions & 28 deletions lib/rtm.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,18 @@ var errors = {
invalid_token: 'Invalid arguments! Please provide a valid auth token.',
send_args_required: 'Send: No arguments specified!',
data_type_undefined: 'data.type not defined'
}
};

// Slack RTM api
function slackAPI(args, err_cb) {
err_cb = err_cb || function () {};
var self = this;
var authtoken = args['token'];
var authtoken = args.token;

this.slackData = {};
this.token = '';
this.logging;
this.autoReconnect;
this.logging = true;
this.autoReconnect = true;
this.i = 0;

if (typeof args !== 'object') {
Expand All @@ -105,11 +105,11 @@ function slackAPI(args, err_cb) {
throw new Error(errors.object_arg_required);
}

if (typeof args['logging'] !== 'boolean') {
if (typeof args.logging !== 'boolean') {
this.logging = true;
this.out('error', errors.boolean_arg_required)
this.out('error', errors.boolean_arg_required);
} else {
this.logging = args['logging']
this.logging = args.logging;
}

if (!authtoken || typeof authtoken !== 'string' || !authtoken.match(/^([a-z]*)\-([0-9]*)\-([0-9a-zA-Z]*)/)) {
Expand All @@ -119,13 +119,13 @@ function slackAPI(args, err_cb) {
throw new Error(errors.invalid_token);
}

if (typeof args['autoReconnect'] !== 'boolean') {
if (typeof args.autoReconnect !== 'boolean') {
this.autoReconnect = false;
} else {
this.autoReconnect = args['autoReconnect'];
this.autoReconnect = args.autoReconnect;
}

this.authtoken = authtoken;
this.token = authtoken;

self.reqAPI('rtm.start', {}, function (data) {
if (!data.ok) return err_cb(data.error);
Expand All @@ -150,6 +150,13 @@ function slackAPI(args, err_cb) {
util.inherits(slackAPI, EventEmitter);

// Protoypes

/**
* Send a request to Slack's web API
* @param string method API method
* @param object data Object with request data
* @param function callback Callback function
*/
slackAPI.prototype.reqAPI = function (method, data, callback) {
data.token = this.token;

Expand All @@ -168,6 +175,10 @@ slackAPI.prototype.reqAPI = function (method, data, callback) {
}).form(data);
};

/**
* Send a message to the webSocket
* @param object data Data to send to Slack's websocket
*/
slackAPI.prototype.sendSock = function (data) {
if (typeof data !== 'undefined') {
data.id = this.i;
Expand All @@ -180,24 +191,37 @@ slackAPI.prototype.sendSock = function (data) {
}
};

/**
* Logging function
* @param string severity Severity of specified message
* @param string message Message to log
*/
slackAPI.prototype.out = function (severity, message) {
if (this.logging) {
logger(severity, 'SlackAPI', message);
}
};

/**
* Send a ping to Slack's socket
*/
slackAPI.prototype.ping = function () {
this.sendSock({
'type': 'ping'
});
};

/**
* Connect to Slack's websocket
* @param string wsurl URL to connect to Slack's websocket.
* @param {Function} cb Callback function
*/
slackAPI.prototype.connectSlack = function (wsurl, cb) {
var self = this;
self.ws = new webSocket(wsurl);
self.ws.on('open', function () {
self.out('transport', 'Connected as ' + self.slackData.self.name + ' [' + self.slackData.self.id + '].');
self.emit('open')
self.emit('open');

}).on('close', function (data) {
self.out('warning', 'Disconnected. Error: ' + data);
Expand All @@ -216,15 +240,15 @@ slackAPI.prototype.connectSlack = function (wsurl, cb) {
if (!err) {
self.emit(events[data.type], data);
} else {
self.emit('error', data)
self.emit('error', data);
}
});
});
}

}).on('error', function (data) {
self.out('error', 'Error. Error: ' + data);
self.emit('error', data)
self.emit('error', data);

}).on('message', function (data) {
self.out('transport', 'Received: ' + data);
Expand All @@ -236,7 +260,7 @@ slackAPI.prototype.connectSlack = function (wsurl, cb) {
self.reqAPI('users.list', messageData, function (data) {
self.slackData.users = data.members;
cb(null, messageData);
})
});
} else if (data.type === 'presence_change') {
// update slackData presence when user becomes active/inactive
for (var i in self.slackData.users) {
Expand All @@ -245,7 +269,7 @@ slackAPI.prototype.connectSlack = function (wsurl, cb) {
break;
}
}
cb(null,data);
cb(null, data);
} else if (typeof events[data.type] !== 'undefined') {
cb(null, data);
}
Expand All @@ -255,101 +279,132 @@ slackAPI.prototype.connectSlack = function (wsurl, cb) {
});
};

/**
* Get a channel by name or ID
* @param string term Search term
* @return object Returns object with channel if found, else null.
*/
slackAPI.prototype.getChannel = function (term) {
var channel = null,
self = this;
for (var i in self.slackData.channels) {
if (self.slackData.channels[i]['name'] === term) {
if (self.slackData.channels[i].name === term) {
channel = self.slackData.channels[i];
}
}
if (channel === null) {
for (var i_ in self.slackData.channels) {
if (self.slackData.channels[i_]['id'] === term) {
if (self.slackData.channels[i_].id === term) {
channel = self.slackData.channels[i_];
}
}
}
return channel;
};

/**
* Get a user by name or ID
* @param string term Search term
* @return object Returns object with user if found, else null.
*/
slackAPI.prototype.getUser = function (term) {
var user = null,
self = this;
for (var i in self.slackData.users) {
if (self.slackData.users[i]['name'] === term) {
if (self.slackData.users[i].name === term) {
user = self.slackData.users[i];
}
}
if (user === null) {
for (var i_ in self.slackData.users) {
if (self.slackData.users[i_]['id'] === term) {
if (self.slackData.users[i_].id === term) {
user = self.slackData.users[i_];
}
}
}
return user;
};

/**
* Get a user by e-mail address
* @param string term Search term
* @return object Returns object with user if found, else null.
*/
slackAPI.prototype.getUserByEmail = function (term) {
var user = null,
self = this;
for (var i in self.slackData.users) {
if (self.slackData.users[i]['profile']['email'] === term) {
if (self.slackData.users[i].profile.email === term) {
user = self.slackData.users[i];
}
}
if (user === null) {
for (var i_ in self.slackData.users) {
if (self.slackData.users[i_]['id'] === term) {
if (self.slackData.users[i_].id === term) {
user = self.slackData.users[i_];
}
}
}
return user;
};

/**
* Get IM by name or ID
* @param string term Search term
* @return object Returns object with IM if found, else null.
*/
slackAPI.prototype.getIM = function (term) {
var im = null,
self = this;
for (var i in self.slackData.ims) {
if (self.slackData.ims[i]['user'] === term) {
if (self.slackData.ims[i].user === term) {
im = self.slackData.ims[i];
}
}
if (im === null) {
var user = this.getUser(term);
if (user !== null) {
for (var i_ in self.slackData.ims) {
if (self.slackData.ims[i_]['user'] === user.id) {
if (self.slackData.ims[i_].user === user.id) {
im = self.slackData.ims[i_];
}
}
}
}
if (im === null) {
for (var i__ in self.slackData.ims) {
if (self.slackData.ims[i__]['id'] === term) {
if (self.slackData.ims[i__].id === term) {
im = self.slackData.ims[i__];
}
}
}
return im;
};

/**
* Get current saved data .
* @return object Returns object with locally saved data
*/
slackAPI.prototype.getSlackData = function () {
// allow process to access locally stored slackData
return this.slackData;
};

/**
* Indicates the user/bot is typing by sending a typing message to the socket
* @param string channel Channel/IM ID
*/
slackAPI.prototype.sendTyping = function (channel) {
this.sendSock({
'type': 'typing',
channel: channel
'channel': channel
});
return this;
};

/**
* Sends a message to a channel, private group or already existing IM/PM.
* @param string channel Channel/IM ID
* @param string text Message
*/
slackAPI.prototype.sendMsg = function (channel, text) {
this.sendSock({
'type': 'message',
Expand All @@ -358,6 +413,11 @@ slackAPI.prototype.sendMsg = function (channel, text) {
});
};

/**
* Send a direct message, if not created, create one.
* @param string userID Destination User ID
* @param string text Message
*/
slackAPI.prototype.sendPM = function (userID, text) {
var self = this;
var channel = self.getIM(userID);
Expand Down Expand Up @@ -386,7 +446,7 @@ slackAPI.prototype.sendPM = function (userID, text) {
});
}
};

slackAPI.prototype.sendIM = slackAPI.prototype.sendPM;

slackAPI.prototype.events = events;
module.exports = slackAPI;
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "slackbotapi",
"version": "1.3.5",
"version": "1.3.6",
"description": "a node.js API using Slack their RTM API",
"main": "index.js",
"scripts": {
Expand All @@ -26,7 +26,8 @@
},
"author": "xBytez <[email protected]>",
"contributors": [
"Samuel Mills <[email protected]>"
"Samuel Mills <[email protected]>",
"Igor Antun <[email protected]>"
],
"license": "LGPL-3.0",
"bugs": {
Expand Down

0 comments on commit 28a12e9

Please sign in to comment.