From e5a508e6969e7c3a1c6233d926bfadd2ac4b483f Mon Sep 17 00:00:00 2001 From: Scott Hamilton Date: Sat, 14 Feb 2015 19:54:35 +1100 Subject: [PATCH 1/2] add ability to send messages using chat.postMessage() and get a channelList --- slack.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/slack.js b/slack.js index 9282665..b8e1c8e 100644 --- a/slack.js +++ b/slack.js @@ -3,9 +3,10 @@ var request = require('request'); var deferred = require('deferred'); -function Slack(domain, token, http_proxy_options) { +function Slack(domain, token, http_proxy_options, token2) { this.domain = domain; this.token = token; + this.token2 = token2 || token; this.http_proxy_options = http_proxy_options; } @@ -70,4 +71,55 @@ Slack.prototype.respond = function(query,cb) { } }; +Slack.prototype.message = function(message, cb) { + if (!message.text) { + if (cb) cb.call(null,{message:'No text specified'},null); + return; + } + if (!message.channel) { message.channel = '#general'; } + + var command = 'https://' + this.domain + '.slack.com/api/chat.postMessage?token=' + this.token2 + '&channel='+message.channel+'&text='+encodeURI(message.text)+'&username='+encodeURI(message.username); + if (message.icon_url) { command.concat("icon_url=",encodeURI(message.icon_url)) } + if (message.icon_emoji) { command.concat("icon_emoji=",encodeURI(message.icon_emoji)); } + var option = { + proxy: (this.http_proxy_options && this.http_proxy_options.proxy) || process.env.https_proxy || process.env.http_proxy, + url: command + }; + + if(!cb) var d = deferred(); + + var req = request.get(option, function(err, res, body) { + if (!err && body!='ok') { + err = {message: body}; + body = null; + } + if (d) return err ? d.reject(err) : d.resolve({res: res, body: body}); + if (cb) return cb.call(null, err, body); + return null; + }); + + return d ? d.promise : req; +}; + +Slack.prototype.channelList = function(cb) { + var command = "https://slack.com/api/channels.list?token="+this.token2+"&exclude_archived=1"; + var option = { + proxy: (this.http_proxy_options && this.http_proxy_options.proxy) || process.env.https_proxy || process.env.http_proxy, + url: command, + json: true + }; + if (!cb) var d = deferred(); + + var req = request.get(option, function(err, res, body){ + if (!err && body!='ok') { + err = {message: body}; + body = null; + } + if (d) return err ? d.reject(err) : d.resolve({res: res, body: body}); + if (cb) return cb.call(null, err, body); + return null; + }); + return d ? d.promise : req; +}; + module.exports = Slack; From 9473b2db21f0be7ec1332d195986c5a451bab229 Mon Sep 17 00:00:00 2001 From: Scott Hamilton Date: Sun, 15 Feb 2015 01:13:32 +1100 Subject: [PATCH 2/2] missing ampersand in build query string. --- slack.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/slack.js b/slack.js index b8e1c8e..780e8b6 100644 --- a/slack.js +++ b/slack.js @@ -78,9 +78,9 @@ Slack.prototype.message = function(message, cb) { } if (!message.channel) { message.channel = '#general'; } - var command = 'https://' + this.domain + '.slack.com/api/chat.postMessage?token=' + this.token2 + '&channel='+message.channel+'&text='+encodeURI(message.text)+'&username='+encodeURI(message.username); - if (message.icon_url) { command.concat("icon_url=",encodeURI(message.icon_url)) } - if (message.icon_emoji) { command.concat("icon_emoji=",encodeURI(message.icon_emoji)); } + var command = 'https://' + this.domain + '.slack.com/api/chat.postMessage?token=' + this.token2 + '&channel='+encodeURI(message.channel)+'&text='+encodeURI(message.text)+'&username='+encodeURI(message.username); + if (message.icon_url) { command.concat("&icon_url=",encodeURI(message.icon_url)) } + if (message.icon_emoji) { command.concat("&icon_emoji=",encodeURI(message.icon_emoji)); } var option = { proxy: (this.http_proxy_options && this.http_proxy_options.proxy) || process.env.https_proxy || process.env.http_proxy, url: command