@@ -84,18 +84,18 @@ var errors = {
8484 invalid_token : 'Invalid arguments! Please provide a valid auth token.' ,
8585 send_args_required : 'Send: No arguments specified!' ,
8686 data_type_undefined : 'data.type not defined'
87- }
87+ } ;
8888
8989// Slack RTM api
9090function slackAPI ( args , err_cb ) {
9191 err_cb = err_cb || function ( ) { } ;
9292 var self = this ;
93- var authtoken = args [ ' token' ] ;
93+ var authtoken = args . token ;
9494
9595 this . slackData = { } ;
9696 this . token = '' ;
97- this . logging ;
98- this . autoReconnect ;
97+ this . logging = true ;
98+ this . autoReconnect = true ;
9999 this . i = 0 ;
100100
101101 if ( typeof args !== 'object' ) {
@@ -105,11 +105,11 @@ function slackAPI(args, err_cb) {
105105 throw new Error ( errors . object_arg_required ) ;
106106 }
107107
108- if ( typeof args [ ' logging' ] !== 'boolean' ) {
108+ if ( typeof args . logging !== 'boolean' ) {
109109 this . logging = true ;
110- this . out ( 'error' , errors . boolean_arg_required )
110+ this . out ( 'error' , errors . boolean_arg_required ) ;
111111 } else {
112- this . logging = args [ ' logging' ]
112+ this . logging = args . logging ;
113113 }
114114
115115 if ( ! authtoken || typeof authtoken !== 'string' || ! authtoken . match ( / ^ ( [ a - z ] * ) \- ( [ 0 - 9 ] * ) \- ( [ 0 - 9 a - z A - Z ] * ) / ) ) {
@@ -119,13 +119,13 @@ function slackAPI(args, err_cb) {
119119 throw new Error ( errors . invalid_token ) ;
120120 }
121121
122- if ( typeof args [ ' autoReconnect' ] !== 'boolean' ) {
122+ if ( typeof args . autoReconnect !== 'boolean' ) {
123123 this . autoReconnect = false ;
124124 } else {
125- this . autoReconnect = args [ ' autoReconnect' ] ;
125+ this . autoReconnect = args . autoReconnect ;
126126 }
127127
128- this . authtoken = authtoken ;
128+ this . token = authtoken ;
129129
130130 self . reqAPI ( 'rtm.start' , { } , function ( data ) {
131131 if ( ! data . ok ) return err_cb ( data . error ) ;
@@ -150,6 +150,13 @@ function slackAPI(args, err_cb) {
150150util . inherits ( slackAPI , EventEmitter ) ;
151151
152152// Protoypes
153+
154+ /**
155+ * Send a request to Slack's web API
156+ * @param string method API method
157+ * @param object data Object with request data
158+ * @param function callback Callback function
159+ */
153160slackAPI . prototype . reqAPI = function ( method , data , callback ) {
154161 data . token = this . token ;
155162
@@ -168,6 +175,10 @@ slackAPI.prototype.reqAPI = function (method, data, callback) {
168175 } ) . form ( data ) ;
169176} ;
170177
178+ /**
179+ * Send a message to the webSocket
180+ * @param object data Data to send to Slack's websocket
181+ */
171182slackAPI . prototype . sendSock = function ( data ) {
172183 if ( typeof data !== 'undefined' ) {
173184 data . id = this . i ;
@@ -180,24 +191,37 @@ slackAPI.prototype.sendSock = function (data) {
180191 }
181192} ;
182193
194+ /**
195+ * Logging function
196+ * @param string severity Severity of specified message
197+ * @param string message Message to log
198+ */
183199slackAPI . prototype . out = function ( severity , message ) {
184200 if ( this . logging ) {
185201 logger ( severity , 'SlackAPI' , message ) ;
186202 }
187203} ;
188204
205+ /**
206+ * Send a ping to Slack's socket
207+ */
189208slackAPI . prototype . ping = function ( ) {
190209 this . sendSock ( {
191210 'type' : 'ping'
192211 } ) ;
193212} ;
194213
214+ /**
215+ * Connect to Slack's websocket
216+ * @param string wsurl URL to connect to Slack's websocket.
217+ * @param {Function } cb Callback function
218+ */
195219slackAPI . prototype . connectSlack = function ( wsurl , cb ) {
196220 var self = this ;
197221 self . ws = new webSocket ( wsurl ) ;
198222 self . ws . on ( 'open' , function ( ) {
199223 self . out ( 'transport' , 'Connected as ' + self . slackData . self . name + ' [' + self . slackData . self . id + '].' ) ;
200- self . emit ( 'open' )
224+ self . emit ( 'open' ) ;
201225
202226 } ) . on ( 'close' , function ( data ) {
203227 self . out ( 'warning' , 'Disconnected. Error: ' + data ) ;
@@ -216,15 +240,15 @@ slackAPI.prototype.connectSlack = function (wsurl, cb) {
216240 if ( ! err ) {
217241 self . emit ( events [ data . type ] , data ) ;
218242 } else {
219- self . emit ( 'error' , data )
243+ self . emit ( 'error' , data ) ;
220244 }
221245 } ) ;
222246 } ) ;
223247 }
224248
225249 } ) . on ( 'error' , function ( data ) {
226250 self . out ( 'error' , 'Error. Error: ' + data ) ;
227- self . emit ( 'error' , data )
251+ self . emit ( 'error' , data ) ;
228252
229253 } ) . on ( 'message' , function ( data ) {
230254 self . out ( 'transport' , 'Received: ' + data ) ;
@@ -236,7 +260,7 @@ slackAPI.prototype.connectSlack = function (wsurl, cb) {
236260 self . reqAPI ( 'users.list' , messageData , function ( data ) {
237261 self . slackData . users = data . members ;
238262 cb ( null , messageData ) ;
239- } )
263+ } ) ;
240264 } else if ( data . type === 'presence_change' ) {
241265 // update slackData presence when user becomes active/inactive
242266 for ( var i in self . slackData . users ) {
@@ -245,7 +269,7 @@ slackAPI.prototype.connectSlack = function (wsurl, cb) {
245269 break ;
246270 }
247271 }
248- cb ( null , data ) ;
272+ cb ( null , data ) ;
249273 } else if ( typeof events [ data . type ] !== 'undefined' ) {
250274 cb ( null , data ) ;
251275 }
@@ -255,101 +279,132 @@ slackAPI.prototype.connectSlack = function (wsurl, cb) {
255279 } ) ;
256280} ;
257281
282+ /**
283+ * Get a channel by name or ID
284+ * @param string term Search term
285+ * @return object Returns object with channel if found, else null.
286+ */
258287slackAPI . prototype . getChannel = function ( term ) {
259288 var channel = null ,
260289 self = this ;
261290 for ( var i in self . slackData . channels ) {
262- if ( self . slackData . channels [ i ] [ ' name' ] === term ) {
291+ if ( self . slackData . channels [ i ] . name === term ) {
263292 channel = self . slackData . channels [ i ] ;
264293 }
265294 }
266295 if ( channel === null ) {
267296 for ( var i_ in self . slackData . channels ) {
268- if ( self . slackData . channels [ i_ ] [ 'id' ] === term ) {
297+ if ( self . slackData . channels [ i_ ] . id === term ) {
269298 channel = self . slackData . channels [ i_ ] ;
270299 }
271300 }
272301 }
273302 return channel ;
274303} ;
275304
305+ /**
306+ * Get a user by name or ID
307+ * @param string term Search term
308+ * @return object Returns object with user if found, else null.
309+ */
276310slackAPI . prototype . getUser = function ( term ) {
277311 var user = null ,
278312 self = this ;
279313 for ( var i in self . slackData . users ) {
280- if ( self . slackData . users [ i ] [ ' name' ] === term ) {
314+ if ( self . slackData . users [ i ] . name === term ) {
281315 user = self . slackData . users [ i ] ;
282316 }
283317 }
284318 if ( user === null ) {
285319 for ( var i_ in self . slackData . users ) {
286- if ( self . slackData . users [ i_ ] [ 'id' ] === term ) {
320+ if ( self . slackData . users [ i_ ] . id === term ) {
287321 user = self . slackData . users [ i_ ] ;
288322 }
289323 }
290324 }
291325 return user ;
292326} ;
293327
328+ /**
329+ * Get a user by e-mail address
330+ * @param string term Search term
331+ * @return object Returns object with user if found, else null.
332+ */
294333slackAPI . prototype . getUserByEmail = function ( term ) {
295334 var user = null ,
296335 self = this ;
297336 for ( var i in self . slackData . users ) {
298- if ( self . slackData . users [ i ] [ ' profile' ] [ ' email' ] === term ) {
337+ if ( self . slackData . users [ i ] . profile . email === term ) {
299338 user = self . slackData . users [ i ] ;
300339 }
301340 }
302341 if ( user === null ) {
303342 for ( var i_ in self . slackData . users ) {
304- if ( self . slackData . users [ i_ ] [ 'id' ] === term ) {
343+ if ( self . slackData . users [ i_ ] . id === term ) {
305344 user = self . slackData . users [ i_ ] ;
306345 }
307346 }
308347 }
309348 return user ;
310349} ;
311350
351+ /**
352+ * Get IM by name or ID
353+ * @param string term Search term
354+ * @return object Returns object with IM if found, else null.
355+ */
312356slackAPI . prototype . getIM = function ( term ) {
313357 var im = null ,
314358 self = this ;
315359 for ( var i in self . slackData . ims ) {
316- if ( self . slackData . ims [ i ] [ ' user' ] === term ) {
360+ if ( self . slackData . ims [ i ] . user === term ) {
317361 im = self . slackData . ims [ i ] ;
318362 }
319363 }
320364 if ( im === null ) {
321365 var user = this . getUser ( term ) ;
322366 if ( user !== null ) {
323367 for ( var i_ in self . slackData . ims ) {
324- if ( self . slackData . ims [ i_ ] [ ' user' ] === user . id ) {
368+ if ( self . slackData . ims [ i_ ] . user === user . id ) {
325369 im = self . slackData . ims [ i_ ] ;
326370 }
327371 }
328372 }
329373 }
330374 if ( im === null ) {
331375 for ( var i__ in self . slackData . ims ) {
332- if ( self . slackData . ims [ i__ ] [ 'id' ] === term ) {
376+ if ( self . slackData . ims [ i__ ] . id === term ) {
333377 im = self . slackData . ims [ i__ ] ;
334378 }
335379 }
336380 }
337381 return im ;
338382} ;
339383
384+ /**
385+ * Get current saved data .
386+ * @return object Returns object with locally saved data
387+ */
340388slackAPI . prototype . getSlackData = function ( ) {
341- // allow process to access locally stored slackData
342389 return this . slackData ;
343390} ;
344391
392+ /**
393+ * Indicates the user/bot is typing by sending a typing message to the socket
394+ * @param string channel Channel/IM ID
395+ */
345396slackAPI . prototype . sendTyping = function ( channel ) {
346397 this . sendSock ( {
347398 'type' : 'typing' ,
348- channel : channel
399+ ' channel' : channel
349400 } ) ;
350- return this ;
351401} ;
352402
403+ /**
404+ * Sends a message to a channel, private group or already existing IM/PM.
405+ * @param string channel Channel/IM ID
406+ * @param string text Message
407+ */
353408slackAPI . prototype . sendMsg = function ( channel , text ) {
354409 this . sendSock ( {
355410 'type' : 'message' ,
@@ -358,6 +413,11 @@ slackAPI.prototype.sendMsg = function (channel, text) {
358413 } ) ;
359414} ;
360415
416+ /**
417+ * Send a direct message, if not created, create one.
418+ * @param string userID Destination User ID
419+ * @param string text Message
420+ */
361421slackAPI . prototype . sendPM = function ( userID , text ) {
362422 var self = this ;
363423 var channel = self . getIM ( userID ) ;
@@ -386,7 +446,7 @@ slackAPI.prototype.sendPM = function (userID, text) {
386446 } ) ;
387447 }
388448} ;
389-
449+ slackAPI . prototype . sendIM = slackAPI . prototype . sendPM ;
390450
391451slackAPI . prototype . events = events ;
392452module . exports = slackAPI ;
0 commit comments