Skip to content

Commit

Permalink
More cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
philliphenslee committed Oct 15, 2015
1 parent 76e9c85 commit 5c974d2
Showing 1 changed file with 77 additions and 47 deletions.
124 changes: 77 additions & 47 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
* @description A JavaScript Slack API client
* for building custom bots on node.js
* @file /lib/index.js
* @copyright Phillip J. Henslee II 2015
* @copyright Phillip J. Henslee II 2015 <ph2@ph2.us>
* @module smartslack
*/

'use strict';

var EventEmitter = require('events').EventEmitter;
Expand All @@ -19,19 +20,19 @@ var bole = require('bole')
var pretty = require('bistre')({ time: true })
var WebSocket = require('ws');

// Bole
bole.output({
level: process.env.LOG_LEVEL || 'info'
, stream: pretty
})
pretty.pipe(process.stdout);

var errors = {
invalid_token: 'Error invalid access token, please provide a valid token.',
missing_options_arg: 'Error missing required argument object options',
missing_required_arg: 'Error missing or invalid required argument'
}

// Bole Logging
bole.output({
level: process.env.LOG_LEVEL || 'debug'
, stream: pretty
})
pretty.pipe(process.stdout);

/**
* Slack RTM API Client
* @constructor
Expand Down Expand Up @@ -299,6 +300,34 @@ SmartSlack.prototype.getGroupByName = function (name, callback) {
}
};

/**
* Gets a im channel id
* @param {string} user name
* @return {object} user object
*/
SmartSlack.prototype.getImChannelId = function (name, callback) {

var _this = this;
var imChannel;
callback = (typeof callback === 'function') ? callback : function () { };

if (name && typeof name === 'string') {

this.getUserByName(name, function (err, user) {
imChannel = _.find(_this.ims, { user: user.id });
if (imChannel) {
return callback(null, imChannel.id);
} else {
_this.openIm(user.id, function(err,channel) {
return callback(channel.id);
});
}
});
} else {
throw new Error(errors.missing_required_arg);
}
};

/**
* Gets the users presence
* @param {string} auto || away
Expand Down Expand Up @@ -411,53 +440,18 @@ SmartSlack.prototype.login = function () {
});
};

/**
* Handle slack RTM event message
* @param The slack RTM message
*/
SmartSlack.prototype._onRtmEvent = function (message) {

var _this = this;

this.emit('rawmessage', message);
this.log.debug({ 'Slack Event': message });

switch (message.type) {

case 'hello':

// Received the hello event message
this.log.debug('Slack RTM socket connected...')
this.connected = true;
this.emit('connected');
this._lastPong = _.now();

// Start pings every five seconds
this._pingInterval = setInterval(function () { _this._ping() }, 5000);
break;

case 'message':
this.emit('message', message);
break;

case 'pong':
this._lastPong = _.now();
this.log.debug('Latency = ' + (this._lastPong - message.time) + 'ms');
break;
}
};

/**
* Open a direct message channel
* @param user
* @param user id
* @returns {object} JSON response
*/
SmartSlack.prototype.openIm = function (user, callback) {
SmartSlack.prototype.openIm = function (userId, callback) {

callback = (typeof callback === 'function') ? callback : function () { };

if (user && typeof user === 'string') {
this._apiCall('im.open', { user: user }, function (data) {
if (userId && typeof userId === 'string') {
this._apiCall('im.open', { user: userId }, function (data) {
if (data.ok) {
return callback(null, data);
}
Expand Down Expand Up @@ -565,6 +559,42 @@ SmartSlack.prototype._canResolve = function (callback) {
});
};

/**
* Handle slack RTM event message
* @param The slack RTM message
*/
SmartSlack.prototype._onRtmEvent = function (message) {

var _this = this;

this.emit('rawmessage', message);
this.log.debug({ 'Slack Event': message });

switch (message.type) {

case 'hello':

// Received the hello event message
this.log.debug('Slack RTM socket connected...')
this.connected = true;
this.emit('connected');
this._lastPong = _.now();

// Start pings every five seconds
this._pingInterval = setInterval(function () { _this._ping() }, 5000);
break;

case 'message':
this.emit('message', message);
break;

case 'pong':
this._lastPong = _.now();
this.log.debug('Latency = ' + (this._lastPong - message.time) + 'ms');
break;
}
};

/**
* Sends ping message over the RTM
*/
Expand Down

0 comments on commit 5c974d2

Please sign in to comment.