diff --git a/lib/index.js b/lib/index.js index 1d68804..4328322 100644 --- a/lib/index.js +++ b/lib/index.js @@ -78,9 +78,7 @@ util.inherits(SmartSlack, EventEmitter) SmartSlack.prototype.authTest = function (callback) { this._apiCall('auth.test', null, function test(data) { if (data) { - if (data.ok) { - callback(data); - } else { + if (callback) { callback(data); } } @@ -92,17 +90,38 @@ SmartSlack.prototype.authTest = function (callback) { * @return {object} passed args */ SmartSlack.prototype.apiTest = function (params, callback) { + this._apiCall('api.test', params, function test(data) { if (data) { - if (data.ok) { - callback(data); - } else { + if (callback) { callback(data); } } }) } +/** + * Add a reaction to a message + * @param emojiName i.e. thumbsup + * @returns {object} JSON response + */ +SmartSlack.prototype.addReaction = function (emojiName, channel, timestamp, callback) { + + if (emojiName && channel && timestamp && typeof emojiName === 'string' + && typeof channel === 'string' && typeof timestamp == 'string') { + + this._apiCall('reactions.add', { name: emojiName, + channel: channel, + timestamp: timestamp}, function (data) { + if (callback) { + callback(data); + } + }); + } else { + throw new Error(errors.missing_required_arg); + }; +}; + /** * Connect a WebSocket */ @@ -155,6 +174,29 @@ SmartSlack.prototype.getActiveChannels = function (callback) { }); } +/** + * Gets group list + * @param {function} callback + * @return {array} the group list + */ +SmartSlack.prototype.getActiveGroups = function (callback) { + var _this = this; + this._apiCall('groups.list', { "exclude_archived": "1" }, function (data) { + + if (data.ok) { + // Save the group list + _this.groups = data.groups; + if (callback) { + callback(data.groups); + } + } else { + if (callback) { + callback(data); + } + } + }); +} + /** * Gets a channel by name * @return {object} channel object @@ -180,6 +222,23 @@ SmartSlack.prototype.getChannelById = function (id) { } } +/** + * Gets group list + * @param {function} callback + * @return {array} the group list + */ +SmartSlack.prototype.getLastChannelMessage = function (channel, callback) { + if (channel && typeof channel === 'string') { + this._apiCall('channels.history', { channel: channel, count: 1 }, function (data) { + if (callback) { + callback(data); + } + }); + } else { + throw new Error(errors.missing_required_arg); + } +} + /** * Gets a group by id * @return {object} group object @@ -204,29 +263,6 @@ SmartSlack.prototype.getGroupByName = function (name) { } } -/** - * Gets group list - * @param {function} callback - * @return {array} the group list - */ -SmartSlack.prototype.getActiveGroups = function (callback) { - var _this = this; - this._apiCall('groups.list', { "exclude_archived": "1" }, function (data) { - - if (data.ok) { - // Save the group list - _this.groups = data.groups; - if (callback) { - callback(data.groups); - } - } else { - if (callback) { - callback(data); - } - } - }); -} - /** * Gets a user by name * @return {object} user object @@ -352,12 +388,14 @@ SmartSlack.prototype.postMessage = function (channelId, text, params, callback) params = _.extend({ channel: channelId, text: text, - username: this.name, - as_user: true + username: this.name }, params || {}); this._apiCall('chat.postMessage', params, function (data) { - callback(data); + if (callback) { + callback(data); + } + }); } else { throw new Error(errors.missing_required_arg); @@ -367,7 +405,7 @@ SmartSlack.prototype.postMessage = function (channelId, text, params, callback) /** * Sends a message via the RTM socket */ -SmartSlack.prototype.sendSocket = function (channel, text) { +SmartSlack.prototype.send = function (channel, text) { var msg; var data; if (channel && text) { @@ -390,7 +428,10 @@ SmartSlack.prototype.sendSocket = function (channel, text) { SmartSlack.prototype.setPresence = function (presence,callback) { if (presence && presence === 'away' || presence === 'auto') { this._apiCall('users.setPresence', { "presence": presence },function(data) { - callback(data); + if (callback) { + callback(data); + } + }) } else { throw new Error(errors.missing_required_arg); @@ -451,7 +492,7 @@ SmartSlack.prototype._reconnect = function () { } /** - * Makes a methos call to the Slack API + * Makes a method call to the Slack API */ SmartSlack.prototype._apiCall = function (method, params, callback) { @@ -486,9 +527,14 @@ SmartSlack.prototype._apiCall = function (method, params, callback) { if (callback) { if (res.statusCode === 200) { value = JSON.parse(output) - callback(value); + if (callback){ + callback(value); + } + } else { - callback({ 'ok': false, 'error': 'API response: ' + res.statusCode }) + if (callback) { + callback({ 'ok': false, 'error': 'API response: ' + res.statusCode }) + } } } diff --git a/test/index.js b/test/index.js index eeb971c..85a86df 100644 --- a/test/index.js +++ b/test/index.js @@ -108,6 +108,37 @@ describe('SmartSlack', function () { }); }); + + describe('#addReaction', function () { + + var slackClient = new SmartSlack(mockopts); + + it('exists as a public method on SmartSlack', function (done) { + expect(typeof slackClient.getActiveGroups).to.equal('function'); + done(); + }) + + it('should check for a valid arguments', function(done){ + expect(function () { + slackClient.addReaction(); + }).to.throw('Error missing or invalid required argument'); + done(); + }) + + it('should return api response', function (done) { + + var scope = nock('https://slack.com') + .post('/api/reactions.add') + .reply(200,{ok: true}); + + slackClient.addReaction('emoji','channel','timestamp', function (data) { + expect(data).to.be.an('object'); + expect(data.ok).to.equal(true); + done(); + }); + }); + + }); describe('#getActiveChannels', function () { @@ -147,6 +178,7 @@ describe('SmartSlack', function () { }); + describe('#getActiveGroups', function () { var response = { @@ -193,21 +225,6 @@ describe('SmartSlack', function () { { "id": "C024BE91L", "name": "fun", - "created": 1360782804, - "creator": "U024BE7LH", - "is_archived": false, - "is_member": false, - "num_members": 6, - "topic": { - "value": "Fun times", - "creator": "U024BE7LV", - "last_set": 1369677212 - }, - "purpose": { - "value": "This channel is for fun", - "creator": "U024BE7LH", - "last_set": 1360782804 - } }, ] @@ -240,21 +257,6 @@ describe('SmartSlack', function () { { "id": "C024BE91L", "name": "fun", - "created": 1360782804, - "creator": "U024BE7LH", - "is_archived": false, - "is_member": false, - "num_members": 6, - "topic": { - "value": "Fun times", - "creator": "U024BE7LV", - "last_set": 1369677212 - }, - "purpose": { - "value": "This channel is for fun", - "creator": "U024BE7LH", - "last_set": 1360782804 - } }, ] @@ -285,16 +287,7 @@ describe('SmartSlack', function () { slackClient.groups = [ { id: 'G0BC7NYJ0', name: 'groupname', is_group: true, - created: 1443296143, - creator: 'U0BC6D9V1', - is_archived: false, - is_mpim: false, - members: [ 'U0BC6D9V1', 'U0BN3JFH7' ], - topic: { value: '', creator: '', last_set: 0 }, - purpose: - { value: 'A private group', - creator: 'U0BC6D9V1', - last_set: 1443296143 } } ] + } ] it('exists as public method on SmartSlack', function (done) { expect(typeof slackClient.getGroupById).to.equal('function'); @@ -316,6 +309,53 @@ describe('SmartSlack', function () { }) }); + describe('#getLastChannelMessage', function () { + + var slackClient = new SmartSlack(mockopts); + + var response = { + "ok": true, + "latest": "1358547726.000003", + "messages": [ + { + "type": "message", + "ts": "1358546515.000008", + "user": "U2147483896", + "text": "Hello" + } + ], + "has_more": false + } + + + it('exists as public method on SmartSlack', function (done) { + expect(typeof slackClient.getLastChannelMessage).to.equal('function'); + done(); + }); + + it('should check for a valid channel argument', function(done){ + expect(function () { + slackClient.getLastChannelMessage(); + }).to.throw('Error missing or invalid required argument'); + done(); + }) + + it('should return a channel object', function (done) { + + var scope = nock('https://slack.com') + .post('/api/channels.history') + .reply(200, response); + + var channel = slackClient.getLastChannelMessage('C024BE91L', function(data) { + expect(data).to.be.an('object'); + expect(data.ok).to.equal(true); + expect(data.messages[0].text).to.equal('Hello'); + }); + + done(); + }) + }); + describe('#getUserByName', function () { var slackClient = new SmartSlack(mockopts);