From 2ed50d426d81c9e289cbc7000ce2687b12bc3c8a Mon Sep 17 00:00:00 2001 From: Zach Davis Date: Thu, 2 Oct 2014 15:42:03 -0700 Subject: [PATCH 1/2] Allow messages without 'text' If an attachment is used, you may not always want to send text with the message. Slack's hook API supports this. --- slack.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/slack.js b/slack.js index 757bd5e..daa9cc2 100644 --- a/slack.js +++ b/slack.js @@ -12,7 +12,7 @@ function Slack(options) { } Slack.prototype.send = function(message,cb) { - if( !( message && message instanceof Object && message.text ) ) { + if( !( message && message instanceof Object && ( message.text || message.attachments ) ) ) { if( cb && cb instanceof Function ) return cb(new Error('No message')); return 'No message'; } @@ -21,10 +21,10 @@ Slack.prototype.send = function(message,cb) { var channel = message.channel || this.defaultChannel; var options = { channel: channel, - text: message.text, - username: message.username, + username: message.username }; + if( message.text ) options.text = message.text; if( message.icon_emoji ) options.icon_emoji = message.icon_emoji; if( message.icon_url ) options.icon_url = message.icon_url; if( message.attachments ) options.attachments = message.attachments; From c029fc8bac439b4ec2f0e6b72a78202e58909e07 Mon Sep 17 00:00:00 2001 From: Zach Davis Date: Thu, 2 Oct 2014 15:51:50 -0700 Subject: [PATCH 2/2] with updated test --- test/testSlack.js | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/test/testSlack.js b/test/testSlack.js index b526112..1c4984f 100644 --- a/test/testSlack.js +++ b/test/testSlack.js @@ -30,7 +30,7 @@ describe('test send', function(){ }; var expected = { url: 'https://testing.slack.com/services/hooks/incoming-webhook?token=testToken', - body: '{"channel":"#test","text":"hello","username":"test","icon_emoji":"smile","attachments":[{"fallback":"hello","color":"good","fields":[{"title":"col 1","value":"hello 1","short":true},{"title":"col 2","value":"hello 2","short":true}]}]}' + body: '{"channel":"#test","username":"test","text":"hello","icon_emoji":"smile","attachments":[{"fallback":"hello","color":"good","fields":[{"title":"col 1","value":"hello 1","short":true},{"title":"col 2","value":"hello 2","short":true}]}]}' }; request.post.callsArgWith(1, null, null, 'ok'); slack.send(input, function(err, res){ @@ -49,7 +49,7 @@ describe('test send', function(){ }; var expected = { url: 'https://testing.slack.com/services/hooks/incoming-webhook?token=testToken', - body: '{"channel":"#general","text":"hello","username":"test","icon_url":"drnick.png"}' + body: '{"channel":"#general","username":"test","text":"hello","icon_url":"drnick.png"}' }; request.post.callsArgWith(1, null, null, 'ok'); slack.send(input, function(err, res){ @@ -70,6 +70,33 @@ describe('test send', function(){ }); }); + it('should allow messages with attachments without text', function(done){ + var input = { + channel: '#test', + username: 'test', + icon_emoji: 'smile', + attachments : [{ + fallback: 'hello', + color: 'good', + fields: [ + {title: 'col 1', value: 'hello 1', short: true}, + {title: 'col 2', value: 'hello 2', short: true} + ] + }] + }; + var expected = { + url: 'https://testing.slack.com/services/hooks/incoming-webhook?token=testToken', + body: '{"channel":"#test","username":"test","icon_emoji":"smile","attachments":[{"fallback":"hello","color":"good","fields":[{"title":"col 1","value":"hello 1","short":true},{"title":"col 2","value":"hello 2","short":true}]}]}' + }; + request.post.callsArgWith(1, null, null, 'ok'); + slack.send(input, function(err, res){ + assert.ok(!err); + assert.equal(res, 'ok'); + assert.deepEqual(request.post.getCall(0).args[0], expected); + done(); + }); + }); + afterEach(function(){ request.post.restore(); });