From 117fbb1ad35363f30cbaa45ea0d2a57d5fc1745e Mon Sep 17 00:00:00 2001 From: ekmartin Date: Thu, 21 Jan 2016 16:40:55 +0100 Subject: [PATCH] Fix a bug where the bot-in-channel check would fail for private groups --- CHANGELOG.md | 4 ++++ lib/bot.js | 4 +++- test/bot.test.js | 19 +++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e89ae58..580e2d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # Changelog This project adheres to [Semantic Versioning](http://semver.org/). +## [3.7.4] - 2016-01-21 +### Fixed +- Fix a bug where the bot-in-channel check would fail for private groups. + ## [3.7.3] - 2016-01-12 ### Fixed - Don't crash when trying to send a message to a Slack channel the bot diff --git a/lib/bot.js b/lib/bot.js index 606a173..bacd9f0 100644 --- a/lib/bot.js +++ b/lib/bot.js @@ -206,7 +206,9 @@ class Bot { if (slackChannelName) { const slackChannel = this.slack.getChannelGroupOrDMByName(slackChannelName); - if (!slackChannel || !slackChannel.is_member) { + // If it's a private group and the bot isn't in it, we won't find anything here. + // If it's a channel however, we need to check is_member. + if (!slackChannel || (!slackChannel.is_member && !slackChannel.is_group)) { logger.info('Tried to send a message to a channel the bot isn\'t in: ', slackChannelName); return; diff --git a/test/bot.test.js b/test/bot.test.js index 5fba5f1..c25ffe1 100644 --- a/test/bot.test.js +++ b/test/bot.test.js @@ -54,6 +54,25 @@ describe('Bot', function() { ChannelStub.prototype.postMessage.should.have.been.calledWith(message); }); + it('should send messages to slack groups if the bot is in the channel', function() { + this.bot.slack.getChannelGroupOrDMByName = () => { + const channel = new ChannelStub(); + delete channel.is_member; + channel.is_group = true; + return channel; + }; + + const message = { + text: 'testmessage', + username: 'testuser', + parse: 'full', + icon_url: 'http://api.adorable.io/avatars/48/testuser.png' + }; + + this.bot.sendToSlack(message.username, '#irc', message.text); + ChannelStub.prototype.postMessage.should.have.been.calledWith(message); + }); + it('should not include an avatar for the bot\'s own messages', function() { const message = {