slackbotapi
Advanced tools
Comparing version 1.3.4 to 1.3.5
@@ -17,3 +17,4 @@ ////////////////////////////////////////////////////////////////// | ||
'token': "TOKENHERE", | ||
'logging': true | ||
'logging': true, | ||
'autoReconnect': false | ||
}); | ||
@@ -47,7 +48,3 @@ | ||
break; | ||
case "hue": | ||
slack.sendMsg(data.channel, "@"+slack.getUser(data.user).name+" brbrbrbrbrb!") | ||
break; | ||
case "say": | ||
@@ -57,6 +54,2 @@ var say = data.text.split('%say '); | ||
break; | ||
case "debug": | ||
console.log(slack.data.ims); | ||
break; | ||
} | ||
@@ -63,0 +56,0 @@ } |
191
index.js
@@ -12,8 +12,8 @@ ////////////////////////////////////////////////////////////////// | ||
// Requiring node modules. | ||
var os = require('os'); | ||
var webSocket = require('ws'); | ||
var request = require('request'); | ||
var logger = require('jethro'); | ||
var util = require('util'); | ||
var EventEmitter= require('eventemitter3'); | ||
var os = require('os'); | ||
var webSocket = require('ws'); | ||
var request = require('request'); | ||
var logger = require('jethro'); | ||
var util = require('util'); | ||
var EventEmitter = require('eventemitter3'); | ||
@@ -83,10 +83,12 @@ var events = { | ||
var errors = { | ||
object_arg_required: 'Invalid arguments! Please provide an object with settings.', | ||
boolean_arg_required: 'Invalid arguments! Please provide a valid boolean for logging.', | ||
invalid_token: 'Invalid arguments! Please provide a valid auth token.', | ||
send_args_required: 'Send: No arguments specified!', | ||
data_type_undefined: 'data.type not defined' | ||
object_arg_required: 'Invalid arguments! Please provide an object with settings.', | ||
boolean_arg_required: 'Invalid arguments! Please provide a valid boolean for logging.', | ||
invalid_token: 'Invalid arguments! Please provide a valid auth token.', | ||
send_args_required: 'Send: No arguments specified!', | ||
data_type_undefined: 'data.type not defined' | ||
}; | ||
function slackAPI(args) { | ||
function slackAPI(args, error_cb) { | ||
error_cb = error_cb || function () { | ||
}; | ||
var self = this; | ||
@@ -98,2 +100,3 @@ var authtoken = args['token']; | ||
this.logging; | ||
this.autoReconnect; | ||
this.i = 0; | ||
@@ -103,5 +106,6 @@ | ||
this.logging = true; | ||
this.out('error', errrs.object_arg_required); | ||
this.out('error', errors.object_arg_required); | ||
throw new Error(errors.object_arg_required); | ||
} if (typeof args['logging'] !== 'boolean') { | ||
} | ||
if (typeof args['logging'] !== 'boolean') { | ||
this.logging = true; | ||
@@ -111,3 +115,4 @@ this.out('error', errors.boolean_arg_required); | ||
this.logging = args['logging']; | ||
} if (!authtoken || typeof authtoken !== 'string' || !authtoken.match(/^([a-z]*)\-([0-9]*)\-([0-9a-zA-Z]*)/)) { | ||
} | ||
if (!authtoken || typeof authtoken !== 'string' || !authtoken.match(/^([a-z]*)\-([0-9]*)\-([0-9a-zA-Z]*)/)) { | ||
this.logging = true; | ||
@@ -117,5 +122,11 @@ this.out('error', errors.invalid_token); | ||
} | ||
if (typeof args['autoReconnect'] !== 'boolean') { | ||
this.autoReconnect = false; | ||
} else { | ||
this.autoReconnect = args['autoReconnect']; | ||
} | ||
this.token = authtoken; | ||
self.reqAPI('rtm.start', {}, function (data) { | ||
if (!data.ok) return error_cb(data.error); | ||
self.slackData.self = data.self; | ||
@@ -127,9 +138,12 @@ self.slackData.team = data.team; | ||
self.slackData.ims = data.ims; | ||
self.connectSlack(data.url, function(err, data){ | ||
if (!err){ | ||
self.connectSlack(data.url, function (err, data) { | ||
if (!err) { | ||
self.emit(events[data.type], data); | ||
} | ||
else { | ||
self.emit('error', data) | ||
} | ||
}); | ||
}); | ||
}; | ||
} | ||
@@ -139,6 +153,6 @@ util.inherits(slackAPI, EventEmitter); | ||
// Protoypes | ||
slackAPI.prototype.reqAPI = function(method, data, callback) { | ||
slackAPI.prototype.reqAPI = function (method, data, callback) { | ||
data.token = this.token; | ||
if (typeof data.attachments !== 'undefined') { | ||
data.attachments = JSON.stringify(data.attachments); | ||
data.attachments = JSON.stringify(data.attachments); | ||
} | ||
@@ -156,7 +170,7 @@ request.post('https://slack.com/api/' + method, function (error, response, body) { | ||
slackAPI.prototype.ping = function() { | ||
slackAPI.prototype.ping = function () { | ||
this.sendSock({'type': 'ping'}); | ||
}; | ||
slackAPI.prototype.out = function(severity, message) { | ||
slackAPI.prototype.out = function (severity, message) { | ||
if (this.logging) { | ||
@@ -167,7 +181,7 @@ logger(severity, "SlackAPI", message); | ||
slackAPI.prototype.sendSock = function(data) { | ||
slackAPI.prototype.sendSock = function (data) { | ||
if (typeof data !== "undefined") { | ||
data.id = this.i; | ||
data = JSON.stringify(data); | ||
this.out("debug", "Send: "+data); | ||
this.out("debug", "Send: " + data); | ||
this.ws.send(data); | ||
@@ -180,25 +194,55 @@ this.i++; | ||
slackAPI.prototype.connectSlack = function(wsurl, cb) { | ||
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.ws.on('open', function () { | ||
self.out('transport', 'Connected as ' + self.slackData.self.name + ' [' + self.slackData.self.id + '].'); | ||
self.emit("open") | ||
}).on('close', function(data) { | ||
self.out('warning', 'Disconnected. Error: '+data); | ||
self.emit("close", data) | ||
}).on('error', function(data) { | ||
self.out('error', 'Error. Error: '+data); | ||
}).on('close', function (data) { | ||
self.out('warning', 'Disconnected. Error: ' + data); | ||
self.emit("close", data); | ||
if (self.autoReconnect) { | ||
// auto-reconnect | ||
self.reqAPI('rtm.start', {}, function (data) { | ||
if (!data.ok) return; | ||
self.slackData.self = data.self; | ||
self.slackData.team = data.team; | ||
self.slackData.channels = data.channels; | ||
self.slackData.groups = data.groups; | ||
self.slackData.users = data.users; | ||
self.slackData.ims = data.ims; | ||
self.connectSlack(data.url, function (err, data) { | ||
if (!err) { | ||
self.emit(events[data.type], data); | ||
} | ||
else { | ||
self.emit('error', data) | ||
} | ||
}); | ||
}); | ||
} | ||
}).on('error', function (data) { | ||
self.out('error', 'Error. Error: ' + data); | ||
self.emit("error", data) | ||
}).on('message', function(data) { | ||
}).on('message', function (data) { | ||
self.out('transport', "Received: " + data); | ||
data = JSON.parse(data); | ||
if (typeof data.type != 'undefined'){ | ||
if (typeof data.type !== 'undefined') { | ||
// update users list when new member joins | ||
if (data.type === 'team_join') { | ||
var messageData = data; // allow cb() to run when user.list refreshes | ||
self.reqAPI('users.list', messageData, function(data) { | ||
data.members = self.slackData.users; | ||
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) { | ||
if (self.slackData.users[i].id === data.user) { | ||
self.slackData.users[i].presence = data.presence; | ||
break; | ||
} | ||
} | ||
} | ||
else if (typeof events[data.type] !== 'undefined') { | ||
@@ -208,3 +252,3 @@ cb(null, data); | ||
} else { | ||
cb(new Error(errors.data_type_undefined)); | ||
cb(new Error(errors.data_type_undefined), data); | ||
} | ||
@@ -214,6 +258,6 @@ }); | ||
slackAPI.prototype.getChannel = function(term) { | ||
slackAPI.prototype.getChannel = function (term) { | ||
var channel = null, self = this; | ||
for(var i in self.slackData.channels) { | ||
if(self.slackData.channels[i]['name'] === term) { | ||
for (var i in self.slackData.channels) { | ||
if (self.slackData.channels[i]['name'] === term) { | ||
channel = self.slackData.channels[i]; | ||
@@ -223,4 +267,4 @@ } | ||
if (channel === null) { | ||
for(var i_ in self.slackData.channels) { | ||
if(self.slackData.channels[i_]['id'] === term) { | ||
for (var i_ in self.slackData.channels) { | ||
if (self.slackData.channels[i_]['id'] === term) { | ||
channel = self.slackData.channels[i_]; | ||
@@ -233,6 +277,6 @@ } | ||
slackAPI.prototype.getUser = function(term) { | ||
slackAPI.prototype.getUser = function (term) { | ||
var user = null, self = this; | ||
for(var i in self.slackData.users) { | ||
if(self.slackData.users[i]['name'] === term) { | ||
for (var i in self.slackData.users) { | ||
if (self.slackData.users[i]['name'] === term) { | ||
user = self.slackData.users[i]; | ||
@@ -242,4 +286,4 @@ } | ||
if (user === null) { | ||
for(var i_ in self.slackData.users) { | ||
if(self.slackData.users[i_]['id'] === term) { | ||
for (var i_ in self.slackData.users) { | ||
if (self.slackData.users[i_]['id'] === term) { | ||
user = self.slackData.users[i_]; | ||
@@ -252,6 +296,6 @@ } | ||
slackAPI.prototype.getUserByEmail = function(term) { | ||
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) { | ||
for (var i in self.slackData.users) { | ||
if (self.slackData.users[i]['profile']['email'] === term) { | ||
user = self.slackData.users[i]; | ||
@@ -261,4 +305,4 @@ } | ||
if (user === null) { | ||
for(var i_ in self.slackData.users) { | ||
if(self.slackData.users[i_]['id'] === term) { | ||
for (var i_ in self.slackData.users) { | ||
if (self.slackData.users[i_]['id'] === term) { | ||
user = self.slackData.users[i_]; | ||
@@ -271,6 +315,6 @@ } | ||
slackAPI.prototype.getIM = function(term) { | ||
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]; | ||
@@ -281,8 +325,8 @@ } | ||
var user = this.getUser(term); | ||
if(user !== null) { | ||
for (var i_ in self.slackData.ims) { | ||
if (self.slackData.ims[i_]['user'] === user.id) { | ||
im = self.slackData.ims[i_]; | ||
} | ||
} | ||
if (user !== null) { | ||
for (var i_ in self.slackData.ims) { | ||
if (self.slackData.ims[i_]['user'] === user.id) { | ||
im = self.slackData.ims[i_]; | ||
} | ||
} | ||
} | ||
@@ -298,22 +342,27 @@ } | ||
return im; | ||
} | ||
}; | ||
slackAPI.prototype.sendTyping = function(channel) { | ||
sendSock({'type': 'typing', channel: channel}); | ||
slackAPI.prototype.getSlackData = function () { | ||
// allow process to access locally stored slackData | ||
return this.slackData; | ||
}; | ||
slackAPI.prototype.sendTyping = function (channel) { | ||
this.sendSock({'type': 'typing', channel: channel}); | ||
return this; | ||
} | ||
}; | ||
slackAPI.prototype.sendMsg = function(channel, text) { | ||
slackAPI.prototype.sendMsg = function (channel, text) { | ||
this.sendSock({'type': 'message', 'channel': channel, 'text': text}); | ||
}; | ||
slackAPI.prototype.sendPM = function(userID, text) { | ||
slackAPI.prototype.sendPM = function (userID, text) { | ||
var self = this; | ||
var channel = self.getIM(userID); | ||
if(channel !== null) { | ||
self.sendSock({'type': 'message', 'channel': channel.id, 'text': text}); | ||
if (channel !== null) { | ||
self.sendSock({'type': 'message', 'channel': channel.id, 'text': text}); | ||
} else { | ||
if(this.getUser(userID)) userID = this.getUser(userID).id; // userID is username here | ||
self.reqAPI('im.open', { user : userID }, function(data){ | ||
if(data.ok === true) { | ||
if (this.getUser(userID)) userID = this.getUser(userID).id; // userID is username here | ||
self.reqAPI('im.open', {user: userID}, function (data) { | ||
if (data.ok === true) { | ||
self.slackData.ims.push(data.channel); | ||
@@ -320,0 +369,0 @@ self.sendSock({'type': 'message', 'channel': data.channel.id, 'text': text}); |
{ | ||
"name": "slackbotapi", | ||
"version": "1.3.4", | ||
"version": "1.3.5", | ||
"description": "a node.js API using Slack their RTM API", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,3 +5,3 @@ SlackbotAPI | ||
[![GitHub Issues](https://img.shields.io/github/issues/xBytez/slackbotapi.svg?style=flat-square)](https://github.com/xBytez/slackbotapi/issues) | ||
[![Current Version](https://img.shields.io/badge/version-1.3.4-green.svg?style=flat-square)](https://github.com/xBytez/slackbotapi) | ||
[![Current Version](https://img.shields.io/badge/version-1.3.5-green.svg?style=flat-square)](https://github.com/xBytez/slackbotapi) | ||
@@ -8,0 +8,0 @@ A Node.JS module for Slack's RTM API |
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
24276
387